Start Python in Virtual Environment (virtualenv)

Start Python in Virtual Environment (virtualenv)

by flandersen


Posted on February 23, 2019


Virtual environments in Python are used to decouple the dependencies of multiple python projects by putting them into a project sub-directory. It is not to be confused with Virtual Machines, which can isolate code execution from the host system, but in contrast require a separate operating system and a lot more system resources.

# goto project directory

cd my_project

# create virtual environment

$ python -m venv .venv

# activate the virtual environment (linux)

source .venv/bin/activate

# activate the virtual environment (windows powershell)

.venv/Scripts/Activate.ps1

# create a file named requirements.txt

echo "requests==2.18.3" >> requirements.txt
echo "beautifulsoup4==4.6.0" >> requirements.txt
echo "isoweek==1.3.3" >> requirements.txt

pip install -r requirements.txt

The dependencies can now be found in the sub-folder .venv.

For further informations about using virtual environments in Python:
https://packaging.python.org/guides/installing-using-pip-and-virtualenv/