Virtualenv — Python for you and me 0.5.beta1 documentation (2024)

Virtual Python Environment or venv is a Python environment which will help youto install different versions of Python modules in a local directory using whichyou can develop and test your code without requiring to install everythingsystemwide.

Installation

In Python3 we can use the venv module to create virtual environments.

Usage

We will create a directory call virtual inside which we will have twodifferent virtual environment.

The following commands will create an env called virt1.

$ cd virtual$ python3 -m venv virt1$

Now we can activate the virt1 environment.

$ source virt1/bin/activate(virt1)[user@host]$

The first part of the prompt is now the name of the virtual environment, itwill help you identify which environment you are in when you have multipleenvironments.

To deactivate the environment use deactivate command.

(virt1)$ deactivate$

So, now we will install a Python module called redis.

(virt1)$ python3 -m pip install redisCollecting redis Downloading redis-2.10.5-py2.py3-none-any.whl (60kB) 100% |████████████████████████████████| 61kB 607kB/sInstalling collected packages: redisSuccessfully installed redis-2.10.5

See also

Read this blog post from Brett Cannon to understand why you should usepython3 -m pip to install packages.

Now we will create another virtual environment virt2 where we willinstall the same redis module but an old 2.4 version of it.

$ python3 -m venv virt2$ source virt2/bin/activate(virt2)$(virt2)$ python3 -m pip install redis==2.4Downloading/unpacking redisDownloading redis-2.4.0.tar.gzRunning setup.py egg_info for package redisInstalling collected packages: redisRunning setup.py install for redisSuccessfully installed redisCleaning up...

This way you can have many different environments for all of your developmentneeds.

Note

Always remember to create virtualenvs while developing new applications. This will help you keep the system modules clean.

Pipenv

Pipenv is a tool created by Kenneth Reitz which helps to create, manage thevirtualenvs for your projects. It also helps to install/uninstall/update thedependencies of your project.

Installing pipenv

We can install pipenv by the following command.

$ python3 -m pip install --user pipenv

Using pipenv

You can go to your project directory, and then use the command pipenvinstall to create a new virtualenv for you. You can also pass any modulename which pipenv will install on the environment.

$ mkdir myproject$ cd myproject$ pipenv install requestsCreating a virtualenv for this project…Using /usr/bin/python3 (3.6.5) to create virtualenv…⠋Already using interpreter /usr/bin/python3Using base prefix '/usr'New python executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/python3Also creating executable in /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4l/bin/pythonInstalling setuptools, pip, wheel...done.Virtualenv location: /home/fedora/.local/share/virtualenvs/myproject-dbBcpQ4lCreating a Pipfile for this project…Installing requests…Collecting requestsDownloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)Collecting chardet<3.1.0,>=3.0.2 (from requests)Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)Collecting idna<2.7,>=2.5 (from requests)Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)Collecting certifi>=2017.4.17 (from requests)Using cached https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whlCollecting urllib3<1.23,>=1.21.1 (from requests)Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)Installing collected packages: chardet, idna, certifi, urllib3, requestsSuccessfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22Adding requests to Pipfile's [packages]…Pipfile.lock not found, creating…Locking [dev-packages] dependencies…Locking [packages] dependencies…Updated Pipfile.lock (b14837)!Installing dependencies from Pipfile.lock (b14837)…🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:02To activate this project's virtualenv, run the following:$ pipenv shell

The above command will create a new virtualenv and then also installrequests module in the environment. You can then use pipenv shellcommand to activate that environment. For our example, we will usethe following Python code in a file named main.py.

import requestsresponse = requests.get('https://httpbin.org/ip')print('Your IP is {0}'.format(response.json()['origin']))
$ pipenv shell$ $ python main.pyYour IP is 192.168.1.2

Exiting from the virtualenv

You can exit from the virtualenv using exit command, or by pressing Ctrl+d.

Pipfile and Pipfile.lock

If you notice your project directory after you have used pipenv, you willfind two new files inside, Pipfile and Pipfile.lock. These files have beencreated by the pipenv command. You should checkin these two files intoyour version control system (say: git), so that others can create the exactsame environment of yours.

Pipfile

The following is the content of our Pipfile. It is using the TOML file format.

[[source]]verify_ssl = truename = "pypi"url = "https://pypi.python.org/simple"[dev-packages][requires]python_version = "3.6.5"[packages]requests = "*"

On the top it tells which source to use to get the packages. It also mentionsthe Python version required. The packages section tells us what all Pythonpackages we need. The string “*” means install the latest version availableon the package index. The exact version details of the packages are stored inthe Pipfile.lock file, it is in machine readable JSON format.

Remember to install any dependency for your project using pipenv comamnd,that will automatically update your Pipfile and Pipfile.lock file. If youhave any dependency which is only required for the development, you caninstall them marked as dev-packages. In the following example I am installingflake8 as development dependency.

$ pipenv install --dev flake8$ cat Pipfile[[source]]verify_ssl = truename = "pypi"url = "https://pypi.python.org/simple"[dev-packages]"flake8" = "*"[requires]python_version = "3.6.5"[packages]requests = "*"

You can watch this talk byKenneth from PyCon 2018 to know more about Pipenv.

Through out the rest of the book, we will use pipenv to create and managevirtualenvs for any code.

Virtualenv — Python for you and me 0.5.beta1 documentation (2024)

FAQs

How to activate virtual env using Python? ›

Activating a virtual environment in Python is straightforward. You can do this via the command source venv/bin/activate . This uses the 'activate' script located in the 'Scripts' directory of your virtual environment.

How to get out of a virtual environment in Python? ›

The most common and recommended way to exit a Python virtualenv is by using the “deactivate” command. This command is automatically created when you activate a virtualenv and is responsible for restoring the system's default settings.

Do I need to activate virtual environment in Python? ›

You don't specifically need to activate a virtual environment, as you can just specify the full path to that environment's Python interpreter when invoking Python. Furthermore, all scripts installed in the environment should be runnable without activating it.

What is the difference between venv and Virtualenv? ›

venv is a built-in module in Python 3.3 and later versions that allows you to create isolated Python environments. It is similar to virtualenv , but it is installed by default with Python. pyvenv is a script that comes with Python 3.3 and later versions that allows you to create virtual environments.

How to create and activate virtual environment in Python in VS Code? ›

Open VS Code. Click on the Terminal menu in the top menu bar and select New Terminal. In the terminal, navigate to the directory where you want to create the virtual environment. Type the following command to create a new virtual environment: python -m venv <name_of_virtual_environment>

Why use Python virtual environment? ›

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.

How to list all virtual environments in Python? ›

Listing your Virtual Environments

To see a list of the Python virtual environments that you have created, you can use the 'conda env list' command. This command will give you the names as well as the filesystem paths for the location of your virtual environments.

How do I find my virtual environment in Python? ›

From a shell prompt, you can just do echo $VIRTUAL_ENV (or in Windows cmd.exe , echo %VIRTUAL_ENV% ). From within Python, sys. prefix provides the root of your Python installation (the virtual environment if active), and sys.

How to create a virtual environment using virtualenv? ›

Create an environment from a requirements.

virtualenv <my_env_name> to create a new environment. source <my_env_name>/bin/activate to activate the new environment. pip install -r requirements. txt to install the requirements in the current environment.

What is meant by virtual environment? ›

A virtual environment is a networked application that allows a user to interact with both the computing environment and the work of other users. Email, chat, and web-based document sharing applications are all examples of virtual environments. Simply put, it is a networked common operating space.

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5855

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.