Quick Note on venv: Python Virtual Environment Library

This is a quick note on how to use venv, a Python library for creating virtual environments.

  • Creating a virtual environment:

    python -m venv <path-to-virtual-environment>
    
  • Activating the virtual environment (for bash users. For other shells, refer to the documentation):

    source <path-to-virtual-environment>/bin/activate
    
  • Deactivating the virtual environment:

    deactivate
    
  • Since Python 3.3, a part of virtualenv was added to the standard library as venv.
  • By creating a virtual environment, you can develop programs in an isolated space without affecting your main environment.
    (Any external libraries installed via pip in the virtual environment won’t affect your main environment, allowing for risk-free testing of new library.)
  • It’s common to name the directory storing the virtual environment as .venv.
  • If you want to switch Python versions, consider using pyenv alongside.

Related Content