How to activate venv python

How to activate venv python

12. Virtual Environments and PackagesВ¶

12.1. IntroductionВ¶

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.

12.2. Creating Virtual EnvironmentsВ¶

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.

Once you’ve created a virtual environment, you may activate it.

On Unix or MacOS, run:

(This script is written for the bash shell. If you use the csh or fish shells, there are alternate activate.csh and activate.fish scripts you should use instead.)

Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running python will get you that particular version and installation of Python. For example:

12.3. Managing Packages with pipВ¶

You can install the latest version of a package by specifying a package’s name:

You can also install a specific version of a package by giving the package name followed by == and the version number:

pip uninstall followed by one or more package names will remove the packages from the virtual environment.

pip show will display information about a particular package:

pip list will display all of the packages installed in the virtual environment:

pip freeze will produce a similar list of the installed packages, but the output uses the format that pip install expects. A common convention is to put this list in a requirements.txt file:

Виртуальное окружение Python (venv)

Location — путь до ваших глобальных пакетов.

В большинстве случаев, устанавливать пакеты глобально — плохая идея 🙅‍♂️ Почему? Рассмотрим простой пример:

Решение данной проблемы — создание виртуального окружения (virtual environment).

Основная цель виртуального окружения Python — создание изолированной среды для python-проектов

Это означает, что каждый проект может иметь свои собственные зависимости, независимо от других проектов.

Настройка виртуального окружения

Устанавливать venv не нужно — он входит в стандартную библиотеку Python

Создание

Для создания виртуального окружения, перейдите в директорию своего проекта и выполните:

В результате будет создан каталог venv/ содержащий копию интерпретатора Python, стандартную библиотеку и другие вспомогательные файлы.

Новые пакеты будут устанавливаться в venv/lib/python3.x/site-packages/

Активация

Чтобы начать пользоваться виртуальным окружением, необходимо его активировать:

source выполняет bash-скрипт без запуска дополнительного bash-процесса.

Проверить успешность активации можно по приглашению оболочки. Она будет выглядеть так:

Также новый путь до библиотек можно увидеть выполнив команду:

Интересный факт: в виртуальном окружении вместо команды python3 и pip3, можно использовать python и pip

Автоматическая активация

В некоторых случаях, процесс активации виртуального окружения может показаться неудобным (про него можно банально забыть 🤷‍♀️).

На практике, для автоматической активации перед запуском скрипта, создают скрипт-обертку на bash :

Теперь можно установить права на исполнение и запустить нашу обертку:

Деактивация

Закончив работу в виртуальной среде, вы можете отключить ее, выполнив консольную команду:

Альтернативы venv

На данный момент существует несколько альтернатив для venv:

Стоит ли использовать виртуальное окружение в своей работе — однозначно да. Это мощный и удобный инструмент изоляции проектов друг от друга и от системы. С помощью виртуального окружения можно использовать даже разные версии Python!

How to Set Up a Virtual Environment in Python – And Why It’s Useful

How to activate venv python. Смотреть фото How to activate venv python. Смотреть картинку How to activate venv python. Картинка про How to activate venv python. Фото How to activate venv python

This is a common approach for a lot of beginners and many people transitioning from working with Python for data analytics.

This works fine for simple Python scripting projects. But in complex software development projects, like building a Python library, an API, or software development kit, often you will be working with multiple files, multiple packages, and dependencies. As a result, you will need to isolate your Python development environment for that particular project.

Consider this scenario: you are working on app A, using your system installed Python and you pip install packageX version 1.0 to your global Python library. Then you switch to project B on your local machine, and you install the same packageX but version 2.0, which has some breaking changes between version 1.0 and 2.0.

When you go back to run your app A, you get all sorts of errors, and your app does not run. This is a scenario you can run into when building software with Python. And to get around this, we can use virtual environments.

This tutorial will cover everything you need to know about virtual environments and how to set one up with Virtualenv.

