All Questions
3,776
questions
3792
votes
14
answers
2.5m
views
What is __init__.py for?
What is __init__.py for in a Python source directory?
876
votes
10
answers
1.2m
views
Can I force pip to reinstall the current version?
I've come across situations where a current version of a package seems not to be working and requires reinstallation. But pip install -U won't touch a package that is already up-to-date. I see how to ...
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)
842
votes
23
answers
664k
views
How to fix "Attempted relative import in non-package" even with __init__.py
I'm trying to follow PEP 328, with the following directory structure:
pkg/
__init__.py
components/
core.py
__init__.py
tests/
core_test.py
__init__.py
In core_test.py I have ...
717
votes
13
answers
553k
views
How do I remove packages installed with Python's easy_install?
Python's easy_install makes installing new packages extremely convenient. However, as far as I can tell, it doesn't implement the other common features of a dependency manager - listing and removing ...
699
votes
23
answers
2.0m
views
How to install pip with Python 3?
I want to install pip. It should support Python 3, but it requires setuptools, which is available only for Python 2.
How can I install pip with Python 3?
697
votes
4
answers
375k
views
What is a Python egg?
I'm trying to understand how Python packages work. Presumably eggs are some sort of packaging mechanism, but what would be a quick overview of what role they play and may be some information on why ...
595
votes
15
answers
708k
views
beyond top level package error in relative import [duplicate]
It seems there are already quite some questions here about relative import in python 3, but after going through many of them I still didn't find the answer for my issue.
so here is the question.
I ...
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 ...
438
votes
19
answers
1.1m
views
Relative imports - ModuleNotFoundError: No module named x
This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:
test.py
config.py
config.py has a few functions defined in it as ...
426
votes
5
answers
206k
views
Is __init__.py not required for packages in Python 3.3+
I am using Python 3.5.1. I read the document and the package section here: https://docs.python.org/3/tutorial/modules.html#packages
Now, I have the following structure:
/home/wujek/Playground/a/b/...
407
votes
32
answers
1.2m
views
"pip install unroll": "python setup.py egg_info" failed with error code 1
I'm trying to install some packages with pip.
But pip install unroll gives me
Command "python setup.py egg_info" failed with error code 1 in
C:\Users\MARKAN~1\AppData\Local\Temp\pip-build-...
388
votes
12
answers
240k
views
Sibling package imports
I've tried reading through questions about sibling imports and even the
package documentation, but I've yet to find an answer.
With the following structure:
├── LICENSE.md
├── README.md
├── api
│ ├...
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
['...
344
votes
13
answers
855k
views
How do I update a Python package?
I'm running Ubuntu 9:10 and a package called M2Crypto is installed (version is 0.19.1). I need to download, build and install the latest version of the M2Crypto package (0.20.2).
The 0.19.1 package ...
321
votes
10
answers
301k
views
Does uninstalling a package with "pip" also remove the dependent packages?
When you use pip to install a package, all the required packages will also be installed with it (dependencies). Does uninstalling that package also remove the dependent packages?
265
votes
17
answers
297k
views
Upgrade python packages from requirements.txt using pip command
How do I upgrade all my python packages from requirements.txt file using pip command?
tried with below command
$ pip install --upgrade -r requirements.txt
Since, the python packages are suffixed ...
252
votes
7
answers
148k
views
How to read a (static) file from inside a Python package?
Could you tell me how can I read a file that is inside my Python package?
My situation
A package that I load has a number of templates (text files used as strings) that I want to load from within ...
246
votes
3
answers
215k
views
How do I write good/correct package __init__.py files
My package has the following structure:
mobilescouter/
__init__.py #1
mapper/
__init__.py #2
lxml/
__init__.py #3
vehiclemapper.py
...
227
votes
16
answers
949k
views
ModuleNotFoundError: No module named 'sklearn'
I want to import sklearn but there is no module apparently:
ModuleNotFoundError: No module named 'sklearn'
I am using Anaconda and Python 3.6.1; I have checked everywhere but still can't find ...
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 ...
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/...
191
votes
15
answers
432k
views
How to list all installed packages and their versions in Python?
Is there a way in Python to list all installed packages and their versions?
I know I can go inside python/Lib/site-packages and see what files and directories exist, but I find this very awkward. ...
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 ...
181
votes
21
answers
347k
views
Check if Python Package is installed
What's a good way to check if a package is installed while within a Python script? I know it's easy from the interpreter, but I need to do it within a script.
I guess I could check if there's a ...
178
votes
4
answers
86k
views
Does it make sense to use Conda + Poetry?
Does it make sense to use Conda + Poetry for a Machine Learning project? Allow me to share my (novice) understanding and please correct or enlighten me:
As far as I understand, Conda and Poetry have ...
163
votes
5
answers
86k
views
How do I create a namespace package in Python?
In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads. For example, with the ...
157
votes
6
answers
80k
views
Access data in package subdirectory [duplicate]
I am writing a python package with modules that need to open data files in a ./data/ subdirectory. Right now I have the paths to the files hardcoded into my classes and functions. I would like to ...
144
votes
8
answers
565k
views
How to install Python packages from the tar.gz file without using pip install
Long story short my work computer has network constraints which means trying to use pip install in cmd just leads to timing out/not finding package errors.
For example; when I try to pip install ...
142
votes
7
answers
213k
views
How to import classes defined in __init__.py
I am trying to organize some modules for my own use. I have something like this:
lib/
__init__.py
settings.py
foo/
__init__.py
someobject.py
bar/
__init__.py
somethingelse.py
...
138
votes
5
answers
59k
views
Why #egg=foo when pip-installing from git repo
When I do a "pip install -e ..." to install from a git repo, I have to specify #egg=somename or pip complains. For example:
pip install -e git://github.com/hiidef/oauth2app.git#egg=oauth2app
What's ...
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/...
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 ...
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 ...
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
|...
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 ...
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 ...
117
votes
6
answers
52k
views
pip install . creates only the dist-info not the package
I am trying to make a python package which I want to install using pip install . locally. The package name is listed in pip freeze but import <package> results in an error No module named <...
115
votes
3
answers
80k
views
Absolute vs. explicit relative import of Python module
I'm wondering about the preferred way to import packages in a Python application. I have a package structure like this:
project.app1.models
project.app1.views
project.app2.models
project.app1.views ...
108
votes
4
answers
44k
views
Difference between entry_points/console_scripts and scripts in setup.py?
There are basically two ways to install Python console scripts to my path by setup.py:
setup(
...
entry_points = {
'console_scripts': [
'foo = package.module:func',
...
104
votes
4
answers
85k
views
Adding code to __init__.py
I'm taking a look at how the model system in django works and I noticed something that I don't understand.
I know that you create an empty __init__.py file to specify that the current directory is a ...
102
votes
5
answers
70k
views
How include static files to setuptools - python package
I want to include the ./static/data.txt to setuptools, here is my code:
# setup.py
import os,glob
from setuptools import setup,find_packages
setup(
name = "PotatoProject",
version = "0.1.1",
...
100
votes
6
answers
99k
views
How do I list the files inside a python wheel?
I'm poking around the various options to setup.py for including non-python files, and they're somewhat less than intuitive. I'd like to be able to check the package generated by bdist_wheel to see ...
92
votes
12
answers
375k
views
Python: How to pip install opencv2 with specific version 2.4.9?
I know that I could pip install opencv-python which installs opencv3, but is there a separate command or name for opencv specific version such as 2.4.9?
If not, how can I specify which version to ...
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?
87
votes
5
answers
40k
views
How to get the list of options that Python was compiled with?
You can compile Python in various ways. I'd like to find out with which options my Python was compiled.
Concrete use-case: was my Python compiled with readline?
I know I can see this by doing "import ...
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.
...
84
votes
4
answers
26k
views
Check if requirements are up to date
I'm using pip requirements files for keeping my dependency list.
I also try to follow best practices for managing dependencies and provide precise package versions inside the requirements file. For ...
80
votes
7
answers
77k
views
jupyter notebook running kernel in different env
I've gotten myself into some kind of horrible virtualenv mess. Help?!
I manage environments with conda. Until recently, I only had a python2 jupyter notebook kernel, but I decided to drag myself ...
77
votes
9
answers
35k
views
How to upload new versions of project to PyPI with twine?
I've uploaded my Python package to PyPI.
But now I made new version of my package and need to upload it.
I tried to make same progress which I did when upload the package first time but I got the ...