Difference between revisions of "How to virtual environments"
Jump to navigation
Jump to search
Line 25: | Line 25: | ||
<li>'''Deactivating the Virtual Environment:''' | <li>'''Deactivating the Virtual Environment:''' | ||
* To deactivate the virtual environment and return to the global Python environment: | * To deactivate the virtual environment and return to the global Python environment: | ||
− | + | ** '''virtualenv or venv''': Type `deactivate`. | |
− | + | ** '''Conda''': Type `conda deactivate`. | |
<li>'''Version Control:''' | <li>'''Version Control:''' | ||
* Include the `requirements.txt` file (if using virtualenv or venv) or `environment.yml` file (if using Conda) in your version control system (e.g., Git) to ensure that all collaborators can recreate the same environment. | * Include the `requirements.txt` file (if using virtualenv or venv) or `environment.yml` file (if using Conda) in your version control system (e.g., Git) to ensure that all collaborators can recreate the same environment. | ||
Line 32: | Line 32: | ||
<li>'''Updating Packages:''' | <li>'''Updating Packages:''' | ||
* Regularly update packages within the virtual environment: | * Regularly update packages within the virtual environment: | ||
− | + | ** With virtualenv or venv: `pip install --upgrade package_name`. | |
− | + | ** With Conda: `conda update package_name`. | |
<li>'''Cleaning Up:''' | <li>'''Cleaning Up:''' | ||
* Periodically clean up unused packages and their dependencies: | * Periodically clean up unused packages and their dependencies: | ||
− | + | ** With virtualenv or venv: `pip autoremove`. | |
− | + | ** With Conda: `conda clean --all`. | |
</ol> | </ol> | ||
Following these best practices ensures a clean and organized workflow when working with Python packages via virtual environments. It promotes reproducibility, simplifies dependency management, and helps avoid compatibility issues across different projects. | Following these best practices ensures a clean and organized workflow when working with Python packages via virtual environments. It promotes reproducibility, simplifies dependency management, and helps avoid compatibility issues across different projects. |
Revision as of 14:15, 17 September 2024
One possiblity to get access to to python packages on your HPC system that are not installed sytem wide is the local installation into you own virtual environment.
- Understanding Virtual Environments:
- Definition: A virtual environment is an isolated Python environment that allows you to install and manage dependencies separately for each project.
- Advantages: Virtual environments prevent conflicts between different projects by keeping dependencies isolated. They also make it easier to manage dependencies and ensure reproducibility across different environments.
- Installing Virtual Environment Tools:
- Virtualenv: One of the most popular tools for creating virtual environments. Install it using pip: `pip install virtualenv`.
- venv (Python 3.3+): A built-in module in Python for creating virtual environments. No need to install separately, but ensure you're using Python 3.3 or later.
- Conda: A package manager, environment manager, and distribution of Python and other software packages for scientific computing. Install Anaconda or Miniconda from the official website: [Anaconda](https://conda.io/projects/conda/en/latest/user-guide/install/index.html) or [Miniconda](https://docs.anaconda.com/free/miniconda/Miniconda).
- Creating a Virtual Environment:
- Using virtualenv: `virtualenv myenv`.
- Using venv: `python -m venv myenv`.
- Using Conda: `conda create --name myenv`.
- Activating the Virtual Environment:
- virtualenv or venv: `source myenv/bin/activate.`
- Conda: `conda activate myenv`.
- Installing Packages: Use `pip` to install packages within the activated virtual environment.
- Use `pip` to install packages within virtualenv or venv: `pip install package_name`.
- Use `pip` or `conda` to install packages within Conda: `pip install package_name` or `conda install package_name`.
- Freezing Dependencies:
- After installing packages, freeze the dependencies into a `requirements.txt` file with virtualenv or venv: `pip freeze > requirements.txt`.
- Conda automatically manages dependencies in its environment and does not require a separate requirements.txt file.
- Deactivating the Virtual Environment:
- To deactivate the virtual environment and return to the global Python environment:
- virtualenv or venv: Type `deactivate`.
- Conda: Type `conda deactivate`.
- To deactivate the virtual environment and return to the global Python environment:
- Version Control:
- Include the `requirements.txt` file (if using virtualenv or venv) or `environment.yml` file (if using Conda) in your version control system (e.g., Git) to ensure that all collaborators can recreate the same environment.
- Ignore the virtual environment directory (e.g., `myenv/`) to avoid cluttering the repository.
- Updating Packages:
- Regularly update packages within the virtual environment:
- With virtualenv or venv: `pip install --upgrade package_name`.
- With Conda: `conda update package_name`.
- Regularly update packages within the virtual environment:
- Cleaning Up:
- Periodically clean up unused packages and their dependencies:
- With virtualenv or venv: `pip autoremove`.
- With Conda: `conda clean --all`.
- Periodically clean up unused packages and their dependencies:
Following these best practices ensures a clean and organized workflow when working with Python packages via virtual environments. It promotes reproducibility, simplifies dependency management, and helps avoid compatibility issues across different projects.