The short answer is this:
A directory with a particular file structure. It has a bin subdirectory that includes links to a Python interpreter as well as subdirectories that hold packages installed in the specific (venv). virtual environment’s are Python’s way of separating dependencies between projects. therefore, the Ruby's bundler or Javascript's npm come with its features built in, python package manager pip, does not.
The Python have few common use cases for a virtual environment:
- You’re developing multiple projects that depend on different versions of the same packages, or you have a project that must be isolated from certain packages because of a namespace collision. This is the most standard use case.
- You’re working in a Python environment where you can’t modify the site-packages directory. This may be because you’re working in a highly controlled environment, such as managed hosting, or on a server where the choice of interpreter (or packages used in it) can’t be changed because of production requirements.
- You want to experiment with a specific combination of packages under highly controlled circumstances, for instance to test cross-compatibility or backward compatibility.
- You want to run a “baseline” version of the Python interpreter on a system with no third-party packages, and only install third-party packages for each individual project as needed.
- Nothing says you can’t simply unpack a Python library into a subfolder of a project and use it that way. Likewise, you could download a standalone copy of the Python interpreter, unpack it into a folder, and use it to run scripts and packages devoted to it.
But managing such cobbled-together projects soon becomes difficult. It only seems easier to do that at first. Working with packages that have binary components, or that rely on elaborate third-party dependencies, can be a nightmare. Worse, reproducing such a setup on someone else’s machine, or on a new machine you manage, is tricky.
Benefits:
- You can have multiple environments, with multiple sets of packages, without conflicts among them. This way, different projects’ requirements can be satisfied at the same time.
- You can easily release your project with its own dependent modules.
Examples:
Here are two ways you can create Python virtual environments.
Virtualenv:
It is a tool used to create isolated Python environments. It creates a folder which contains all the necessary executables to use the packages that a Python project would need.
you can install it with pip: pip install virtualenv
Verify the installation of following command: virtualenv --version
We can create Virtual environment in Visual studio code can do this when the Python extension is enabled and pycharm ide in both windows and Linux platform. Many Python IDEs will automatically detect and activate a virtual environment if one is found in the current project directory. Visual Studio Code, Opening a terminal inside Visual Studio Code will automatically activate the selected virtual environment. PyCharm automatically creates a virtual environment for each new project.
Activate the Python virtual environment
Before you can use this virtual environment, you need to explicitly activate it. Activation makes the virtual environment the default Python interpreter for the duration of a shell session.
You’ll need to use different syntax for activating the virtual environment depending on which operating system and command shell you’re using.
- On Unix or MacOS, using the bash shell: source /path/to/venv/bin/activate
- On Unix or MacOS, using the csh shell: source /path/to/venv/bin/activate.csh
- On Unix or MacOS, using the fish shell: source /path/to/venv/bin/activate.fish
- On Windows using the Command Prompt: path\to\venv\Scripts\activate.bat
- On Windows using PowerShell: path\to\venv\Scripts\Activate.ps1
Install Packages
You can install packages one by one, or by setting a requirements.txt file for your project.
pip install some-package
pip install -r requirements.txt
If you want to create a requirements.txt file from the already installed packages, run the following command:
pip freeze > requirements.txt
The file will contain the list of all the packages installed in the current environment, and their respective versions. This will help you release your project with its own dependent modules.
Deactivate an Environment
If you are done working with the virtual environment you can deactivate it with:
deactivate
This puts you back to the system’s default Python interpreter with all its installed libraries.
Delete an Environment
Simply delete the environment folder.
Top comments (0)