Skip to main content

All Questions

Tagged with
Filter by
Sorted by
Tagged with
3792 votes
14 answers
2.5m views

What is __init__.py for?

What is __init__.py for in a Python source directory?
Mat's user avatar
  • 85.2k
851 votes
10 answers
358k views

What's the difference between a module and package in Python?

What's the difference between a module and package in Python? See also: What's the difference between "package" and "module"? (for other languages)
Dave's user avatar
  • 9,293
447 votes
24 answers
1.0m views

Python 3: ImportError "No Module named Setuptools"

I'm having troubles with installing packages in Python 3. I have always installed packages with setup.py install. But now, when I try to install the ansicolors package I get: importerror "No ...
user1994934's user avatar
  • 4,473
381 votes
6 answers
211k views

What is the purpose of the -m switch?

Could you explain to me what the difference is between calling python -m mymod1 mymod2.py args and python mymod1.py mymod2.py args It seems in both cases mymod1.py is called and sys.argv is ['...
Charles Brunet's user avatar
207 votes
3 answers
186k views

Execution of Python code with -m option or not [duplicate]

The python interpreter has -m module option that "Runs library module module as a script". With this python code a.py: if __name__ == "__main__": print __package__ print __name__ I tested ...
prosseek's user avatar
  • 188k
207 votes
15 answers
537k views

Can't import my own modules in Python

I'm having a hard time understanding how module importing works in Python (I've never done it in any other language before either). Let's say I have: myapp/__init__.py myapp/myapp/myapp.py myapp/...
user avatar
191 votes
4 answers
144k views

Can I use __init__.py to define global variables?

I want to define a constant that should be available in all of the submodules of a package. I've thought that the best place would be in in the __init__.py file of the root package. But I don't know ...
Andrei Vajna II's user avatar
181 votes
5 answers
245k views

Go update all modules

Using this module as an example (using a specific commit so others will see what I see): git clone git://github.com/walles/moar Set-Location moar git checkout d24acdbf I would like a way to tell Go ...
Zombo's user avatar
  • 1
137 votes
12 answers
167k views

Is there a standard way to list names of Python modules in a package?

Is there a straightforward way to list the names of all modules in a package, without using __all__? For example, given this package: /testpkg /testpkg/__init__.py /testpkg/modulea.py /testpkg/...
DNS's user avatar
  • 37.9k
135 votes
7 answers
113k views

List all the modules that are part of a python package?

Is there a straightforward way to find all the modules that are part of a python package? I've found this old discussion, which is not really conclusive, but I'd love to have a definite answer before ...
static_rtti's user avatar
  • 55.2k
133 votes
4 answers
128k views

Whats the difference between a module and a library in Python?

I have background in Java and I am new to Python. I want to make sure I understand correctly Python terminology before I go ahead. My understanding of a module is: a script which can be imported by ...
joker's user avatar
  • 2,073
129 votes
3 answers
210k views

Python: importing a sub‑package or sub‑module

Having already use flat packages, I was not expecting the issue I encountered with nested packages. Here is… Directory layout dir | +-- test.py | +-- package | +-- __init__.py |...
Hibou57's user avatar
  • 7,030
127 votes
13 answers
170k views

What is the most compatible way to install python modules on a Mac?

I'm starting to learn python and loving it. I work on a Mac mainly as well as Linux. I'm finding that on Linux (Ubuntu 9.04 mostly) when I install a python module using apt-get it works fine. I can ...
GloryFish's user avatar
  • 13.3k
124 votes
6 answers
164k views

Python: import module from another directory at the same level in project hierarchy

I've seen all sorts of examples and other similar questions, but I can't seem to find an example that exactly matches my scenario. I feel like a total goon asking this because there are so many ...
CptSupermrkt's user avatar
  • 6,994
91 votes
3 answers
38k views

How python deals with module and package having the same name?

Suppose I have a module foo.py and a package foo/. If I call import foo which one will be loaded? How can I specify I want to load the module, or the package?
Charles Brunet's user avatar
86 votes
3 answers
83k views

How to install my own python module (package) via conda and watch its changes

I have a file mysql.py, which I use in almost all of my projects. Since I do not want to copy and paste the same file into each of these projects I wrote a module - possibly a package in the future. ...
Michael's user avatar
  • 1,697
67 votes
2 answers
43k views

using __init__.py

I am having difficulty understanding the usage scenarios or design goals of python's __init__.py files in my projects. Assume that I have 'model' directory (refers as a package) which contains the ...
user avatar
63 votes
5 answers
40k views

