How To Create A Virtual Environment in Python

I have been using Python a lot recently and thought I would write a blog post on how to create a virtual environment in Python and why I suggest you use them

What is a Virtual Environment in Python?

A virtual environment is a self-contained directory that will contain a Python installation along with all the dependencies of Python packages that your installation will want to have also. The beauty really is that you can work on multiple Python projects that will require different libraries or even different Python package versions with it causing any issues to your main Python installation, lets look at this more:

Why you would want to use a Virtual Environment?

  • Environment Isolation: A virtual environment allows for total environment isolation, where that be for different dependencies or even Python versions
  • Dependency Management: I find this huge, ability to have different dependencies within a virtual environment that won’t effect your main Python installation
  • Create a list of dependencies for your project: Another useful option as to why you should be using virtual environments in Python – you can create a list of dependencies for your project to run, I will share how to do this later in this psot
  • Reproducibility: You can share your project with others that will include all its dependencies, ensuring the projects running is consistent

Create a Virtual Environment in Python and running a Python script

Prior to the below, do ensure Python is installed on your system, if not you can download from here (please note, the below I am using macOS, windows commands may slightly differ)

  • Install virtualenv with pip
pip install virtualenv
  • Create a Virtual Environment in the directory to where you want to run your Python script
python -m venv example

example is the name I have used for my python virtual environment, reviewing the below we can see a new directory created containing the Python interpreter and library

Screenshot of Vscode, showing the virtual environment example has been created successfully.
  • Activating the Python Virtual Environment

Now that a virtual environment has been created, it now needs to be activated using source <virtual_environment_name>/bin/activate

thomas@macbook python-virtual-env-example % source example/bin/activate
(example) thomas@macbook python-virtual-env-example %

Notice the (example) in the terminal output above? This shows that I am now in the Python Virtual environment

  • Installing a Python Package

Now that the Virtual Environment is activated, lets install an example Python Package

(example) thomas@macbook python-virtual-env-example % pip install requests
Collecting requests
  Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Collecting charset-normalizer<4,>=2 (from requests)
  Downloading charset_normalizer-3.3.2-cp312-cp312-macosx_10_9_x86_64.whl.metadata (33 kB)
Collecting idna<4,>=2.5 (from requests)
  Using cached idna-3.7-py3-none-any.whl.metadata (9.9 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
  Downloading urllib3-2.2.2-py3-none-any.whl.metadata (6.4 kB)
Collecting certifi>=2017.4.17 (from requests)
  Using cached certifi-2024.7.4-py3-none-any.whl.metadata (2.2 kB)
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 64.9/64.9 kB 2.2 MB/s eta 0:00:00
Using cached certifi-2024.7.4-py3-none-any.whl (162 kB)
Downloading charset_normalizer-3.3.2-cp312-cp312-macosx_10_9_x86_64.whl (122 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 122.2/122.2 kB 4.0 MB/s eta 0:00:00
Using cached idna-3.7-py3-none-any.whl (66 kB)
Downloading urllib3-2.2.2-py3-none-any.whl (121 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.4/121.4 kB 4.5 MB/s eta 0:00:00
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2024.7.4 charset-normalizer-3.3.2 idna-3.7 requests-2.32.3 urllib3-2.2.2

We have installed the python package requests

  • Running python within your virtual environment
import requests

# URL of the webpage to fetch
url = "https://www.thomasthornton.cloud"

# Make an HTTP GET request
response = requests.get(url)

# Print the content of the response
print(response.text)

The above, uses the package requests and attempts a HTTP GET request

The above Python is in python.py – and when running inside my virtual environment:

(example) thomas@macbook python-virtual-env-example % python python.py

Creating a list of packages that are required to run the Python project

As I mentioned above, a pretty cool use case for virtual environments is the ability to output the installed packages it uses. Useful to share with colleagues and a public repository etc!

(example) thomas@macbook python-virtual-env-example % pip freeze > requirements.txt

Reviewing requirements.txt shows the list of package dependencies needed:

certifi==2024.7.4
charset-normalizer==3.3.2
idna==3.7
requests==2.32.3
urllib3==2.2.2
  • Installing requirements.txt in another virtual environment is very straight forward:
pip install -r requirements.txt

Finally, when you are finished with the current virtual environment – you can just deactivate by running deactivate:

(example) thomas@macbook python-virtual-env-example % deactivate
thomas@macbook python-virtual-env-example % 

Awesome, hopefully this has given you an overview of why you should be using Python environments, certainly a simple but awesome way to manage all them Python projects, allowing you to work on multiple with different dependencies and avoiding any package conflicts that may occur – working in isolated virtual environments

Leave a Reply

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Thomas Thornton Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading