Check sys path python

Check and add the module search path with sys.path in Python

In Python, the module search path is a list of directories that are searched when importing modules and packages using import . This search path is stored in sys.path .

This article describes how to check the current module search paths and add new ones.

See the following article for basic usage of import .

Check the module search path with sys.path

The current module search path is stored in sys.path .

sys.path is a list of strings specifying the module search path. You can view these paths using print() .

In this example, pprint is used to make it easier to read.

import sys import pprint pprint.pprint(sys.path) 

The output should look similar to this:

pwd # /Users/mbp/Documents/my-project/python-snippets/notebook python3 print_sys_path.py # ['/Users/mbp/Documents/my-project/python-snippets/notebook', # '/Users/mbp/Documents/lib', # '/Users/mbp/Documents/my-project/python-snippets/notebook', # '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', # '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7', # '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', # '/usr/local/lib/python3.7/site-packages'] 

Keep in mind that the result will depend on your environment. In the example environment with Python3 installed by Homebrew on a Mac, the following directories are stored in sys.path .

  1. The directory containing the executed script file ( .py )
  2. The directory set by the environment variable PYTHONPATH (more on this later)
  3. The current working directory
  4. Three directories for the standard library
  5. The site-packages directory for third-party libraries installed with pip

If you change your current directory in the terminal and execute the script again, 3. current directory will be updated to this new path.

cd .. pwd # /Users/mbp/Documents/my-project/python-snippets python3 notebook/print_sys_path.py # ['/Users/mbp/Documents/my-project/python-snippets/notebook', # '/Users/mbp/Documents/lib', # '/Users/mbp/Documents/my-project/python-snippets', # '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', # '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7', # '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', # '/usr/local/lib/python3.7/site-packages'] 

Since the module search path changes depending on the current directory, the import may fail depending on where the script file is executed.

Modules are searched in the order listed in sys.path . Thus, modules in the directory of the currently executed script file ( .py ) are searched first. Be aware that if there is a file with the same name as a standard library in the same directory as the executed script file, Python will import that file instead.

Add new module search path with sys.path.append()

Since sys.path is essentially a list, you can easily add new paths. In this example, the append() method is used, but you can also use the insert() method or other list methods.

Читайте также:  Сумма всех элементов одномерного массива питон

After adding a path to sys.path , you can import modules from the added path.

For example, if you want to add a directory one level above the script file, you can do it like this:

import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), '..')) 

Remember, adding to sys.path will only affect the current session. If you want to permanently add paths, you can use PYTHONPATH or .pth files, as explained below.

Add new module search path with PYTHONPATH

You can use the environment variable PYTHONPATH to permanently add a module search path.

For Unix-like OS, including Mac, add the following line to ~/.bashrc , for example. To specify multiple paths, separate them with a colon : .

export PYTHONPATH="/path/to/add:$PYTHONPATH" 

For Windows, you can add PYTHONPATH to your environment variables in the same way you would add any other variable: right-click on your PC (My Computer) -> System -> System Properties -> Environment Variables. In Windows, separate multiple paths with a semicolon ; .

In the above example, the directory ‘/Users/mbp/Documents/lib’ is added to the PYTHONPATH .

Add new module search path with path configuration file ( .pth )

You can also add module search paths by creating a path configuration file ( .pth ) in the site-packages directory.

The path configuration file ( .pth ) should contain one path per line. These paths can be relative or absolute. You can also include comments with # . The file can be named anything as long as the extension is .pth .

Источник

sys.path Python

Изображение баннера

Когда Python получает команду import, сперва он пытается найти нужный модуль в первой директории из списка sys.path, затем во второй и так далее.

Как только находится совпадение поиск заканчивается.

Если у вас несколько модулей с одинаковым названием, найдётся тот, чья директория ближе к началу списка.

Не факт, что это будет нужный. Если кто-то вставит в начало вашего sys.path директорию с кучей интересных модулей, выполняться будут именно они.

ImportError

Поднимается если была попытка импортировать несуществующий модуль

ModuleNotFoundError

Поднимается если была попытка импортировать несуществующий модуль

Traceback (most recent call last): File «», line 1, in ModuleNotFoundError: No module named ‘missing’

Пример

Перейдите в интерактивный режим

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32 Type «help», «copyright», «credits» or «license» for more information. >>>

В Windows результат будет примерно таким (обратные слэши \)

[», ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\lib’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32’, ‘C:\\Users\\Andrei\\AppData\\Roaming\\Python\\Python38\\site-packages’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages’] [», ‘/home/andrei/.pyenv/versions/3.9.5/lib/python39.zip’, ‘/home/andrei/.pyenv/versions/3.9.5/lib/python3.9’, ‘/home/andrei/.pyenv/versions/3.9.5/lib/python3.9/lib-dynload’, ‘/home/andrei/.local/lib/python3.9/site-packages’, ‘/home/andrei/.pyenv/versions/3.9.5/lib/python3.9/site-packages’]

Отдельные части

Так как sys.path это список никто не мешает нам обращаться к его частям или отедльным элементам

