Adding path to sys path python

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)

Источник

Читайте также:  Javascript get all parents element

How to Use Sys.path.append() in Python

Sys.path is a list of directories where the Python interpreter searches for modules. Mind you, this is a list! When a module is claimed in a project file, it will search through each one of the directories in the list. If the module is located within one of those directories, then everything goes fine and your project is successfully rendered. However, if the module is not located within any of the listed directories, then your project will fail lest you can “append” the directory where your module is located to the list using the append() function. In this tutorial, we’ll be learning about how to use sys.path.append() in Python.

List Current Path

By declaring the following, we can check the current Python path:

from pprint import pprint

Here, the pprint module is just used to make things look pretty, so you can ignore that part, and concentrate on the rest. All we’re trying to do here is to print out sys.path.

What you notice here is that first of all, it’s a list. So according to the list, it will first look in the current working directory, then in other directories one by one. The current working directory is the directory in which the main python script – the one being executed – is found. Further, what this also means is that this list can be modified or edited.

Adding a Path

The sys.path.append() method is used specifically to add a Path to the existing ones.

Suppose now that I have two distinct files: main.py, and file1.py. Main.py is the main file, and we’re going to try to import file1.py (a module that we wrote).

Because file1.py or our module is within the working directory, you should be able to successfully print out the secret sentence. This is so because when the Python interpreter searches for the requested module, it will search in the working directory first and because our module called file1 is within the working directory, it will automatically print out our secret sentence.

Now, suppose that I create a directory within my working directory called “directory1” and place my module called file1 within it.

Now, within the module file1 is the secret phrase that we’re trying to print out. In this case, it will not print out the secret phrase. Why? Because the interpreter isn’t looking in “directory1” for any modules. In other words, the current working directory is a member of the list of sys.path but “directory1” is not a member of that list. So, the resulting error is a ModuleNotFound Error – it couldn’t find or locate the module called file1.

In order to avert this problem, you need to tell the Python interpreter where to look for the module file1. This is where the sys.path.append() method comes in.

The sys.path.append() method is used to append the path that we want to the existing list.

On a Windows system, you’d write:

Please note that you cannot add any given file to sys.path, instead you may add directories, and then import the files you want.

Example #1

So, let’s try the previous operation (where the file1 module is located within directory1) again but this time, we’ll append the path to the file1 module to sys.path using the sys.path.append() method.

sys . path . append ( ‘C:\\Users\\never\\PycharmProjects\\

When we append the path using the sys.path.append() method, we first append the location of the module to Path, and then import it. If done in such a manner, the Python interpreter should be able to locate the requested module, and therefore retrieve the secret variable.

The output of the previous operation is as follows:

Example #2

Let’s take another example, this time on a Linux machine. Let’s suppose that we have one file – main.py.

from pprint import pprint

Here, sys.path[0] is “/home/kalyani” because that is where main.py is located.

Now, let’s modify the script a little bit by using the sys.path.append() method. Let’s use the method to append a particular path (‘/home/kalyani/directory1’) to sys.path.

from pprint import pprint

sys . path . append ( ‘/home/kalyani/directory1’ )

So, the sys.path.append() method has appended the path to the end of the existing list! Here, please also note that the appending is done at the end of the list not at the beginning.

PYTHONPATH

There is one tiny little problem with what we just did and that is that we didn’t permanently put that location onto the sys.path list. The sys.path.append() method is used to temporarily add a path and as such, that path would be valid for a session for example. If you’d like to permanently alter the sys.path list, then we can use PYTHONPATH to add the path to the list. Since sys.path also looks into PYTHONPATH, the locations listed will then be included in the list of paths.

  1. On your Linux machine, open up a terminal, and navigate to the folder containing .bashrc.
  2. nano .bashrc
  3. Add the following at the end of the .bashrc file:

Here, you can add the path that you want.

  1. Close the terminal, and open up a new terminal
  2. Now, try executing your program. In my case, it will be the following:

from pprint import pprint

When writing complex programs in Python, we will eventually add third party modules or even better, create our own modules. In all cases, we cannot keep every single Python file we create in one directory, we may wish to use multiple directories to adequately classify our files. The problem however is that we may need to import our created modules left and right. In such cases, if we want to prevent a ModuleNotFound Error, then we can add the location of the directory – and mind you, it has to be a directory, not a file – to sys.path using the sys.path.append() method.

Источник

Python sys.path.append() Method

The sys.path.append() method takes a file path string as an argument.

Example 1: How to Use sys.path.append() Method

import sys print(sys.path) sys.path.append("/Users/krunal/Desktop/code/pyt/database") print("-----------------------") print("After appending a path") print("-----------------------") # printing all paths print(sys.path)
['/Users/krunal/Desktop/code/pyt/database', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Users/krunal/Library/Python/3.9/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages'] ----------------------- After appending a path ----------------------- ['/Users/krunal/Desktop/code/pyt/database', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Users/krunal/Library/Python/3.9/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages', '/Users/krunal/Desktop/code/pyt/database']

The sys.path attribute always contains a listing of default paths.

Using the sys.path.append() method, and we added a new path to the default paths, as you can see in the last line of this output.

Example 2: Adding an absolute path

import sys sys.path.append('/home/user/path/to/your/module') 

Источник

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