Open In App

Create virtual environment using venv | Python

Improve
Improve
Like Article
Like
Save
Share
Report

Managing multiple Python projects that have conflicting dependencies can be a daunting task. However, virtual environments can help you solve this problem. With virtual environments, each project can thrive in its own sandboxed paradise with its libraries and settings. By learning how to create virtual environments using Venv, you can say goodbye to dependency conflicts and welcome organized, conflict-free Python development!

Why Need Virtual Environment?

Imagine a scenario where a web app is hosted on a cloud hosting service provider with a Python development environment. The configuration for the web app comes with an option for installing the newest version of the Flask web framework. Suppose, the web app is created on the local system with an older version of the framework and as soon as it is uploaded on the site, there will be a version conflict as some of the modules used are depreciated in the latest versions of Flask.

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated Python virtual environments for them. This is one of the most important tools that most Python developers use.

In this discussion, I shall elucidate the process of creating a virtual environment in Python and the digital surroundings on Windows, Linux, Unix, and Mac OS. contingent on your operating system and the shell in use, the act of initializing the virtual environment entails disparate syntaxes.

OS

Shell

Activation Command

Windows

Command Prompt

‘path\to\venv\Scripts\activate’

Windows

PowerShell

‘.\path\to\venv\Scripts\Activate’

macOS/Linux

Bash

‘source path/to/venv/bin/activate’

macOS/Linux

Fish

‘source path/to/venv/bin/activate.fish’

macOS/Linux

PowerShell

‘path\to\venv\Scripts\Activate’

Create a Virtual Environment using virtualenv in Python

First, check whether the pip has the same version of the interpreter as that on the system and where the Python environment currently resides: To check where the Python currently resides type the below command in the terminal.

where python

where pip

If you’re using Linux, then the “where” command is “which”. It will give you the same information as “where” on Windows.

which python

which pip

Output:

C:\Users\GeeksforGeeks\AppData\Local\Programs\Python\Python37\python.exe C:\Users\GeeksforGeeks\AppData\Local\Programs\Python\Python37\Scripts\pip.exe

To create a virtualenv use the following command:

python -m venv ./venv

After running this command, a directory named venv will be created. This is the directory which contains all the necessary executables to use the packages that a Python project would need. This is where Python packages will be installed. To list the files in the folder type below command in the terminal:

 dir ./venv

Output:

The pip command still points to the global environment. We need to explicitly activate the created virtual environment to configure the current shell session to use pip commands from the virtualenv folder and don’t end up installing packages in the global environment: To activate venv first change the directory to venv\Scripts.

cd venv\Scripts

After changing the directory type the below command.

$ Source venv_name\Scripts> activate

Once the virtual environment is activated, the name of your virtual environment will appear on left side of terminal. This will let you know that the virtual environment is currently active. In the image below, venv named virtual environment is active. 

(Note: try “./activate” instead of “activate” if using powershell terminal) The Python interpreter as well would run the version from the virtual environment and not the global one. We can verify where the Python environment currently resides by below command:

where python

Output:

E:\distribution\venv\Scripts\python.exe
C:\Users\GeeksforGeeks\AppData\Local\Programs\Python\Python37\python.exe

The virtual environment is an almost clean Python environment. Run pip list to see a list with packages installed: 

Output: 

Now you can install dependencies related to the project in this virtual environment. For example if you are using Django 1.9 for a project, you can install it like you install other packages.

(venv_name)$ pip install Django==1.9

Once you are done with the work, you can deactivate the virtual environment by the following command:

(venv_name)$ deactivate

 

Now you will be back to system’s default Python installation.

Activate Virtual Environment in Python Windows/Linux/MacOS

After create virtual environment in python, you need to Activate Virtual Environment Python. The activation command in windows use the below commands.

.\venv\Scripts\activate

The activation command differs between Linux and macOS.

For Linux: For activate the virtual environment in Linux use the below commands.

source myenv/bin/activate

For macOS: For activate the virtual environment in maxOS use the below commands. Alternatively, you can use the second command on macOS

source myenv/bin/activate (or) source myenv/bin/activate.csh

After activation, you should see the virtual environment name in your terminal prompt, indicating that you are now working within the virtual environment.

Use of virtual environment

The above scenario can be solved using virtual environment. Python development environments can be separated by making use of some virtual environment. A virtual environment, here, is an isolated Python installation that allows to manage dependencies and work on separate Python projects without affecting other projects. When a virtual environment is created, it creates a separate folder from the global Python or other virtual environments and copies Python into it along with a site-packages folder among a couple of others. For older versions of Python, virtual machines require installing a third-party tool called virtualenv. It’s been integrated into newer versions of Python3 under the module venv. To know more about virtualenv click here.

Conclusion

Now that you have the ability to create virtual environments, you can manage multiple projects with ease. You can work on a web application using Django on one side and experiment with data analysis using Pandas on the other, without worrying about version conflicts or library contamination. So, take advantage of the power of venv, create your personalized Python sandboxes, and unlock your full development potential!

Frequently Asked Question(FAQs)

1. How do I create a virtual environment using venv in Python?

You can create a virtual environment using the following command: python -m venv <environment_name>. Replace <environment_name> with the desired name for your virtual environment.

2. Do I need to install venv separately?

No, venv is included in the Python standard library for versions 3.3 and above, so there’s no need to install it separately.

3. Can I create a virtual environment for a specific Python version?

Yes, you can specify the Python version when creating a virtual environment. For example, python3.9 -m venv <environment_name>.

4. How do I activate a virtual environment?

On Windows, use <environment_name>\Scripts\activate. On Unix or MacOS, use source <environment_name>/bin/activate.

5. Can I deactivate a virtual environment?

Yes, simply run the command deactivate in the command prompt or terminal to exit the virtual environment.

6. Is it possible to create a virtual environment in a specific directory?

Yes, you can specify the directory when creating a virtual environment. For instance, python -m venv /path/to/directory.



Last Updated : 15 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads