The Wayback Machine - https://web.archive.org/web/20240910010511/https://www.geeksforgeeks.org/create-virtual-environment-using-venv-python/
Open In App

Create virtual environment using venv | Python

Last Updated : 15 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Similar Reads

Install OpenCV that was built with CMAKE build option to venv
OpenCV (Open Source Computer Vision Library) is a powerful tool for image processing and computer vision tasks. Often, we may need to customize the OpenCV installation using CMake to include specific modules or optimizations. This guide will walk us through the process of building OpenCV from the source using CMake and installing it into the Python
2 min read
Using mkvirtualenv to create new Virtual Environment - Python
Virtual Environment are used If you already have a python version installed and you want to use a different version for a project without bothering the older ones. it is good practice to use a new virtual environment for different projects. There are multiple ways of creating that, today we will create one using mkvirtualenv command. virtualenvwrap
2 min read
Using Jupyter Notebook in Virtual Environment
In this article, we are going to see how to set Virtual Environment in Jupyter. Sometimes we want to use the Jupyter notebook in a virtual environment so that only selected packages are available in the scope of the notebook. To do this we have to add a new kernel for the virtual environment in the list of kernels available for the Jupyter notebook
2 min read
Creating Python Virtual Environment in Windows and Linux
A Virtual Environment is a Python environment, that is an isolated working copy of Python that allows you to work on a specific project without affecting other projects So basically it is a tool that enables multiple side-by-side installations of Python, one for each project. Creating a Python virtual environment in Linux If pip is not in your syst
2 min read
Python Virtual Environment | Introduction
A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python. You can set up your own libraries and dependencies without affecting the system Python. We will use virtualenv to create a virtual environment in Python. What is a Virtual Environment?A virtual environment is a
4 min read
Setting Up a Virtual Environment in Django
Setting up a virtual environment in Django is essential for isolating your project's dependencies and ensuring consistent behavior across different environments. A virtual environment allows you to install packages locally without affecting the global Python installation. Here's how to set up a virtual environment for your Django project. Setting U
2 min read
Add packages to Anaconda environment in Python
Let's see some methods that can be used to install packages in the Anaconda environment. There are many ways one can add pre-built packages to an anaconda environment. So, let's see how to direct the path in the anaconda and install them. Add packages to the Anaconda environment using the navigator Step 1: Open your Anaconda navigator Step 2: Go to
2 min read
Python - Setting up the Bokeh Environment
Bokeh is supported on CPython versions 3.6+ only both with Standard distribution and Anaconda distribution. Other Python versions or implementations may or may not function. Current version of Bokeh is 2.0.2 . Bokeh package has the following dependencies: 1. Required Dependencies PyYAML&gt;=3.10python-dateutil&gt;=2.1Jinja2&gt;=2.7numpy&gt;=1.11.3p
1 min read
PYTHONPATH Environment Variable in Python
Python's behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules. The PYTHONPATH variable holds a string with the
2 min read
Environment Variables in Python
In Python, its behavior is highly influenced by the setup of the environment variables. There is a fixed number of environment variables that Python recognizes and these generally are processed before the command line switches. Whenever a conflict arises between the environmental variable and the command line switches, the environment variable gets
4 min read
Python Falcon - Environment Setup
Falcon is a Python library renowned for its proficiency in crafting indispensable REST APIs and microservices. Capable of accommodating both the WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface) standards, Falcon has gained widespread recognition since its inception. Originally conceived by Kurt Griffiths in Janua
3 min read
Python Pyramid - Environment Setup
Python Pyramid, often simply referred to as Pyramid, is an open-source web framework used for building web applications and web services in Python. Whether you're constructing a website, or a sophisticated web application Pyramid offers the resources and adaptability to complete the task effectively. Environment Setup for Python PyramidTo begin wit
3 min read
Access environment variable values in Python
An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-value pairs. In this article, we will see how to use
3 min read
Why can't we access Python from a Conda Environment?
Conda environments are isolated spaces where we can install specific Python versions and packages without interfering with other projects. But sometimes, some issues may arise when trying to activate or use these environments. In this article, we will learn about accessing issues of Python from a conda environment. Problem Statement:You've created
2 min read
Read Environment Variables with Python dotenv
Environment variables play a crucial role in the configuration and operation of software applications. They provide a mechanism to store configuration settings that can be used by applications to function properly. This separation of configuration from code allows for more secure and flexible software development practices. Introduction to python-d
4 min read
Create a shared Folder between Host OS and Guest OS ( Virtual Box)
How to create a shared folder between host Operating System and Guest Operating system? This is the scenario that you run Windows as your host operating system and Ubuntu in a VirtualBox, and that you want to access a specific Windows folder from Ubuntu. In short- Share a folder between Host OS-&gt; Windows and Guest OS -&gt;Ubuntu(Virtual box) Ste
2 min read
How to Create Virtual Machines with VirtualBox in Linux?
Quick Preview - Create Virtual Machines with VirtualBox on Linux:Configuring Virtual Machine ISO File:Open the VirtualBox Application &amp; click New.Upload the ISO file of the OS.Click Next to move ahead.Increase Base Memory at least by 3000 MB.Increase Virtual Disk Size by at least 30 GB.Click Finish.Configuring Settings of Virtual Machine:Click
5 min read
Build a Virtual Assistant Using Python
Virtual desktop assistant is an awesome thing. If you want your machine to run on your command like Jarvis did for Tony. Yes it is possible. It is possible using Python. Python offers a good major library so that we can use it for making a virtual assistant. Windows has Sapi5 and Linux has Espeak which can help us in having the voice from our machi
8 min read
Virtual Keyboard in python using OpenCv
Virtual keyboards are becoming a necessary tool for many applications, particularly those using touchscreens and augmented reality. Hand gestures combined with a virtual keyboard may provide for an intuitive and engaging user interface. This post will demonstrate how to use OpenCV, a powerful package for computer vision applications, and Python to
6 min read
Set up Opencv with anaconda environment
If you love working on image processing and video analysis using python then you have come to the right place. Python is one of the major languages that can be used to process images or videos. Requirements for OpenCV and Anaconda - 32- or a 64-bit computer. - For Miniconda—400 MB disk space. - For Anaconda—A minimum 3 GB disk space to download and
3 min read
Kotlin Environment setup for Command Line
To set up a Kotlin environment for the command line, you need to do the following steps: Install the Java Development Kit (JDK): Kotlin runs on the Java virtual machine, so you need to have the JDK installed. You can download the latest version from the official Oracle website.Download the Kotlin compiler: You can download the latest version of the
2 min read
Kotlin Environment setup with Intellij IDEA
Kotlin is a statically typed, general-purpose programming language developed by JetBrains that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011. Kotlin is object-oriented language and a better language than Java, but still be fully interoperable with Java code. Let's see how to setu
2 min read
Environment setup for CherryPy
CherryPy is a popular framework of Python. Using CherryPy, web applications can be built in a faster and more reliable way. It is also called a web application library. It is known for its simplicity as it is based on object-oriented Python programming, resulting in smaller source code in less time. Note: For more information, refer to Introduction
2 min read
Environment Setup for Matplotlib
Matplotlib is an overall package for creating static, animated, and interactive visualizations in Python. It literally opens up a whole new world of possibilities for you! Especially when it is used with Numpy or Pandas library, one can do unimaginable things. The plots give may give a new insight altogether. Now, the question arises i.e. How to ma
1 min read
Setup OpenCV With PyCharm Environment
Introduction:If you love working on image processing and video analysis using python then you have come to the right place. Python is one of the key languages which is used to process videos and images. Requirements for OpenCV:32- or a 64-bit computer.Windows, macOS or Linux.Python 2.7, 3.4, 3.5 or 3.6.PyCharmPyCharm is a cross-platform IDE used in
2 min read
How to setup Conda environment with Jupyter Notebook ?
Anaconda is open-source software that contains Jupyter, spyder, etc that are used for large data processing, data analytics, heavy scientific computing. Anaconda works for R and python programming language. Spyder(sub-application of Anaconda) is used for python. Opencv for python will work in spyder. Package versions are managed by the package mana
2 min read
How to Delete an Environment in Conda
If you have an environment in Anaconda and you no longer need it in your Anaconda, in this article we will see how to deactivate an environment in Anaconda and delete it. Firstly, let's know what is an anaconda environment. Anaconda EnvironmentAnaconda environments commonly known as Environment in Conda is generally the directory that contains the
3 min read
How to add environment variable in MacOS?
While programmers work on different Programming Languages, they use to declare Variables to work on any certain piece of code. The Variables in general terms are used to store some values that can be again accessed in the future. There are some Operating System Variables present that are known as Environmental Variables. In different Operating Syst
3 min read
How to Setup Anaconda Path to Environment Variable?
Anaconda is open-source software that contains Jupyter, spyder, etc that are used for large data processing, data analytics, and heavy scientific computing. Anaconda works for R and Python programming languages. Spyder(a sub-application of Anaconda) is used for Python. for Python will work in Spyder. Package versions are managed by the package mana
4 min read
Setting up the environment in Java
Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, etc. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. The latest version is Java 22. Below are the environment settings for both Linux and Windows. JVM, JRE
6 min read
Article Tags :
Practice Tags :