Python: import the containing package

In a module residing inside a package, i have the need to use a function defined within the __init__.py of that package. how can i import the package within the module that resides within the package, ...
user avatar
61 votes
3 answers
39k views

Organizing Python classes in modules and/or packages

I like the Java convention of having one public class per file, even if there are sometimes good reasons to put more than one public class into a single file. In my case I have alternative ...
deamon's user avatar
  • 91.3k
57 votes
5 answers
68k views

What's the difference between "package" and "module"?

I use Java where we only have packages. I know there are other programming languages that also include modules. What's the difference?
OscarRyz's user avatar
  • 198k
56 votes
7 answers
38k views

How to reference python package when filename contains a period

I am using django and I have a file named models.admin.py and I want to do the following idea in models.py: from "models.admin" import * however, I get a syntax error for having double quotes. But ...
Alexander Bird's user avatar
52 votes
4 answers
35k views

How to organize multiple python files into a single module without it behaving like a package?

Is there a way to use __init__.py to organize multiple files into a module? Reason: Modules are easier to use than packages, because they don't have as many layers of namespace. Normally it makes a ...
Brian's user avatar
  • 923
50 votes
5 answers
27k views

The Pythonic way of organizing modules and packages

I come from a background where I normally create one file per class. I organize common classes under directories as well. This practice is intuitive to me and it has been proven to be effective in C++,...
carl's user avatar
  • 50.3k
44 votes
3 answers
23k views

Libraries, projects, modules and packages in Intellij Idea

I'm a beginner programmer and I'm learning how to work with Intellij IDEA. A project in IntelliJ IDEA has some different structures like libraries, modules and packages. Can someone explain what the ...
Michiel Van Couwenberghe's user avatar
39 votes
1 answer
28k views

Flask('application') versus Flask(__name__)

In the official Quickstart, it's recommended to use __name__ when using a single module: ... If you are using a single module (as in this example), you should use __name__ because depending on ...
nalzok's user avatar
  • 15.8k
39 votes
1 answer
16k views

When or why to use relative imports in Python [duplicate]

Are there any rules or guidelines concerning when to use relative imports in Python? I see them in use all the time, such as in the Flask web framework. When searching for this topic, I only see ...
trinth's user avatar
  • 6,019
34 votes
3 answers
9k views

How to structure python packages without repeating top level name for import

I'm brand new at python package management, and surely have done something wrong. I was encouraged to create a directory structure as follows: bagoftricks ├── bagoftricks │   ├── bagoftricks │   │   ...
Mittenchops's user avatar
  • 19.4k
34 votes
1 answer
82k views

Running a python package [duplicate]

Running Python 2.6.1 on OSX, will deploy to CentOS. Would like to have a package to be invoked from a command line like this: python [-m] tst For that, here is the directory structure made: $...
Vlad Didenko's user avatar
  • 4,661
32 votes
10 answers
10k views

How do YOU manage Perl modules when using a package manager?

A recent question here on SO got me thinking. On most Linux distributions that I tried, some Perl modules would be available through the package manager. Others, of course, not. For quite a while I ...
innaM's user avatar
  • 47.7k
32 votes
6 answers
42k views

How to reference to the top-level module in Python inside a package?

In the below hierachy, is there a convenient and universal way to reference to the top_package using a generic term in all .py file below? I would like to have a consistent way to import other modules,...
hllau's user avatar
  • 10.3k
30 votes
1 answer
102k views

How to pass arguments to main function within Python module?

I have recently started learning more about Python Packages and Modules. I'm currently busy updating my existing modules so that that can be run as script or imported as a module into my other code. I'...
Peter Wilson's user avatar
  • 1,125
28 votes
4 answers
59k views

How To Reduce Python Script Memory Usage

I have a very large python script, 200K, that I would like to use as little memory as possible. It looks something like: # a lot of data structures r = [34, 78, 43, 12, 99] # a lot of functions ...
Coo Jinx's user avatar
  • 339
26 votes
6 answers
25k views

How can I properly use relative or absolute imports in Python modules? [duplicate]

Usage of relative imports in Python has one drawback; you will not be able to run the modules as standalones anymore, because you will get an exception: ValueError: Attempted relative import in non-...
sorin's user avatar
  • 167k
25 votes
4 answers
10k views

How to only import sub module without exec __init__.py in the package

When import sub module from a package, the __init__.py file in the package folder will be exec first, how can I disable this. Sometimes I only need one function in a package, import the whole package ...
HYRY's user avatar
  • 96.5k
