Linux python lib path

Lee On Coding

My blog about coding and stuff.

How does python find packages?

I just ran into a situation where I compiled and installed Python 2.7.9 from source on Ubuntu, but Python could not find the packages I had previously installed. This naturally raises the question — how does Python know where to find packages when you call import ? This post applies specifically to Python 2.7.9, but I’m guessing Python 3x works very similarly.

In this post I first describe how Python finds packages, and then I’ll finish with the discovery I made regarding the default Python that ships with Ubuntu and how it differs from vanilla Python in how it finds packages.

sys.path

Python imports work by searching the directories listed in sys.path .

Using my default Ubuntu 14.04 Python:

So Python will find any packages that have been installed to those locations.

How sys.path gets populated

As the docs explain, sys.path is populated using the current working directory, followed by directories listed in your PYTHONPATH environment variable, followed by installation-dependent default paths, which are controlled by the site module.

You can read more about sys.path in the Python docs.

Assuming your PYTHONPATH environment variable is not set, sys.path will consist of the current working directory plus any manipulations made to it by the site module.

The site module is automatically imported when you start Python, you can read more about how it manipulates your sys.path in the Python docs.

It’s a bit involved.

You can manipulate sys.path

You can manipulate sys.path during a Python session and this will change how Python finds modules. For example:

The module __file__ attribute

When you import a module, you usually can check the __file__ attribute of the module to see where the module is in your filesystem:

However, the Python docs state that:

The file attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

So, for example this doesn’t work:

It makes sense that the sys module is statically linked to the interpreter — it is essentially part of the interpreter!

The imp module

Python exposes the entire import system through the imp module. That’s pretty cool that all of this stuff is exposed for us to abuse, if we wanted to.

imp.find_module can be used to find a module:

You can also import and arbitrary Python source as a module using imp.load_source . This is the same example before, except imports our module using imp instead of by manipulating sys.path :

Passing ‘hi’ to imp.load_source simply sets the __name__ attribute of the module.

Ubuntu Python

Now back to the issue of missing packages after installing a new version of Python compiled from source. By comparing the sys.path from both the Ubuntu Python, which resides at /usr/bin/python , and the newly installed Python, which resides at /usr/local/bin/python , I could sort things out:

Ubuntu Python ( /usr/bin/python ):

Python compiled from source ( /usr/local/bin/python )

Turns out what mattered for me was dist-packages vs. site-packages . Using Ubuntu’s Python, my packages were installed to /usr/local/lib/python2.7/dist-packages , whereas the new Python I installed expects packages to be installed to /usr/local/lib/python2.7/site-packages . I just had to manipulate the PYTHONPATH environment variable to point to dist-packages in order to gain access to the previously installed packaged with the newly installed version of Python.

How did Ubuntu manipulate the sys.path ?

So how does the Ubuntu distribution of Python know to use /usr/local/lib/python2.7/dist-packages in sys.path ? It’s hardcoded into their site module! First, find where the site module code lives:

Here is an excerpt from Ubuntu Python’s site.py , which I peeked by opening /usr/lib/python2.7/site.py in a text editor. First, a comment at the top:

For Debian and derivatives, this sys.path is augmented with directories for packages distributed within the distribution. Local addons go into /usr/local/lib/python /dist-packages, Debian addons install into /usr//python /dist-packages. /usr/lib/python /site-packages is not used.

OK so there you have it. They explain how the Debian distribution of Python is different.

And now, for the code that implementes this change:

It’s all there, if you are crazy enough to dig this deep.

© Lee Mendelowitz – Built with Pure Theme for Pelican

Источник

Python import, sys.path, and PYTHONPATH Tutorial

Introduction

The import statement is usually the first thing you see at the top of any Python file. We use it all the time, yet it is still a bit mysterious to many people. This tutorial will walk through how import works and how to view and modify the directories used for importing.

If you want to learn how to import a module by using a string variable name to reference the module, check out my tutorial on Import Python Module by String Name

Also check out my Python Virtual Environments Tutorial to learn more about isolated Python environments.

Modules versus packages

First, let’s clarify the difference between modules and packages. They are very closely related, and often confused. They both serve the same purpose which is to organize code, but they each provide slightly different ways of doing that.

  • A module is a single .py file with Python code.
  • A package is a directory that can contains multiple Python modules.

A module can be thought of as a self-contained package, and a package is like a module that is separated out across multiple files. It really depends on how you want to organize your code and how large your project is. I always start with a module and turn it in to a package if needed later.

How import works

