Virtual Environment

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments.

Resources:

This way, you don’t run into those stupid dependencies issues. Another way to fix that would be using Docker, but that’s for another time.

if you just use virtualenv, you can run

virtualenv venv
 
# If the above doesnt work, do this
python3 -m virtualenv venv
 
# To activate the virtual environment
source venv/bin/activate

To activate in windows, use

C:\> <venv>\Scripts\activate.bat

Folder structure

venv/
β”‚
β”œβ”€β”€ bin/
β”‚   β”œβ”€β”€ Activate.ps1
β”‚   β”œβ”€β”€ activate
β”‚   β”œβ”€β”€ activate.csh
β”‚   β”œβ”€β”€ activate.fish
β”‚   β”œβ”€β”€ pip
β”‚   β”œβ”€β”€ pip3
β”‚   β”œβ”€β”€ pip3.10
β”‚   β”œβ”€β”€ python
β”‚   β”œβ”€β”€ python3
β”‚   └── python3.10
β”‚
β”œβ”€β”€ include/
β”‚
β”œβ”€β”€ lib/
β”‚   β”‚
β”‚   └── python3.10/
β”‚       β”‚
β”‚       └── site-packages/
β”‚           β”‚
β”‚           β”œβ”€β”€ _distutils_hack/
β”‚           β”‚
β”‚           β”œβ”€β”€ pip/
β”‚           β”‚
β”‚           β”œβ”€β”€ pip-22.0.4.dist-ino/
β”‚           β”‚
β”‚           β”œβ”€β”€ pkg_resources/
β”‚           β”‚
β”‚           β”œβ”€β”€ setuptools/
β”‚           β”‚
β”‚           β”œβ”€β”€ setuptools-58.1.0.dist-info/
β”‚           β”‚
β”‚           └── distutils-precedence.pth
β”‚
└── pyvenv.cfg

PyCharm Virtual Environment

In PyCharm, it is a bit different. We configure it within PyCharm, it will create a folder called venv/ instead of env/. (on Windows)

To activate the virtual environment, just run

# Windows
./venv/Scripts/activate
 
# Mac
source venv/bin/activate

Install packages in virtual environments

Once you are in the virtual environment, you can install the right packages. Ex: pip install speechRecognition

Export to requirements.txt

Finally, once you are done installing all of the relevant packages, you can simply run

pip freeze > requirements.txt

This will be how other users install the relevant Python packages.