venv Copypasta & Reminders Computers, Copypasta, Programming

venv Copypasta & Reminders

As a “digital native” but hobbyist/enthusiast with many other interests, I find it hard to remember simple processes like this, and just write them down somewhere easy to find.

venv Initialization Reminders

Note: These are for a Windows Powershell environment. Adjust for Linux/MacOS.

Navigate to the target project folder.

cd "D:\path\to\project"

Create the virtual environment. The second venv in this command is the conventional name for the environment folder.

python -m venv venv

Depending on how the local system and python installation, you may need to address permission/policy issues. Check and fix these if needed.

Activate virtual environment.

.\venv\Scripts\Activate.ps1

Ensure that pip is upgraded within the virtual environment. It uses a different pip than the system-wide python module library and is only upgraded on complete reinstall or update of the python interpreter.

python -m pip install --upgrade pip

For the modules needed in your project, create a requirements.txt in the project root, like the following example.

openai>=1.0.0
pydantic>=2.0.0

Install the modules from requirements.txt into the virtual environment.

pip install -r requirements.txt

To exit the virtual environment use:

deactivate

Restart it later with:

cd D:\path\to\project
.\venv\Scripts\Activate.ps1

to add/remove modules to the virtual environment later, from within the virtual environment, make the desired changes to requirements.txt and then run:

pip install -r requirements.txt
Search Titles & Keywords