The import keyword in Python is used to load other Python source code files in to the current interpreter session. This is how you re-use code and share it among multiple files or different projects.

There are a few different ways to use import . For example, if we wanted to use the function join() that lives in the path module of the os package. Its full name would be os.path.join() . We have a few ways of importing and using the function. Read more about the os package at https://docs.python.org/3/library/os.path.html.

Import versus from

There are a few different ways you can import a package or a module. You can directly call import or use from x import y format. The from keyword tells Python what package or module to look in for the name specified with import . Here are a few example that all accomplish the same end goal.

Different ways to import and execute os.path.join() :

As you can see, you can import the whole package, a specific module within a package, a specific function from within a module. The * wildcard means load all modules and functions. I do not recommend using the wildcard because it is too ambiguous. It is better to explicitly list each import so you can identify where it came from. A good IDE like PyCharm will help you manage these easily.

When you call import in the Python interpreter searches through a set of directories for the name provided. The list of directories that it searches is stored in sys.path and can be modified during run-time. To modify the paths before starting Python, you can modify the PYTHONPATH environment variable. Both sys.path and PYTHONPATH are covered more below.

Import by string

If you want to import a module programmatically, you can use importlib.import_module() . This function is useful if you are creating a plugin system where modules need to be loaded at run-time based on string names.

This method is not commonly used, and is only useful in special circumstances. For example, if you are building a plugin system where you want to load every file in a directory as a module based on the filepath string.

How __init__ and __main__ work

Names that start and end with double underscores, often called ‘dunders’, are special names in Python. Two of them are special names related to modules and packages: __init__ and __main__ . Depending on whether you are organizing your code as a package or a module, they are treated slightly differently.

We will look at the difference between a module and a package in a moment, but the main idea is this:

  • When you import a package it runs the __init__.py file inside the package directory.
  • When you execute a package (e.g. python -m my_package ) it executes the __main__.py file.
  • When you import a module it runs the entire file from top to bottom.
  • When you execute a module ir runs the entire file from top-to-bottom and sets the __name__ variable to the string «__main__» .

In a package

In a Python package (a directory), you can have a module named __init__.py and another named __main__.py .

Here is an example structure of a package:

If a package is invoked directly (e.g. python -m my_package ), the __main__.py module is executed. The __init__.py file is executed when a package is imported (e.g. import my_package ).

In a module

In the previous section, we saw how a package can have separate files for __init__.py and __main__.py . In a module (a single .py file) the equivalent of __init__ and __main__ are contained in the single file. The entire itself essentially becomes both the __init__.py and the __main__.py .

When a module is imported, it runs the whole file, loading any functions defined.

When a module is invoked directly, for example, python my_module.py or python -m my_module , then it does the same thing as importing it, but also sets the __name__ variable to the string «__main__» .

You can take advantage of this and execute a section of code only if the module is invoked directly, and not when it is imported. To do this, you need to explicitly check the __name__ variable, and see if it equals __main__ . If it is set to the string __main__ , then you know the module was invoked directly, and not simply imported.

Take this example. Create a file named my_module.py with the following contents:

Try out a few different things to understand how it works:

  • Run the file directly with Python: python my_module.py
  • Invoke the module with -m flag: python -m my_module
  • Import the module from another Python file: python -c «import my_module»
  • Import and call the function defined: python -c «import my_module; my_module.my_function()»

Manage import paths

sys.path

When you start a Python interpreter, one of the things it creates automatically is a list that contains all of directories it will use to search for modules when importing. This list is available in a variable named sys.path . Here is an example of printing out sys.path . Note that the empty » entry means the current directory.

You are allowed to modify sys.path during run-time. Just be sure to modify it before you call import . It will search the directories in order stopping at the first place it finds the specified modules.

PYTHONPATH

PYTHONPATH is related to sys.path very closely. PYTHONPATH is an environment variable that you set before running the Python interpreter. PYTHONPATH , if it exists, should contain directories that should be searched for modules when using import . If PYTHONPATH is set, Python will include the directories in sys.path for searching. Use a semicolon to separate multiple directories.

Here is an example of setting the environment variable in Windows and listing the paths in Python:

And in Linux and Mac you can do the equivalent like this:

So, in order to import modules or packages, they need to reside in one of the paths listed in sys.path . You can modify the sys.path list manually if needed from within Python. It is just a regular list so it can be modified in all the normal ways. For example, you can append to the end of the list using sys.path.append() or to insert in an arbitrary position using sys.path.insert() . For more help, refer to https://docs.python.org/3/tutorial/datastructures.html

