Virtual Environments
Background
One issue that programmers have is keeping the working environment for each project clean and with only the correct versions of Python and other installed packages. Virtual environments are useful for this purpose in keeping Python projects separate from other Python projects on your machine.
Even if we aren’t installing any new Python packages in our project, we still want to use a virtual environment for each project because:
Inside our virtual environment, we don’t need to specify which version of Python we use, we can just call
python, and it will always use the correct version.It is a good practice in general to use a separate virtual environment for every project.
We are not installing any new Python packages for this course, but you should have an idea of how to set up virtual environments for your use in the future.
Creating the Virtual Environment
Important. First, open a command prompt or terminal window that you will use. Create a new folder for your project in your home directory or desktop (or wherever you want to save your work).
We will call this folder python_class for this example.
Once you create the folder, change into the directory using cd.
Do not put it in your C:\Python*\ or win32 folder or any other system folder.
You can call it whatever you want, but we will be referring to it as python_class.
Install the Virtual Environment
Now inside this new python_class directory, we will create our virtual environment.
On Windows, enter these 2 commands:
> py -m venv myvenv
> myvenv\Scripts\activate
On OSX and Linux, enter these commands:
$ python3 -m venv myvenv
$ source myvenv/bin/activate
Note that it might take a minute or two to create the virtual environment, especially on Windows, so be patient.
The first command creates the virtual environment, which is stored in a new folder (created in python_class) named myvenv. The second command activates the environment.
Do not store any other files in myvenv or modify what is there!
It is only for the system to use.
You may give it a different name from myvenv, just make sure that you always know that it is the virtual environment folder.
Notice that your command prompt is now prefixed with (myvenv). This means that your virtual environment is activated and working correctly.
Activating the Virtual Environment
From now on, whenever you open a new command or terminal window to work on this project, you will need to change directories into the python_class folder and activate your virtual environment.
Activating on Windows:
> myvenv\Scripts\activate
Activating on OSX and Linux:
$ source myvenv/bin/activate
You can tell that your virtual environment is working when your command prompt is prefixed with (myvenv).
Working in a Virtual Environment
Once your virtual environment is activated, typing python will always run the version of Python that we created our environment with. Type python --version and it should say Python 3.11.4 or whatever is your current version of Python.
More on virtual environments
For more on using virtual environments in Python, see Using virtual environments in Python.
Also see these official resources from the Python Packaging Authority:
Tutorial: Installing Python Packages
Tutorial: Managing Application Dependencies
Guide: Installation.