- I’m only taking down notes for this because I want to stop googling the same docs over and over again & eventually just memorize this lol
- Resource
venv
- Creates a “virtual” isolated Python installation, like a disposable workshop
- Allows you to manage separate package installations for different projects
- When you switch projects, you can create a new virtual environment isolated from other virtual environments
- DO NOT upload any
venvfolders to Github- Instead just upload a list of the packages your project needs
python3 -m venv .venv # (1)
source .venv/bin/activate # (2)
which python # (3)
deactivate # (4)
rm -rf .venv # (5)python3 -m venv .venv- Create a new virtual environment in a local folder named
.venv venvwill create a virtual Python installation in the.venvfolder
- Create a new virtual environment in a local folder named
source .venv/bin/activate- Activate the virtual environment before installing/using packages
- this allows you to put the virtual environment-specific
pythonandpipexecutables into your shell’sPATH - While a virtual environment is activated,
pipwill install packages into that specific environment
which python- Confirm if the virtual environment is activated by checking the location of the python interpreter
- If the virtual environment is active, the above command will output a filepath that includes the
.venvdirectory by ending with the following.venv/bin/python
deactivate- Deactivate it (switching projects, leaving ur virtual environment etc)
rm -rf .venv- Deleting the
venvfolder (common to delete them) - Bash commands
- Make sure your
requirements.txtfile is up to date
- Deleting the
📌venv vs docker
venv- Primarily focuses only on isolating Python dependencies and versions for a specific project
- docker
- Provides a containerization platform for packaging an entire application and its dependencies (including the operating system, libraries, and other software) into a self-contained unit called a container
- Much stronger isolation than
venv
- Basically (acc to this Stack Overflow post)
- With a Python
venv, you can easily switch between Python versions and dependencies, but you’re stuck with your host OS. - With a Docker image, you can swap out the entire OS - install and run Python on Ubuntu, Debian, Alpine, even Windows Server Core.
- With a Python
📌Why explicitly state python3 instead of python when creating venv?
venvmodule is explicitly apython3feature, it doesn’t exist on python 2- “Using the Python 3 interpreter, find its built-in
venvmodule and run it.”
- “Using the Python 3 interpreter, find its built-in
pip
- The reference Python package manager, used to install and update packages into a virtual environment
- If you
pip installsomething in thevenv, even if youdeactivateand reactivate it again, all the installations & dependencies will remain
Examples of using pip
python3 -m pip install requests # (1)
python3 -m pip install 'requests==2.18.4' # (2)
python3 -m pip install --upgrade requests # (3)
python3 -m pip install -r requirements.txt # (4)
python3 -m pip freeze # (5)
python3 pip freeze > requirements.txt
# Example of requirements.txt # (6)
requests==2.18.4
google-auth==1.1.0- 📌
-mflag- run a module as a script
-m pip install ...→ Use Python to run thepipmodule as a program
python3 -m pip install requests- Installing the
Requestslibrary
- Installing the
python3 -m pip install 'requests==2.18.4'- Installing with a specific version
python3 -m pip install --upgrade requests- Upgrade packages in-place using the
--upgradeflag (latest version & all of its dependencies)
- Upgrade packages in-place using the
python3 -m pip install -r requirements.txt- You can declare all dependencies in a requirements file instead of installing packages individually
- Requirements file
- files containing a list of items to be installed using
pip install
- files containing a list of items to be installed using
python3 -m pip freeze- export a list of all installed packages and their versions
pip freezecommand is useful for creating requirements files that can re-create the exact versions of packages installed in an environmentpython3 pip freeze > requirements.txt- You can save all the current requirements and put them in a requirements file
- Redirection - Manage Data Streams
Requirements.txt→ a requirements file- If you don’t state a specific version,
pipwill install the latest stable version it can find in PyPI (Python Package Index)- This is actually not recommended (breaking changes) so just pin the version
- If you don’t state a specific version,