How To Set Python Module Search Path To Find Modules

When you want to import a python module library in your python source code, you need first make the python module library importable by add the module package path in PYTHONPATH system environment variable. You can also add python module package path to python module search path at runtime in python source code. This example will show you how to do it.

1. Add Python Module Package Path In System Environment Variable PYTHONPATH.

Suppose your python module is saved in folder /tmp. We will add /tmp folder in PYTHONPATH environment variable value.

  1. Open a terminal and go to user home directory use cd ~ command.
    $ cd ~
    $ pwd
    /Users/zhaosong
    
  2. Run list -al command to list all hidden files, you can see there is a .bash_profile file ( in MacOS ).
    $ ls -al
    ......
    [email protected]   1 zhaosong  staff       1176 Apr 30 09:15 .bash_profile
    ......
  3. Edit above bash file with vim text editor.
    $ vim .bash_profile
  4. Press shift + i key to go to insert text mode, then add below text line in above bash profile file.
    export PYTHONPATH="/tmp"
  5. Press esc —> :wq! to save the changes.
  6. Run $ source .bash_profile command to make above changes take effect.
  7. Run echo command to print PYTHONPATH system environment variable value.
    $ echo $PYTHONPATH
    /tmp
    

2. Display Python Library Search Path In Python Source Code.

Python sys library’s path variable contains all python library search path, you can loop to print the path value out in python source code like below.

  1. Run into python interactive console in a terminal.
    $ python3
    Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
    [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
  2. Import sys python library.
    import sys
  3. Loop in sys.path to print out each python library search path. We can see that the first directory is current directory ( . ), so when import a python module library, it will search the library in the program executed directory first.
    >>> import sys
    >>> for path in sys.path:
    ...     print(path)
    ... 
    
    /tmp
    /Users/zhaosong/anaconda3/lib/python36.zip
    /Users/zhaosong/anaconda3/lib/python3.6
    /Users/zhaosong/anaconda3/lib/python3.6/lib-dynload
    /Users/zhaosong/.local/lib/python3.6/site-packages
    /Users/zhaosong/anaconda3/lib/python3.6/site-packages
    /Users/zhaosong/anaconda3/lib/python3.6/site-packages/aeosa
    

3. Append Directory To Python Library Search Path.

Python sys.path.append function can append directory to the end of python library search directory.

>>> sys.path.append('/abc')
>>> 
>>> 
>>> for line in sys.path:
...     print(line)
... 

/tmp
/Users/zhaosong/anaconda3/lib/python36.zip
/Users/zhaosong/anaconda3/lib/python3.6
/Users/zhaosong/anaconda3/lib/python3.6/lib-dynload
/Users/zhaosong/.local/lib/python3.6/site-packages
/Users/zhaosong/anaconda3/lib/python3.6/site-packages
/Users/zhaosong/anaconda3/lib/python3.6/site-packages/aeosa
/abc

4. Append Exist Module Library Directory To Python Library Search Directory.

Python module’s __file__ attribute return the module file saved directory. You can append that directory to python library search path as below.

>>> import sys, os
# os.__file__ will return the os module directory.
>>> sys.path.append(os.__file__)
>>> 
>>> 
>>> for line in sys.path:
...     print(line)
... 

/tmp
/Users/zhaosong/anaconda3/lib/python36.zip
/Users/zhaosong/anaconda3/lib/python3.6
/Users/zhaosong/anaconda3/lib/python3.6/lib-dynload
/Users/zhaosong/.local/lib/python3.6/site-packages
/Users/zhaosong/anaconda3/lib/python3.6/site-packages
/Users/zhaosong/anaconda3/lib/python3.6/site-packages/aeosa
/abc
/Users/zhaosong/anaconda3/lib/python3.6/os.py
READ :   How To Use Matplotlib In Python And Ipython To Draw Single Line Plot Example

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.