The solution for this problem is to create a virtual environment, aself-contained directory tree that contains a Python installation for aparticular version of Python, plus a number of additional packages.
The module used to create and manage virtual environments is calledvenv. venv will usually install the most recent version ofPython that you have available. If you have multiple versions of Python on yoursystem, you can select a specific Python version by running python3 orwhichever version you want.
A common directory location for a virtual environment is .venv.This name keeps the directory typically hidden in your shell and thusout of the way while giving it a name that explains why the directoryexists. It also prevents clashing with .env environment variabledefinition files that some tooling supports.
Used to contain a specific Python interpreter and software libraries andbinaries which are needed to support a project (library or application). Theseare by default isolated from software in other virtual environments and Pythoninterpreters and libraries installed in the operating system.
While symlinks are supported on Windows, they are not recommended. Ofparticular note is that double-clicking python.exe in File Explorerwill resolve the symlink eagerly and ignore the virtual environment.
When a Python interpreter is running from a virtual environment,sys.prefix and sys.exec_prefixpoint to the directories of the virtual environment,whereas sys.base_prefix and sys.base_exec_prefixpoint to those of the base Python used to create the environment.It is sufficient to checksys.prefix != sys.base_prefix to determine if the current interpreter isrunning from a virtual environment.
When a virtual environment has been activated, the VIRTUAL_ENVenvironment variable is set to the path of the environment.Since explicitly activating a virtual environment is not required to use it,VIRTUAL_ENV cannot be relied upon to determinewhether a virtual environment is being used.
You can deactivate a virtual environment by typing deactivate in your shell.The exact mechanism is platform-specific and is an internal implementationdetail (typically, a script or shell function will be used).
The high-level method described above makes use of a simple API which providesmechanisms for third-party virtual environment creators to customize environmentcreation according to their needs, the EnvBuilder class.
Create a virtual environment by specifying the target directory(absolute or relative to the current directory) which is to contain thevirtual environment. The create method will either create theenvironment in the specified directory, or raise an appropriateexception.
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 thatexecutable, unless files with those names already exist.
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.
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.
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.
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.
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:
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).
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.
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.
Virtual environments are a common and effective technique used in Python development. Gaining a better understanding of how they work, why you need them, and what you can do with them will help you master your Python programming workflow.
Throughout the tutorial, you can select code examples for either Windows, Ubuntu Linux, or macOS. Pick your platform at the top right of the relevant code blocks to get the commands that you need, and feel free to switch between your options if you want to learn how to work with Python virtual environments on other operating systems.
Note: There are other great third-party tools for creating virtual environments,such as conda and virtualenv,that you can learn more about later in this tutorial.Any of these tools can help you set up a Python virtual environment.
This command is the default command that you should use to install external Python packages with pip. Because you first created and activated the virtual environment, pip will install the packages in an isolated location.
Congratulations, you can now install your packages to your virtual environment. To get to this point, you began by creating a Python virtual environment named venv and then activated it in your current shell session.
Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt.In the example above, the name of the environment is venv.
Nearly everyone in the Python community suggests that you use virtual environments for all your projects.But why?If you want to find out why you need to set up a Python virtual environment in the first place,then this is the right section for you.
Your Flask app has turned out to be quite helpful, so other developers want to work on it as well. They need to reproduce the environment that you used for working on it. You want to go ahead and pin your dependencies so that you can share your project online:
bin/ contains the executable files of your virtual environment. Most notable are the Python interpreter (python) and the pip executable (pip), as well as their respective symlinks (python3, python3.10, pip3, pip3.10). The folder also contains activation scripts for your virtual environment. Your specific activation script depends on what shell you use. For example, in this tutorial, you ran activate, which works for the Bash and Zsh shells.
The installed packages inside site-packages/ are optional but come as a reasonable default. However, your virtual environment would still be a valid virtual environment if this directory were empty, and there are ways to create it without installing any dependencies.
Keep in mind that your virtual environment is just a folder structure, which means that you can delete and re-create it anytime you want to. But why this specific folder structure, and what does it make possible?
Note: Because you always need an existing Python installation to create your virtual environment, venv opts to reuse the existing standard library modules to avoid the overhead of copying them into your new virtual environment. This intentional decision speeds up the creation of virtual environments and makes them more lightweight, as described in the motivation for PEP 405.
With the standard folder structure in place, the Python interpreter in your virtual environment can understand where all relevant files are located. It does this with only minor adaptations to its prefix-finding process according to the venv specification.
This change effectively allows the Python interpreter in your virtual environment to use the standard library modules from your base Python installation while pointing to its internal site-packages directory to install and access external packages.
The Python executable in your virtual environment has access to the standard library modules of the Python installation on which you based the environment. Python makes this possible by pointing to the file path of the base Python executable in the home setting in pyvenv.cfg:
If you navigate to the path value of the highlighted line in pyvenv.cfg and list the contents of the folder, then you find the base Python executable that you used to create your virtual environment. From there, you can navigate to find the folder that contains your standard library modules:
aa06259810