Start Python in virtual environment

Start Python in virtual environment

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, though, which isolate a complete guest operating system from a host system.

# goto project directory

cd my_project

# create virtual environment

$ python3 -m venv .venv

# activate the virtual environment

source .venv/bin/activate

# 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/