What is a Virtual Environment?

Python’s official documentation says:

«A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system»

To break this down, when you activate a virtual environment for your project, your project becomes its own self contained application, independent of the system installed Python and its modules.

Your new virtual environment has its own pip to install libraries, its own libraries folder, where new libraries are added, and its own Python interpreter for the Python version you used to activate the environment.

With this new environment, your application becomes self-contained and you get some benefits such as:

Using virtual environments is recommended for software development projects that generally grow out of a single Python script, and Python provides multiple ways of creating and using a virtual environment.

In the sections below, we will walk through how to set up your virtual environment, using venv, which gives you a lot more low level control of your environment.

Another common way to set up your virtual environment is to use pipenv, which is a more high level approach.

How to Install a Virtual Environment using Venv

Virtualenv is a tool to set up your Python environments. Since Python 3.3, a subset of it has been integrated into the standard library under the venv module. You can install venv to your host Python by running this command in your terminal:

To use venv in your project, in your terminal, create a new project folder, cd to the project folder in your terminal, and run the following command:

When you check the new projectA folder, you will notice that a new folder called env has been created. env is the name of our virtual environment, but it can be named anything you want.

If we check the contents of env for a bit, on a Mac you will see a bin folder. You will also see scripts that are typically used to control your virtual environment, such as activate and pip to install libraries, and the Python interpreter for the Python version you installed, and so on. (This folder will be called Scripts on windows).

The lib folder will contain a list of libraries that you have installed. If you take a look at it, you will see a list of the libraries that come by default with the virtual environment.

How to Activate the Virtual Environment

Now that you have created the virtual environment, you will need to activate it before you can use it in your project. On a mac, to activate your virtual environment, run the code below:

This will activate your virtual environment. Immediately, you will notice that your terminal path includes env, signifying an activated virtual environment.

Note that to activate your virtual environment on Widows, you will need to run the following code below (See this link to fully understand the differences between platforms):

Is the Virtual Environment Working?

We have activated our virtual environment, now how do we confirm that our project is in fact isolated from our host Python? We can do a couple of things.

First we check the list of packages installed in our virtual environment by running the code below in the activated virtual environment. You will notice only two packages – pip and setuptools, which are the base packages that come default with a new virtual environment

Next you can run the same code above in a new terminal in which you haven’t activated the virtual environment. You will notice a lot more libraries in your host Python that you may have installed in the past. These libraries are not part of your Python virtual environment until you install them.

How to Install Libraries in a Virtual Environment

To install new libraries, you can easily just pip install the libraries. The virtual environment will make use of its own pip, so you don’t need to use pip3.

After installing your required libraries, you can view all installed libraries by using pip list, or you can generate a text file listing all your project dependencies by running the code below:

You can name this requirements.txt file whatever you want.

Requirements File

Why is a requirements file important to your project? Consider that you package your project in a zip file (without the env folder) and you share with your developer friend.

To recreate your development environment, your friend will just need to follow the above steps to activate a new virtual environment.

Instead of having to install each dependency one by one, they could just run the code below to install all your dependencies within their own copy of the project:

Note that it is generally not advisable to share your env folder, and it should be easily replicated in any new environment.

How to Deactivate a Virtual Environment

To deactivate your virtual environment, simply run the following code in the terminal:

Conclusion

Python virtual environments give you the ability to isolate your Python development projects from your system installed Python and other Python environments. This gives you full control of your project and makes it easily reproducible.

Питон в коробке – venv в python 3.3

Наверняка, большинство из тех, кто разрабатывает или деплоит Python приложения, использует виртуальные окружения. В частности через virtualenv, написанный Ian Bicking.

Идея оказалась так хороша и распространена, что нечто похожее теперь присутствует в Python 3.3 из коробки в виде модуля venv. Он почти такой же, как virtualenv, только немного лучше.

Как это работает?

