Source: How to Use Virtual Environments with the Built-In venv Module- Corey Schafer
in cmd, type mkdir project_name, then enter your project directory
In cmd, enter: python -m venv venv
In cmd, enter: python -m venv venv --system-site-packages
Legend:
- -m: searches for a module of specified name after -m
- venv: module name
- venv: create a folder with this name and put a virtual environment inside
- system-site-packages: copies all system libraries to this venv. Any new library you install in this env is still available locally only, not affecting the system.
Then run venv\Scripts\activate.bat to activate virtual environment.
Just type deactivate.
You can create virtual environment anywhere and activate it and work on your project code anywhere. But by convention, you enter your project directory and create and activate virtual environment inside it.
By default, you name the venv as venv.
You do not put any of your project files inside venv. Your project files and venv should be at the same directory level. i.e. Immediately inside Project folder.
where python tells current python location at the topmost of python installation locations.
You'll also see (venv) at the start of every command line as long as virtual environment is active.
pip list only lists packages installed in this environment.
pip install requests installs requests in the virtual environment.
pip freeze gives a list of these packages which you can copy and paste into project folder in a requirements.txt file. The list may be longer than what you installed, cuz even dependencies of packages are installed when you install requests.
You could also redirect the output of pip freeze using pip freeze > requirements.txt.
pip list --local for only local packages installed in this venv. Same logic for pip freeze --local.
Make and activate your own venv.
In cmd: pip install -r requirements.txt where the requirements.txt is full path to the file.
rmdir folder_name /s will delete folder_name and also delete subfolders thanx to the /s
cls
Corey Schafer Windows VSCODE guide
You only commit the requirements.txt file, not the venv itself. So in .gitignore,
- add the folder name for 'venv'.
- add '.vscode' for vscode related files.