The site module

You can also use the site module to modify sys.path . See more at https://docs.python.org/3/library/site.html.

You can also direclty invoke the site module to get a list of default paths:

PYTHONHOME

The PYTHONHOME environment variable is similar to PYTHONPATH except it should define where the standard libraries are. If PYTHONHOME is set, it will assume some default paths relative to the home, which can be supplemented with PYTHONPATH .

This is particularly relevant if you embedded Python in to a C application and it is trying to determine the path of Python using the PYTHONHOME environment variable.

Just for reference, here is a quick example of how you would build a C application with Python embedded in it.

Источник

Урок 3. Установка Python на Windows, Linux, Mac OS

Установка Python доступна на самых разных платформах, включая Linux и Mac OS X. Давайте разберемся, как установить среду Python.

Содержание

Установка локальной среды

Откройте окно терминала и введите «python», чтобы узнать, установлен ли он уже и если да, то какая версия установлена.

  • Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX и т. д.)
  • Windows 9x/NT/2000
  • Macintosh (Intel, PPC, 68K)
  • OS/2
  • DOS (несколько версий)
  • PalmOS
  • Мобильные телефоны Nokia
  • Windows CE
  • Acorn/RISC OS
  • BeOS
  • Amiga
  • VMS/OpenVMS
  • QNX
  • VxWorks
  • Psion
  • Python также был перенесен на виртуальные машины Java и .NET.

Загрузка Python

Самый последний и актуальный исходный код, двоичные файлы, документация, новости и т. д. доступны на официальном сайте Python https://www.python.org/.

Вы можете загрузить документацию Python, перейдя по адресу https://www.python.org/doc/. Документация доступна в форматах HTML, PDF и PostScript.

Установка Python

Дистрибутив Python доступен для самых разных платформ. Вам необходимо загрузить только двоичный код, подходящий для вашей платформы, и установить Python.

Если двоичный код для вашей платформы недоступен, вам понадобится компилятор C для компиляции исходного кода вручную. Компиляция исходного кода обеспечивает большую гибкость с точки зрения выбора функций, необходимых для вашей установки.

Ниже приведен краткая инструкция по установке Python на различных платформах.

Установка версии для Unix и Linux

Выполните следующие шаги по установке Python на устройстве Unix/Linux.

  • Откройте веб-браузер и перейдите по адресу https://www.python.org/downloads/
  • Перейдите по ссылке, чтобы загрузить заархивированный исходный код, доступный для Unix/Linux.
  • Загрузите и распакуйте файлы.
  • Отредактируйте файл Modules/Setup, если вы хотите настроить некоторые параметры.
  • Запустите скрипт ./configure
  • Выполните
  • Выполните установку Python

Python установится в стандартную директорию /usr/local/bin, а его библиотеки в /usr/local/lib/pythonXX, где XX — это версия Python.

Установка Python на Windows

Выполните следующие шаги по установке Python на ОС Windows.

  • Откройте веб-браузер и перейдите по адресу https://www.python.org/downloads/
  • Перейдите по ссылке на файл python-XYZ.msi установщика Windows, где XYZ — это версия, которую необходимо установить.
  • Чтобы использовать этот установщик python-XYZ.msi, система Windows должна поддерживать Microsoft Installer 2.0. Сохраните файл установщика на компьютере, а затем запустите его, чтобы узнать, поддерживает ли ваш компьютер MSI.
  • Запустите скачанный файл, после чего откроется мастер установки и настройки Python, который делает процесс установки максимально простым. Просто примите настройки по умолчанию и дождитесь завершения установки.

Установка версии для Macintosh

Последние Mac поставляются с установленным Python, но его версия может быть устаревшей. Смотрите инструкции по получению текущей версии вместе с дополнительными инструментами для поддержки разработки на Mac на странице http://www.python.org/download/mac/. Для версий Mac OS до Mac OS X 10.3 (выпущенных в 2003 году) доступен MacPython.

Он поддерживается Джеком Янсеном, и вы можете получить полный доступ ко всей документации на его веб-сайте — http://www.cwi.nl/

jack/macpython.html. Также там вы можете получить полную информацию об установке версии для Mac OS.

Настройка PATH

Программы и другие исполняемые файлы могут находиться во многих каталогах, поэтому операционные системы предоставляют путь поиска, в котором перечислены каталоги, в которых ОС ищет исполняемые файлы.

Путь хранится в переменной среде, которая представляет собой именованную строку, поддержива емую операционной системой. Эта переменная содержит информацию, доступную для командной оболочки и других программ.