Вот и вся суть venv, всё остальное уже обёртка над этим.

Как создать?

Скрипт создаст указанную директорию, вместе со всеми родительскими директориями, если потребуется, и построит виртуальное окружение. Это можно делать и в Windows, только вызов будет чуть более многословным:

При создании можно добавлять различные параметры, как, например, включение системных site-packages или использование symlink вместо копирования интерпретатора.

В отличии от virtualenv новый venv требует чтобы создаваемая директория не существовала, либо была пустой. Вероятно, это сделано, чтобы не допускать конфликтов с существующими файлами. Это бага в python 3.3, в 3.4 уже исправлено. (Спасибо, svetlov).

Как использовать?

Можно использовать старый добрый метод активации через bin/activate (Scripts/activate в windows):

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

Обновление

Это произойдёт автоматически, если использовать symlink, но если вы хотите кроме изоляции делать фиксацию версии python и библиотек, я бы рекомендовал делать обновление вручную.

Расширение EnvBuilder

Метод описывает суть всего процесса: создание директории ( ensure_directories ), конфигурацию ( create_configuration ), добавление бинарников питона ( setup_python ) и добавление скриптов активации ( setup_scripts ).

Соответственно, для запуска python скрипта внутри окружения, можно сделать:

Исполняемые скрипты внутри venv

Вернёмся к проблеме с исполняемыми скриптами внутри виртуального окружения.

Пример шаблона запускаемого python скрипта:

__VENV_PYTHON__ будет заменено на полный путь к интерпретатору python в виртуальном окружении.

venv — Creation of virtual environments¶

New in version 3.3.

Source code: Lib/venv/

The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories. Each virtual environment has its own Python binary (which matches the version of the binary that was used to create this environment) and can have its own independent set of installed Python packages in its site directories.

See PEP 405 for more information about Python virtual environments.

Creating virtual environmentsВ¶

Creation of virtual environments is done by executing the command venv :

Deprecated since version 3.6: pyvenv was the recommended tool for creating virtual environments for Python 3.3 and 3.4, and is deprecated in Python 3.6.

Changed in version 3.5: The use of venv is now recommended for creating virtual environments.

On Windows, invoke the venv command as follows:

Alternatively, if you configured the PATH and PATHEXT variables for your Python installation :

While symlinks are supported on Windows, they are not recommended. Of particular note is that double-clicking python.exe in File Explorer will resolve the symlink eagerly and ignore the virtual environment.

On Microsoft Windows, it may be required to enable the Activate.ps1 script by setting the execution policy for the user. You can do this by issuing the following PowerShell command:

Once a virtual environment has been created, it can be “activated” using a script in the virtual environment’s binary directory. The invocation of the script is platform-specific ( must be replaced by the path of the directory containing the virtual environment):

Command to activate virtual environment

PS C:\> \Scripts\Activate.ps1

When a virtual environment is active, the VIRTUAL_ENV environment variable is set to the path of the virtual environment. This can be used to check if one is running inside a virtual environment.

You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.

You can deactivate a virtual environment by typing “deactivate” in your shell. The exact mechanism is platform-specific and is an internal implementation detail (typically a script or shell function will be used).

New in version 3.4: fish and csh activation scripts.

New in version 3.8: PowerShell activation scripts installed under POSIX for PowerShell Core support.

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

A virtual environment is a directory tree which contains Python executable files and other files which indicate that it is a virtual environment.

Common installation tools such as setuptools and pip work as expected with virtual environments. In other words, when a virtual environment is active, they install Python packages into the virtual environment without needing to be told to do so explicitly.

When a virtual environment is active (i.e., the virtual environment’s Python interpreter is running), the attributes sys.prefix and sys.exec_prefix point to the base directory of the virtual environment, whereas sys.base_prefix and sys.base_exec_prefix point to the non-virtual environment Python installation which was used to create the virtual environment. If a virtual environment is not active, then sys.prefix is the same as sys.base_prefix and sys.exec_prefix is the same as sys.base_exec_prefix (they all point to a non-virtual environment Python installation).

