Python lib local path

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)

Источник

Читайте также:  Mshta exe microsoft r html приложение

Where does Python look for modules?¶

Let’s say we have written a Python module and saved it as a_module.py , in a directory called code .

We also have a script called a_script.py in a directory called scripts .

We want to be able to import the code in a_module.py to use in a_script.py . So, we want to be able to put his line in a_script.py :

The module and script might look like this:

def func(): print("Running useful function")
import a_module a_module.func()

At the moment, a_script.py will fail with:

$ python3 scripts/a_script.py Traceback (most recent call last): File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module' 

When Python hits the line import a_module , it tries to find a package or a module called a_module . A package is a directory containing modules, but we will only consider modules for now. A module is a file with a matching extension, such as .py . So, Python is looking for a file a_module.py , and not finding it.

You will see the same effect at the interactive Python console, or in IPython:

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

Python looks for modules in “sys.path”¶

Python has a simple algorithm for finding a module with a given name, such as a_module . It looks for a file called a_module.py in the directories listed in the variable sys.path .

>>> import sys >>> type(sys.path) >>> for path in sys.path: . print(path) . /Users/brettmz-admin/dev_trees/psych-214-fall-2016/sphinxext /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload /Users/brettmz-admin/Library/Python/3.7/lib/python/site-packages /Users/brettmz-admin/dev_trees/grin /Users/brettmz-admin/dev_trees/rmdex /usr/local/lib/python3.7/site-packages 

The a_module.py file is in the code directory, and this directory is not in the sys.path list.

Because sys.path is just a Python list, like any other, we can make the import work by appending the code directory to the list.

>>> import sys >>> sys.path.append('code') >>> # Now the import will work >>> import a_module 

There are various ways of making sure a directory is always on the Python sys.path list when you run Python, including:

  • put the directory into the contents of the PYTHONPATH environment variable – Using PYTHONPATH
  • make the module part of an installable package, and install it – see: making a Python package.

As a crude hack, you can also put your code directory on the Python sys.path at the top of the files that need it:

import sys sys.path.append('code') import a_module a_module.func()
$ python3 scripts/a_script_with_hack.py Running useful function 

The simple append above will only work when running the script from a directory containing the code subdirectory. For example:

$ mkdir another_dir $ cd another_dir $ python3 ../scripts/a_script_with_hack.py Traceback (most recent call last): File "../scripts/a_script_with_hack.py", line 4, in import a_module ModuleNotFoundError: No module named 'a_module' 

This is because the directory code that we specified is a relative path, and therefore Python looks for the code directory in the current working directory.

To make the hack work when running the code from any directory, you could use some path manipulation on the The “__file__” variable :

from os.path import dirname, abspath, join import sys # Find code directory relative to our directory THIS_DIR = dirname(__file__) CODE_DIR = abspath(join(THIS_DIR, '..', 'code')) sys.path.append(CODE_DIR) import a_module a_module.func()

Now the module import does work from another_dir :

$ python3 ../scripts/a_script_with_better_hack.py Running useful function 

Источник

Оцените статью