Difference between revisions of "How to virtual environments"

From HPC Wiki
Jump to navigation Jump to search
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:HPC-User]]
 +
 
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.
 
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.
 
+
<ol>
# '''Understanding Virtual Environments:'''
+
<li>'''Understanding Virtual Environments:'''
 
+
* '''Definition:''' A virtual environment is an isolated Python environment that allows you to install and manage dependencies separately for each project.
    * '''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.
    * '''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.
+
<li>'''Installing Virtual Environment Tools:'''
 
+
* '''Virtualenv:''' One of the most popular tools for creating virtual environments. Install it using pip: <code>pip install virtualenv</code>
# '''Installing Virtual Environment Tools:'''
+
* '''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: [https://conda.io/projects/conda/en/latest/user-guide/install/index.html Anaconda] or [https://docs.anaconda.com/free/miniconda/Miniconda Miniconda].
    * '''Virtualenv:''' One of the most popular tools for creating virtual environments. Install it using pip: `pip install virtualenv`.
+
<li>'''Creating a Virtual Environment:'''
    * '''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.
+
* '''Using virtualenv:''' <code>virtualenv myenv</code>
    * '''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).
+
* '''Using venv:''' <code>python -m venv myenv</code>
 
+
* '''Using Conda:''' <code>conda create --name myenv</code>
# '''Creating a Virtual Environment:'''
+
Replace `myenv` with the desired name for your virtual environment.
    * '''Using virtualenv:''' `virtualenv myenv`.
+
<li>'''Activating the Virtual Environment:'''
    * '''Using venv:''' `python -m venv myenv`.
+
* '''virtualenv or venv:''' <code>source myenv/bin/activate</code>
    * '''Using Conda:''' `conda create --name myenv`.
+
* '''Conda:''' <code>conda activate myenv</code>
 
+
After activation, your command line prompt should indicate the active virtual environment. Remember to add this line also to your job-script.  
    Replace `myenv` with the desired name for your virtual environment.
+
<li>'''Installing Packages:''' Use `pip` to install packages within the activated virtual environment.
 
+
* '''Use `pip` to install packages within virtualenv or venv:''' <code>pip install package_name</code>
# '''Activating the Virtual Environment:'''
+
* '''Use `pip` or `conda` to install packages within Conda:''' <code>pip install package_name</code> or <code>conda install package_name</code>
    * '''virtualenv or venv:''' `source myenv/bin/activate.`
+
<li>'''Freezing Dependencies:'''
* '''Conda:''' `conda activate myenv`.
+
* After installing packages, freeze the dependencies into a `requirements.txt` file with virtualenv or venv: <code>pip freeze > requirements.txt</code>
 
+
* Conda automatically manages dependencies in its environment and does not require a separate requirements.txt file.
    After activation, your command line prompt should indicate the active virtual environment. Remember to add this line also to your job-script.  
+
<li>'''Deactivating the Virtual Environment:'''
 
+
* To deactivate the virtual environment and return to the global Python environment:
# '''Installing Packages:''' Use `pip` to install packages within the activated virtual environment.
+
** '''virtualenv or venv''': <code>deactivate</code>
  * '''Use `pip` to install packages within virtualenv or venv:''' `pip install package_name`.
+
** '''Conda''': <code>conda deactivate</code>
  * '''Use `pip` or `conda` to install packages within Conda:''' `pip install package_name` or `conda install package_name`.
+
<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.
# '''Freezing Dependencies:'''
+
* Ignore the virtual environment directory (e.g., `myenv/`) to avoid cluttering the repository.
  * After installing packages, freeze the dependencies into a `requirements.txt` file with virtualenv or venv: `pip freeze > requirements.txt`.
+
<li>'''Updating Packages:'''
  * Conda automatically manages dependencies in its environment and does not require a separate requirements.txt file.
+
* Regularly update packages within the virtual environment:
 
+
** With virtualenv or venv: <code>pip install --upgrade package_name</code>
 
+
** With Conda: <code>conda update package_name</code>
# '''Deactivating the Virtual Environment:'''
+
<li>'''Cleaning Up:'''
  * To deactivate the virtual environment and return to the global Python environment:
+
* Periodically clean up unused packages and their dependencies:
      * '''virtualenv or venv''': Type `deactivate`.
+
** With virtualenv or venv: <code>pip autoremove</code>
      * '''Conda''': Type `conda deactivate`.
+
** With Conda: <code>conda clean --all</code>
 
+
</ol>
# '''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`.
 
 
# '''Cleaning Up:'''
 
  * Periodically clean up unused packages and their dependencies:
 
      * With virtualenv or venv: `pip autoremove`.
 
      * With Conda: `conda clean --all`.
 
 
 
 
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.

Latest revision as of 15:56, 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.

  1. 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.
  2. 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 or Miniconda.
  3. Creating a Virtual Environment:
    • Using virtualenv: virtualenv myenv
    • Using venv: python -m venv myenv
    • Using Conda: conda create --name myenv
    Replace `myenv` with the desired name for your virtual environment.
  4. Activating the Virtual Environment:
    • virtualenv or venv: source myenv/bin/activate
    • Conda: conda activate myenv
    After activation, your command line prompt should indicate the active virtual environment. Remember to add this line also to your job-script.
  5. 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
  6. 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.
  7. Deactivating the Virtual Environment:
    • To deactivate the virtual environment and return to the global Python environment:
      • virtualenv or venv: deactivate
      • Conda: conda deactivate
  8. 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.
  9. Updating Packages:
    • Regularly update packages within the virtual environment:
      • With virtualenv or venv: pip install --upgrade package_name
      • With Conda: conda update package_name
  10. Cleaning Up:
    • Periodically clean up unused packages and their dependencies:
      • With virtualenv or venv: pip autoremove
      • With Conda: conda clean --all

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.