Читайте также:  Пространство имен в java
[», ‘C:\\Users\\Andrei\\python\\pluralsight\\packages\\not_searched’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\lib’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32’, ‘C:\\Users\\Andrei\\AppData\\Roaming\\Python\\Python38\\site-packages’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages’] [‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\lib’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32’, ‘C:\\Users\\Andrei\\AppData\\Roaming\\Python\\Python38\\site-packages’, ‘C:\\Users\\Andrei\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages’]

import

Рассмотрим такую конфигурацию

scripts └── my_dir └── path_test.py

Изначально мы внутри директории my_dir

Код path_test.py следующий

print ( «path test is ok» )

Теперь перейдём в директорию scripts

cd .. python >>> import path_test Traceback (most recent call last): File «», line 1, in ModuleNotFoundError: No module named ‘path_test’

Python не смог найти path_test.py

Решение этой проблемы описано в следующем параграфе

sys.path.append

Чтобы избежать этой ошибки можно добавить директорию my_dir в sys.path

>>> import sys >>> sys.path.append(‘my_dir’) >>> import path_test path test is ok!

PYTHONPATH

Ещё один вариант — это задание переменной окружения PYTHONPATH

PYTHONPATH это список путей, который добавляются в sys.path при запуске Python

Способ зависит от окружения, в котором вы работаете

Если нужно добавить несколько директорий, это делается через :

Если у вас уже есть переменная PYTHONPATH и нужно добавить в неё директорию не удаляя старую

Подробнее о системных переменных в Linux можно прочитать в статье Linux PATH

Добавить несколько директорий сразу

Подробнее о системных переменных в Windows можно прочитать в статье Windows PATH

Рассмотрим следующую конфигурацию

andrei └── scripts ├── hidden_dir │ └── pythonpath_test.py └── my_dir └── path_test.py

Изначально мы в директории andrei

export PYTHONPATH=/home/andrei/scripts/hidden_dir python >>> import pythonpath_test

Можно вручную проверить находится ли hidden_dir в sys.path или нет с помощью sys и, например генератора списков

>>> import sys [path for path in sys.path if ‘hidden_dir’ in path] [‘/home/andrei/scripts/hidden_dir’]

Если вы уже находитесь в нужной для Python директории, добавить её в PYTHONPATH можно с помощью команды

Если вы добавляете в PYTHONPATH путь до пакета, нужно добавить путь на один уровень выше, чтобы Python увидел название самого пакета, а не только его содержимое

Источник

Learn Python Sys Module

In this article, we will take a look at the Python Sys Module. There are variables and functions that are maintained by the interpreter and the sys module provides a way of interacting with them. These variables are available until the interpreter is alive. We will have a glance at some of the commonly used sys functions.

To work with the sys module you have to first import the module.

sys.version – This stores the information about the current version of python.

$ python3 >>> import sys >>> sys.version

Show Current Python Version

sys.path – Path variable stores the directory path in the form of a list of strings. Whenever you import a module or run a program using a relative path, python interpreter search for the necessary module or script using the path variable.

Читайте также:  Php запустить от другого пользователя

Path index stores the directory containing the script that was used to invoke the Python interpreter at the index “Zero”. If the interpreter is invoked interactively or if the script is read from standard input, path[0] will be an empty string.

Invoking in Interpreter

When invoking the script the path[0] stores the directory path.

Invoking as Script

If you have modules in a custom directory then you can add the directory path to the path variable using a path.append() method (since the path is a list object we are using the list method “append”).

$ python3 >>> import sys >>> sys.path >>> sys.path.append('/root/test/') >>> sys.path

Python Append Method

sys.argvargv is used to pass run time arguments to your python program. Argv is a list that stores the script name as the 1st value followed by the arguments we pass. Argv values are stored as type string and you have to explicitly convert it according to your needs.

When you run below snippet, the end value of range function is passed via sys.argv[1] as 10 and few other values are also passed to print the list of argv values at the end of the program.

#!/usr/bin/python3 import sys for x in range(1,int(sys.argv[1])): print(x) # Print all the arguments passed print("Arguments passed:",sys.argv)

Passing Arguments in Python

sys.executable – Prints the absolute path of the python interpreter binary.

>>> sys.executable '/usr/bin/python3'

sys.platform – Prints the os platform type. This function will be very useful when you run your program as a platform dependent.

sys.exit – Exit the interpreter by raising SystemExit(status). By default, status is said to be Zero and is said to be successful. We can either use an integer value as Exit Status or other kinds of objects like string(“failed”) as shown in the below example.

Below the sample, a snippet is used to check if the platform is windows and then run the code. If not raise exit() function.

#!/usr/bin/python3 import sys if sys.platform == 'windows': # CHECK ENVIRONMENT #code goes here pass else: print("This script is intended to run only on Windows, Detected platform: ", sys.platform) sys.exit("Failed") 

Check OS Platform

sys.maxsize – This is an integer value representing maximum value a variable can hold.

On a 32-bit platform it is 2**31 - 1 On a 64-bit platform it is 2**63 - 1
Wrapup

We have seen some of the important functions of the sys module and there are a lot more functions. Until we come up with the next article you can read more about the sys module here.

Источник

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