Переменная пути называется PATH в Unix или Path в Windows (Unix чувствителен к регистру; Windows — нет).

В Mac OS установщик обрабатывает сведения о пути. Чтобы вызвать интерпретатор Python из любого конкретного каталога, вы должны добавить каталог Python в свой путь.

Настройка пути в Unix/Linux

Чтобы добавить директорию Python к пути для определенного сеанса в Unix:

  • В командной оболочке csh введите setenv PATH «$PATH:/usr/local/bin/python» и нажмите Enter.
  • В командной оболочке bash (Linux) введите export PATH=»$PATH:/usr/local/bin/python» и нажмите Enter.
  • В командной оболочке sh или ksh введите PATH=»$PATH:/usr/local/bin/python» и нажмите Enter.
  • Примечание: /usr/local/bin/python — это путь к каталогу Python.

Настройка пути в Windows

Чтобы добавить каталог Python к пути для определенного сеанса в Windows:

В командной строке введите path %path%;C:\Python и нажмите Enter.

Примечание: C:\Python — это путь к каталогу Python.

Переменные среды Python

В таблице приведены важные переменные среды, которые может распознавать Python:

№ п/п Переменная и описание
1 PYTHONPATH
Роль данной переменной аналогична PATH. Эта переменная сообщает интерпретатору Python, где найти файлы модуля, импортированные в программу. Переменная должна включать каталог исходной библиотеки Python и каталоги, содержащие исходный код Python. PYTHONPATH иногда задается установщиком Python.
2 PYTHONSTARTUP
Содержит путь к файлу инициализации, содержащему исходный код Python. Выполняется каждый раз при запуске интерпретатора. В Unix называется .pythonrc.py и содержит команды, которые загружают утилиты или изменяют PYTHONPATH.
3 PYTHONCASEOK
Используется в Windows, чтобы указать Python найти первое совпадение без учета регистра в операторе импорта. Задайте для этой переменной любое значение, чтобы активировать ее.
4 PYTHONHOME
Это альтернативный путь поиска модуля. Обычно встраивается в каталоги PYTHONSTARTUP или PYTHONPATH для упрощения переключения библиотек модулей.

Запуск Python

Есть три разных способа запуска Python.

Интерактивный интерпретатор

Вы можете запустить Python из Unix, DOS или любой другой системы, которая предоставляет вам интерпретатор командной строки или командное окно.

Введите python в командной строке.

Начните писать код прямо в интерактивном интерпретаторе.

В таблице приведен список всех доступных параметров командной строки:

№ п/п Опции и описание
1 -d
Обеспечивает вывод отладки.
2 -O
Генерирует оптимизированный байт-код (в результате создаются файлы .pyo).
3 -S
Не запускает импорт местоположения для поиска путей Python при запуске.
4 -v
Подробный вывод (подробная трассировка операторов импорта).
5 -X
Отключает встроенные исключения на основе классов (используйте только строки); устарело, начиная с версии 1.6.
6 -c cmd
Запускает скрипт Python, отправленный в качестве строки cmd
7 file
Запускает скрипт Python из заданного файла

Скрипт из командной строки

Сценарий Python может быть выполнен из командной строки, вызвав интерпретатор в вашем приложении, как показано ниже:

Примечание: убедитесь, что режим разрешений для файла позволяет выполнение.

Интегрированная среда разработки

Вы также можете запустить Python из среды графического интерфейса пользователя (GUI), если в вашей системе установлено приложение с графическим интерфейсом, которое поддерживает Python.

  • Unix — IDLE является первой Unix IDE для Python.
  • Windows — PythonWin является первым интерфейсом Windows для Python, представляющий собой IDE с графическим интерфейсом.
  • Macintosh — версия Python для Macintosh вместе с IDLE IDE доступна с основного веб-сайта и может быть загружена в виде файлов MacBinary или BinHex’d.

Если вы не можете правильно настроить среду, обратитесь за помощью к системному администратору. Убедитесь, что среда Python правильно настроена и работает нормально.

Примечание: все примеры, приведенные в последующих главах, выполняются с версией Python 2.4.3, доступной в версии CentOS Linux.

Мы провели настройку окружения онлайн-среды программирования Python, так что вы можете выполнять все доступные примеры онлайн одновременно, когда изучаете теорию. Не стесняйтесь изменять любой пример и выполнять его онлайн.

Источник

Читайте также:  Cpu control не видит процессы windows 10
Оцените статью