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 to make the python module library importable by adding the module package path in the PYTHONPATH system environment variable. You can also add the python module package path to the 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 the PYTHONPATH environment variable value.

  1. Open a terminal and go to the 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 the above bash file with a vim text editor.
    $ vim .bash_profile
  4. Press shift + i key to go to insert text mode, then add the below text line in the above bash profile file.
    export PYTHONPATH="/tmp"
  5. Press esc —> :wq! to save the changes.
  6. Run $ source .bash_profile command to make the 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 the 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.

  1. 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.

  1. Python module’s __file__ attribute returns the module file saved directory. You can append that directory to the 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

5. Question & Answer.

5.1 How can I add another python source directory in the Python search path for a large python project.

  1. My team just handle an old python project from another team, the python project is so large, there are a lot of python source files in the python project. Our development environment is Linux and no IDE, only in the command line. And when I run the python script with the command python abc.py it prompts the error ImportError: no module named com.test_module. And there is a lot of such kind of errors in other python scripts. All the old python project files are saved in a folder like /codebase/old_python_project. And there are a lot of subfolders in the project base folder. How can I make python search all the modules in the project folder and it’s subfolders to fix the import error? Thanks.
  2. You can add your existing python project folder in the PYTHONPATH system environment variable to fix your issue. You can also use the function sys.path.append(‘/codebase/old_python_project’) in your python script source code and then can import the python modules. If you want to add all the project subfolders in the python module search path, you can reverse loop your project folder and when you reach it’s subfolder then you can call the sys.path.append() function to add the subfolder to the python module search path, you can try it.
    import os, sys 
          
    def reverse_add_python_module_search_path(module_dir):
        
        files_array = []
        
        files_array = os.listdir(module_dir)
        
        for file in files_array:
            
            if os.path.isdir(file):
                
                sys.path.append(file)
                
                reverse_add_python_module_search_path(file)
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x