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?

  1. Python development has a sea of modules
  2. As you start developing the modules your need of modules explode. It is not uncommon to have hundreds of modules.
  3. 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

  1. As you try to move your project to prod...
  2. The modules that are necessary on the production machine needs to be installed in prod. Knowing what they are become essential
  3. Knowing these dependent modules, which is what from the total python global environment is a trial and error process
  4. So isolating the environment allows better accounting of dependencies
  5. Of course, this also avoid possible library conflicts (it does happen)

satya - 3/6/2025, 1:42:44 PM

How does this work?

  1. There is a module called "venv" in the python eco system natively to do this
  2. When you run this module in a root folder of your project, it creates a subdirectory
  3. You can name the sub directory
  4. usually the name is .venv
  5. .....
  6. All of the python versions and dependent libraries are tucked underneath here
  7. Of course you don't want to check this directory into the github
  8. .....
  9. Then when you run python command line, you want to run it the version of python and its libraries from this sub directory
  10. That means the paths of the terminal needs to be accordingly set in that terminal
  11. ....
  12. To do this, you have to run a program called "activate.cmd" that is in the scripts sub directory
  13. when you do this, then on all library references will be run from here.
  14. Importantly any pip installs from now on in that terminal will load the libraries or modules into the .venv sub folder
  15. ...
  16. So this completes the activating of the virtual environment
  17. You can deactivate and go back to the global python if you need (usually you don't need to) for this project
  18. .....
  19. For a different project...
  20. it has its own virtual .venv
  21. and has its own enviornment.
  22. ....
  23. Not only can you have different python libraries
  24. 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

  1. Creates a sub directory called .venv under your root folder
  2. 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?

  1. It lists all the current packages in this virtual environment
  2. And pipes them to a file called requirements.txt
  3. 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

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?

  1. It is really a simple idea.
  2. Don't confound it with the OS VMs that one refers to as virtual...
  3. It simply means this....
  4. .....
  5. 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.
  6. And the command line python will be run from that directory and library references will be resolved only from that directory.
  7. This keeps your project pretty isolated for its dependencies, so that it is easy to move the project to production or another machine.
  8. ....
  9. Thats it.
  10. It is not like a complicated VM.
  11. ....
  12. The mechanics how this is done is the detail.