When a virtual environment is active, any options that change the installation path will be ignored from all distutils configuration files to prevent projects being inadvertently installed outside of the virtual environment.

The high-level method described above makes use of a simple API which provides mechanisms for third-party virtual environment creators to customize environment creation according to their needs, the EnvBuilder class.

The EnvBuilder class accepts the following keyword arguments on instantiation:

system_site_packages – a Boolean value indicating that the system Python site-packages should be available to the environment (defaults to False ).

clear – a Boolean value which, if true, will delete the contents of any existing target directory, before creating the environment.

symlinks – a Boolean value indicating whether to attempt to symlink the Python binary rather than copying.

prompt – a String to be used after virtual environment is activated (defaults to None which means directory name of the environment would be used). If the special string «.» is provided, the basename of the current directory is used as the prompt.

upgrade_deps – Update the base venv modules to the latest on PyPI

Changed in version 3.4: Added the with_pip parameter

New in version 3.6: Added the prompt parameter

New in version 3.9: Added the upgrade_deps parameter

Creators of third-party virtual environment tools will be free to use the provided EnvBuilder class as a base class.

The returned env-builder is an object which has a method, create :

Create a virtual environment by specifying the target directory (absolute or relative to the current directory) which is to contain the virtual environment. The create method will either create the environment in the specified directory, or raise an appropriate exception.

The create method of the EnvBuilder class illustrates the hooks available for subclass customization:

Creates the environment directory and all necessary directories, and returns a context object. This is just a holder for attributes (such as paths), for use by the other methods. The directories are allowed to exist already, as long as either clear or upgrade were specified to allow operating on an existing environment directory.

Creates the pyvenv.cfg configuration file in the environment.

Creates a copy or symlink to the Python executable in the environment. On POSIX systems, if a specific executable python3.x was used, symlinks to python and python3 will be created pointing to that executable, unless files with those names already exist.

Installs activation scripts appropriate to the platform into the virtual environment.

Upgrades the core venv dependency packages (currently pip and setuptools ) in the environment. This is done by shelling out to the pip executable in the environment.

New in version 3.9.

A placeholder method which can be overridden in third party implementations to pre-install packages in the virtual environment or perform other post-creation steps.

Changed in version 3.7.2: Windows now uses redirector scripts for python[w].exe instead of copying the actual binaries. In 3.7.2 only setup_python() does nothing unless running from a build in the source tree.

In addition, EnvBuilder provides this utility method that can be called from setup_scripts() or post_setup() in subclasses to assist in installing custom scripts into the virtual environment.

path is the path to a directory that should contain subdirectories “common”, “posix”, “nt”, each containing scripts destined for the bin directory in the environment. The contents of “common” and the directory corresponding to os.name are copied after some text replacement of placeholders:

__VENV_DIR__ is replaced with the absolute path of the environment directory.

__VENV_NAME__ is replaced with the environment name (final path segment of environment directory).

__VENV_PROMPT__ is replaced with the prompt (the environment name surrounded by parentheses and with a following space)

__VENV_BIN_NAME__ is replaced with the name of the bin directory (either bin or Scripts ).

__VENV_PYTHON__ is replaced with the absolute path of the environment’s executable.

The directories are allowed to exist (for when an existing environment is being upgraded).

There is also a module-level convenience function:

Create an EnvBuilder with the given keyword arguments, and call its create() method with the env_dir argument.

New in version 3.3.

Changed in version 3.4: Added the with_pip parameter

Changed in version 3.6: Added the prompt parameter

Changed in version 3.9: Added the upgrade_deps parameter

An example of extending EnvBuilder В¶

The following script shows how to extend EnvBuilder by implementing a subclass which installs setuptools and pip into a created virtual environment:

This script is also available for download online.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *