Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put my own python module so that it can be imported

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 ?

like image 358
Cobry Avatar asked Apr 24 '13 15:04

Cobry


People also ask

Where should I store my Python modules?

For the most part, modules are just Python scripts that are stored in your Lib or Lib/site-packages folder, or local to the script being run. That's it.

Where do I put Python imports?

Imports are always put at the top of the file, just after any module comments and docstrings, and before module globals and constants.

Where does Python look for modules to import?

Python looks for modules in “sys. It looks for a file called a_module.py in the directories listed in the variable sys.


1 Answers

I usually put the stuff i want to have ready to import in the user site directory:

~/.local/lib/pythonX.X/site-packages 

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:

mkdir -p "`python -m site --user-site`" 
like image 107
mata Avatar answered Oct 30 '22 22:10

mata