22 votes
4 answers
16k views

Python modules with identical names (i.e., reusing standard module names in packages)

Suppose I have a package that contains modules: SWS/ __init.py__ foo.py bar.py time.py and the modules need to refer to functions contained in one another. It seems like I run into problems ...
BFTM's user avatar
  • 3,305
22 votes
1 answer
28k views

What's the difference between module, package and library in Haskell?

What's the difference between module, package and library in Haskell? From http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html Prelude: a standard module imported by default into ...
Matt Elson's user avatar
  • 4,305
22 votes
3 answers
15k views

How to check if a module/library/package is part of the python standard library?

I have installed sooo many libraries/modules/packages with pip and now I cannot differentiate which is native to the python standard library and which is not. This causes problem when my code works on ...
alvas's user avatar
  • 120k
21 votes
2 answers
45k views

How does Python import modules from .egg files?

How can I open __init__.pyc here? >>> import stompservice <module 'stompservice' from 'C:\Python25\lib\site-packages\stompservice-0.1.0-py2.5.egg\stompservice\__init__.pyc'> ...
Alex's user avatar
  • 44.2k
20 votes
2 answers
43k views

python import module from parent package

I have the following directory structure foo/ __init__.py settings.py bar/ __init__.py myfile.py In myfile.py I have: import settings I get the following error: ...
danielrvt's user avatar
  • 10.7k
19 votes
2 answers
8k views

Android Studio Copy Module

I am trying to make a copy of a module in Android Studio, to make small changes. How can I do that? I found several solution but i did not get any to work. Instead of posting my questions in several ...
honiahaka10's user avatar
19 votes
3 answers
11k views

how can I force npm 3 to install nested dependencies?

I just upgraded to npm version 3 and noticed one of the biggest changes it made is that it enforces a flat dependency tree. Your dependencies will now be installed maximally flat. Insofar as is ...
chharvey's user avatar
  • 9,098
19 votes
1 answer
21k views

Go and namespaces: is it possible to achieve something similar to Python?

I wonder if there is a way to use namespaces in the Go language in a similar way as Python does. In Python if I have the following files containing functions: /a.py def foo(): /b.py def bar()...
fstab's user avatar
  • 4,929
18 votes
2 answers
72k views

How do I install python packages with poetry?

Im migrating projects to poetry but have a problem here. This is simple project, not many modules needed. I installed poetry, used poetry add to add few packages required and then ran poetry install ...
M.wol's user avatar
  • 1,221
15 votes
2 answers
35k views

AttributeError: module '' has no attribute '__path__'

I'm having a problem which I have no idea how to further debug. I have a project with different purposes, making use of Python 3 among other things. I created a Python package named package. The ...
Blitzkoder's user avatar
  • 1,798
15 votes
2 answers
554 views

Python: what does "import" prefer - modules or packages?

Suppose in the current directory there is a file named somecode.py, and a directory named somecode which contains an __init__.py file. Now I run some other Python script from this directory which ...
oliver's user avatar
  • 153
14 votes
7 answers
13k views

How can I update all npm packages/modules at once?

I'm struggling to find a way to update all npm packages in one go, some articles suggest that package.json file should be edited where all version numbers need to be changed to * therefore forcing ...
Ilja's user avatar
  • 45.7k
14 votes
2 answers
6k views

What is the difference between "package" and "module" in Kotlin?

In terms of Kotlin, what is the difference between a package and a module? When it comes to visibility modifiers for top-level code, the internal modifier only allows accessibility for code inside ...
Jisip's user avatar
  • 415
14 votes
6 answers
15k views

Error publishing a new version of a package in npm

I'm trying to republish a package using npm. I did update the package version in package.json, yet I get the following error: npm ERR! publish Failed PUT 404 npm ERR! Windows_NT 6.3.9600 npm ERR! ...
Michał Szydłowski's user avatar
14 votes
2 answers
3k views

Why does "import module" and then "from package import module" load the module again?

I have a package in my PYTHONPATH that looks something like this: package/ __init__.py module.py print 'Loading module' If I'm running Python from the package/ directory (or writing ...
Ben Hoyt's user avatar
  • 10.9k
13 votes
4 answers
4k views

How to keep submodule names out of the name space of a Python package?

I want the interface of some module to contain a certain number of functions and classes (and nothing else). I could implement all of those in a single file, and would easily get the interface I want....
Sven Marnach's user avatar

1
2 3 4 5
15