Python Virtual Environments: venv
satya - 3/6/2025, 1:33:28 PM
My detailed document on setting up virtual environments: github md file
My detailed document on setting up virtual environments: github md file
satya - 3/6/2025, 1:35:05 PM
why do this?
- Python development has a sea of modules
- As you start developing the modules your need of modules explode. It is not uncommon to have hundreds of modules.
- As you work with multiple projects, you don't want the global python environment having too many unrelated modules downloaded to the same place.
satya - 3/6/2025, 1:36:39 PM
More importantly
- As you try to move your project to prod...
- The modules that are necessary on the production machine needs to be installed in prod. Knowing what they are become essential
- Knowing these dependent modules, which is what from the total python global environment is a trial and error process
- So isolating the environment allows better accounting of dependencies
- Of course, this also avoid possible library conflicts (it does happen)
satya - 3/6/2025, 1:42:44 PM
How does this work?
- There is a module called "venv" in the python eco system natively to do this
- When you run this module in a root folder of your project, it creates a subdirectory
- You can name the sub directory
- usually the name is .venv
- .....
- All of the python versions and dependent libraries are tucked underneath here
- Of course you don't want to check this directory into the github
- .....
- Then when you run python command line, you want to run it the version of python and its libraries from this sub directory
- That means the paths of the terminal needs to be accordingly set in that terminal
- ....
- To do this, you have to run a program called "activate.cmd" that is in the scripts sub directory
- when you do this, then on all library references will be run from here.
- Importantly any pip installs from now on in that terminal will load the libraries or modules into the .venv sub folder
- ...
- So this completes the activating of the virtual environment
- You can deactivate and go back to the global python if you need (usually you don't need to) for this project
- .....
- For a different project...
- it has its own virtual .venv
- and has its own enviornment.
- ....
- Not only can you have different python libraries
- You can have a diff version of python itself in each venv
satya - 3/6/2025, 1:42:59 PM
Lets document code for some of these key elements
Lets document code for some of these key elements
satya - 3/6/2025, 1:44:21 PM
Creating a virtual environment
Go to the root directory of your project and run the following command
python -m venv .venv
satya - 3/6/2025, 1:45:01 PM
This will
- Creates a sub directory called .venv under your root folder
- This will then host all future pip installs
If you run this in vscode terminal, it may prompt you to apply this to the entire workspace or just the root folder/project. Choose "No" so that it is just for this project.
satya - 3/6/2025, 1:46:25 PM
Activating a virtual environment
Then you activate the virtual environment by running activate script. This sets up the terminal or command line. You can do this directly in vscode or in an open command window.
.venv\Scripts\activate
or
.venv\Scripts\Activate.ps1
Note: On newer windows running a powershell script is more secure and so will not allow you to run the activate script. You can change the execution policy by running the following command in an elevated PowerShell (Run as Administrator):
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
You may want to run this powershell outside of vscode so that you can run it as an admin. I am not sure how you do that inside vscode.
satya - 3/6/2025, 1:47:23 PM
To know which packages are installed
pip freeze > requirements.txt
satya - 3/6/2025, 1:48:43 PM
What will this do?
- It lists all the current packages in this virtual environment
- And pipes them to a file called requirements.txt
- You can then use that requirements.txt to reinstall all packages when need arises.
satya - 3/6/2025, 1:49:00 PM
Here is how you do that
pip install -r requirements.txt
satya - 3/6/2025, 1:50:51 PM
How do I know I am running under a virtual environment
Your command prompt changes
like this....
.....
(.venv) your-directory>
(.venv) your-directory>
satya - 3/6/2025, 1:51:33 PM
For rest of the details use my previous github reference
satya - 3/7/2025, 10:51:42 AM
So what is a python virtual environment?
- It is really a simple idea.
- Don't confound it with the OS VMs that one refers to as virtual...
- It simply means this....
- .....
- For every project (usually a root folder) specify a "directory" somewhere (usually a local sub directory in that project) where python libraries will be installed just for the sake of that project.
- And the command line python will be run from that directory and library references will be resolved only from that directory.
- This keeps your project pretty isolated for its dependencies, so that it is easy to move the project to production or another machine.
- ....
- Thats it.
- It is not like a complicated VM.
- ....
- The mechanics how this is done is the detail.