⬆️ ⬇️

Using the local package directory in Python now

In Python 3.8, it is proposed to add an alternative to virtual environments — a local directory with PEP 582 packages . Python local packages directory .



This PEP proposes to add to Python the mechanism of automatic detection of the __pypackages__ directory and use it when importing as the source of installed packages. The __pypackages__ directory will have a higher priority when importing than global or user directories with packages. This will prevent the creation, activation or deactivation of virtual environments.



This is how the package structure will look like in Python 3.8 using __pypackages__ :



 foo __pypackages__ 3.8 lib bottle myscript.py 


In this article I will tell you how to use a local directory with packages without waiting for Python 3.8.



The article describes a basic example, tested in Linux, Python 3.5. For other platforms, you may need to make changes.



Installing packages in a local directory



Installation is almost the same as installing packages using pip, with the exception of the additional option --target . In it we indicate the full or relative path to the directory with local packages.



 pip3 install --target="$PWD/__pypackages__/3.5/lib/" bar 


$ PWD is a variable with the current working directory.



The following directory tree will be created:



 foo __pypackages__ 3.5 lib bar myscript.py 


The version of Python and sub-directories must be specified manually.



There may also be problems if you need to install packages with binary code and under different architectures. I did not have such packages, but as a solution, you can add another architecture to the directory structure.



Another installation option

There is another way to install packages in a specific directory:



 pip3 install --ignore-installed --install-option="--prefix=$PWD/__pypackages__" --install-option="--no-compile" bar 


But it is necessary to specify the full path to the installation location and the directory tree will differ from the one proposed in PEP 582:



 foo __pypackages__ lib python3.5 site-packages bar myscript.py 


Using the local package directory



After installing the packages, it remains to tell the interpreter where to look for dependencies.



To do this, add to the sys.path list the path to the local directory with packages. It is enough to add the path to the main (loadable first) module, it is not necessary to add to the rest. You can then import the locally installed packages.



 import os import sys _PATH = '/__pypackages__/3.5/lib/' sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + _PATH) import bar 


The only condition is that the main module must be at the same nesting level as the __pypackages__ directory.



Another way to tell Python where to look for packages is to set the environment variable before running the script.



 PYTHONPATH="$PWD/__pypackages__/3.5/lib/:$PYTHONPATH" python3 ./myscript.py 


This simple way you can achieve similar to the PEP 582 functionality now.



')

Source: https://habr.com/ru/post/444504/



All Articles