I have my own package in python and I am using it very often. what is the most elegant or conventional directory where i should put my package so it is going to be imported without playing with PYTHONPATH or sys.path?
What about site-packages for example?
/usr/lib/python2.7/site-packages
.
Is it common in python to copy and paste the package there ?
This is something that works for me (I have to frequently create python packages that are uploaded to a private pip repository). elaborating on the comment by @joran on the question.
python setup.py sdist --format=tar
. the package created should ideally be in thedist
folder.pip install <yourpackage>.tar
you can use
pip install --force-reinstall
if you need to play around with the libraries more and re-create the dist packages.I've found that this method works great for me. If you do not need to package the modules for use of other systems instead of just your local, this method might be an overkill
Happy hacking.
import folders could be extracted by adding following source code:
automatic symlink generation example would be:
instead of "em" you may use other package you've "just installed but the python can't see it"
below I'll explain in more details as being requested in the comment.
suppose you've installed python module em or pyserial with the following command (examples are for ubuntu):
and the output is like this:
the question would be following - python can't see the module pyserial, why? because the location where the module has been installed isn't the one python is looking at for your particular user account.
solution - we have to create symlink from the path where pyserial arrived to the path where your python is looking for.
symlink creation command would be:
instead of typing exact location we are asking pip to tell us where it stored modules by executing command:
instead of typing exact location we are asking python to tell us where it looks for the modules being installed by executing command:
both commands has to be escaped with "`" character (usually on the left of your 1 button for the US keyboards)
in result following command will be provided for ln and the missing symlink would be created:
or something similar, depending on your distro and python/pip defaults.
On my Mac, I did a
sudo find / -name "site-packages"
. That gave me a few paths like/Library/Python/2.6/site-packages
,/Library/Python/2.7/site-packages
, and/opt/X11/lib/python2.6/site-packages
.So, I knew where to put my modules if I was using v2.7 or v2.6.
Hope it helps.
I usually put the stuff i want to have ready to import in the user site directory:
To show the right directory for your platform, you can use
python -m site --user-site
edit: it will show up in
sys.path
once you create it:So if your a novice like myself and your directories are not very well organized you may want to try this method.
Open your python terminal. Import a module that you know works such as numpy in my case and do the following.
Import numpy
numpy.__file__
which results in
'/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages/numpy/__init__.py'
The result of
numpy.__file__
is the location you should put the python file with your module (excluding thenumpy/__init__.py
) so for me that would be/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages
To do this just go to your terminal and type
mv "location of your module" "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site- packages"
Now you should be able to import your module.