![[Formales]] ![[venv_666737877.png]] [Creation of virtual environments](https://docs.python.org/3/library/venv.html)<br> In: *Python Documentation* > [!Note] Purpose of a VENV >> [!multi-column] >> >>> [!NOTE] Dependency Management >>> It allows for installing and managing package dependencies specific to a project without affecting the global Python environment or other projects. This way, different projects can use different versions of the same package without conflict. >> >>> [!NOTE] Environment Consistency >>> By isolating dependencies, virtual environments ensure that projects run consistently across different machines and deployment environments, such as development, staging, and production. This reduces the "it works on my machine" problem. > >> [!multi-column] >> >>> [!NOTE] Simplified Setup >>> New developers on a project can set up their development environments quickly and accurately by creating a new virtual environment and installing the required dependencies from a `requirements.txt` file. >> >>> [!NOTE] Platform Compatibility >>> Virtual environments help manage dependencies and project setups across different operating systems and Python versions, making it easier to maintain platform compatibility. > >> [!multi-column] >> >>> [!NOTE] Risk Reduction >>> Virtual environments reduce the risk of system-wide issues or conflicts between project dependencies, as changes are localized to the virtual environment. >> >>> [!TLDR] Overall >>> Virtual environments are a foundational tool for Python development, promoting clean, manageable, and reproducible project environments. # Creating a Virtual Environment > [!TIP] Starting Point > Folder where you wish to create the VENV. > Best Practice: Keep all VENV-Subfolder in one root folder. > Make Visual Studio Code (VSC) known to the root folder. VSC will then find your activated VENV without any problems. ## Windows (bash) Windows is sometimes tricky. Ensure you have installed all Windows updates and are using the latest version of *bash*. ``` python -m venv myenv ``` ## Mac OS, Linux (zsh) ``` python3 -m venv myenv ``` # Activating a Virtual Environment ## Windows (bash) Windows is sometimes tricky. Ensure you have installed all Windows updates and are using the latest version of *bash*. ``` .\myenv\Scripts\activate ``` ## Mac OS, Linux (zsh) ``` source myenv/bin/activate ``` # All OS > [!TIP] Starting Point > Terminal with activated VENV ## Deactivating a Virtual Environment ``` deactivate ``` ## Installing Packages in a Virtual Environment ``` pip install package_name ``` ## Listing Installed Packages ``` pip list ``` ## Freezing Dependencies To create a **requirements.txt** file listing all installed packages and their versions: ``` pip freeze > requirements.txt ``` ## Installing Packages from a Requirements File ```bash pip install -r requirements.txt ``` # Deleting a Virtual Environment To delete a virtual environment, simply remove the environment directory after deactivating it. ## Windows (bash) Windows is sometimes tricky. Ensure you have installed all Windows updates and are using the latest version of *bash*. ``` rmdir /s /q myenv ``` ## Mac OS, Linux (zsh) ``` rm -rf myenv ``` # Let VSC know where your VENV Root Folder is Open *Settings* within VSC and search for *venv*—copy and paste your path into VSC as shown below. ![[venv_vsc.png]] > [!ATTENTION] Work allways within your VENV > Suppose you aren't in your VENV while installing sth. Via PIP, then you mess up your locally installed Python. It will work as before but isn't clean anymore. # VENV in a Docker Container? ![[venv_in_docker__660146698.png]] > [!TLDR] VENV in Docker is not necessary > - Whether to use a virtual environment within a Docker container depends on your specific project needs, workflow preferences, and the complexity of your application. > - Adding a virtual environment might be unnecessary for simple applications where Docker provides sufficient isolation. > - However, for complex projects or when maintaining consistency with existing development practices is crucial, using a venv inside Docker can be advantageous. ## Pros ### Consistency with Development Workflow If your development workflow heavily relies on virtual environments, using them within Docker can keep your development and production environments more consistent, easing the transition between local development and containerized deployment. ### Dependency Isolation While Docker isolates your application and its dependencies from the host and other containers, a virtual environment within Docker can add an extra isolation layer for Python dependencies. This can be useful in complex projects where you want to experiment with or upgrade dependencies without affecting the entire container's setup. ### Familiarity and Convenience For teams already accustomed to Python virtual environments, continuing to use them inside containers can leverage existing knowledge and practices, such as activating environments and managing dependencies via **pip**. ## Cons ### Additional Overhead Adding a virtual environment inside Docker introduces extra steps in the Dockerfile (e.g., creating and activating the venv), slightly increasing the build time and complexity of the Docker image. ### Redundant Isolation Docker already provides isolation by packaging an application and its dependencies into a container. Adding a virtual environment inside a container might be considered redundant since one of the primary benefits of venvs—dependency isolation—is inherently provided by Docker. ### Resource Usage While the additional resource usage is generally minimal, there's still some overhead in maintaining an extra layer within the container, especially if you're running multiple containers and are very sensitive to optimizing resource usage.