Python set path to modules

How to add a new path to your PYTHONPATH to import your own python modules or packages ?

Examples of how to add a new path to your PYTHONPATH to import your own python modules or packages:

Introduction

For example, I have on my local computer a reperestory called «github_projects» (located in the following path «/Users/John/github_projects») where I stored all my own python modules that I develop:

github_projects/ project_01/ project_02/ project_03/ . . . 

However, if I try to import a python module from another reperestory on my computer I will get the following error message:

>>> import project_01 Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'project_01' 

We get this error message here because python doesn't know where to find the python module project_01. A simple solution to check that is to look at sys.path:

returns for example in my case:

['', '/Users/John/anaconda3/lib/python36.zip', '/Users/John/anaconda3/lib/python3.6', '/Users/John/anaconda3/lib/python3.6/lib-dynload', '/Users/John/anaconda3/lib/python3.6/site-packages', '/Users/John/anaconda3/lib/python3.6/site-packages/aeosa'] 

can also check your PYTHONPATH using os:

>>> import os >>> os.environ['PYTHONPATH'] 

Another solution to check your PYTHONPATH is to enter directly

in your terminal (not in your python interpreter!).

Adding a new path to your PYTHONPATH

To add a new path to your PYTHONPATH it is going to depend on the your shell (I used hereafter bash shell ). To get your shell just enter

To temporary add a new path in your PYTHONPATH:

export PYTHONPATH="/Users/John/github_projects" 

and you can now start python (in the same window that you entered "export PYTHONPATH="/Users/John/github_projects") and try to import your module:

Another solution to make that more permanently just open the

file and add the following line:

export PYTHONPATH="/Users/John/github_projects" 

It will then add automatically "/Users/John/github_projects" to your PYTHONPATH each time you open a new terminal window.

Reloading your own python module

Note: another interessting tool is to be able to reload your python (> 3.4) module:

import importlib importlib.reload(module) 

So you don't need to restart python each time you make some change in your python module for example "project_01". You just need to reload it:

import importlib importlib.reload(project_01) 

References

Benjamin

Greetings, I am Ben! I completed my PhD in Atmospheric Science from the University of Lille, France. Subsequently, for 12 years I was employed at NASA as a Research Scientist focusing on Earth remote sensing. Presently, I work with NOAA concentrating on satellite-based Active Fire detection. Python, Machine Learning and Open Science are special areas of interest to me.

Skills

Источник

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.

    Open a terminal and go to the user home directory use cd ~ command.

$ ls -al . -rw-r--r--@ 1 zhaosong staff 1176 Apr 30 09:15 .bash_profile .

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

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

Источник

Читайте также:  Публикация в facebook php
Оцените статью