murloc murloc - 4 months ago 15
Python Question

Global Python package

so I have this package. In the cmd I go to

hp@HP-PC C:\Users\hp\Documents\scripts
:

hp@HP-PC C:\Users\hp\Documents\scripts
> python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import toolzz.printer as t
>>> t.printz()
5


Everything is working fine but I want to have a directory in which I could add my scripts and be able to open my cmd->python->import my package and do whatever I am going to do and not get this instead:

hp@HP-PC C:\Users\hp
> python
Python 3.5.2 |Anaconda 4.1.1 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import toolzz
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'toolzz'
>>>


PS: keep in mind I have added the directory
scripts
to the path and I have global bat files which work

Answer

Run

python -m site

It'll list 2 important pieces of information:

  • The Python module search path, sys.path
  • The location for the USER_SITE directory, and wether or not this exists.

Python looks for modules along those locations. Put your module in a sys.path location (in one that ends in site-packages preferably), or make sure you created the USER_SITE directory and put your code in there.

And you can always extend the path by setting the PYTHONPATH environment variable.