Exporting a Python Environment

To ensure consistency and reproducibility, it’s a good practice to export your Python environment into a requirements file. This file captures all the dependencies and their versions, making it easy for others to recreate the same environment.

Table of Contents

  1. For pip/PyPI
    1. 1. Activate Your Virtual Environment (if applicable):
    2. 2. Install pip-tools (if not installed):
    3. 3. Generate requirements.txt:
    4. 4. Review requirements.txt:
    5. 5. Share requirements.txt:
    6. Pin Specific Versions:
    7. Update requirements.txt regularly:
    8. Include Development Dependencies (Optional):
  2. For conda
    1. 1. Activate your conda environment (if applicable):
    2. 2. Export environment to YAML file:
    3. 3. Review environment.yml:
  3. Sharing conda environments
    1. 1. Include environment.yml in your repository:
    2. 2. To reproduce the conda environment:
    3. 3. Activate the new environment:
  4. Additional Considerations:

For pip/PyPI

1. Activate Your Virtual Environment (if applicable):

If you’re working within a virtual environment (e.g. mamba or venv), activate it using the following command:

1
source venv/bin/activate  # Linux/macOS
1
venv\Scripts\activate  # Windows

2. Install pip-tools (if not installed):

To simplify the process of managing requirements files, install pip-tools using:

1
pip install pip-tools

3. Generate requirements.txt:

Use pip freeze to generate a requirements.txt file containing all your project dependencies:

1
pip freeze > requirements.txt

4. Review requirements.txt:

Open the generated requirements.txt file in a text editor. It should look similar to this:

1
2
3
package1==1.0.0
package2>=2.1.0,<3.0.0
...

Ensure that all the necessary packages and their versions are listed.

5. Share requirements.txt:

Include the requirements.txt file in your project repository. Others can then recreate your Python environment using the following command:

1
pip install -r requirements.txt

This installs all the dependencies specified in the file. Additional Considerations:

Pin Specific Versions:

It’s recommended to pin specific versions of your dependencies. This ensures that others will use the exact same versions that you used during development.

Update requirements.txt regularly:

As you add or remove dependencies, remember to update your requirements.txt file. This keeps it synchronized with your actual environment.

Include Development Dependencies (Optional):

If your project has dependencies for development (e.g., testing libraries), include them in a separate file, such as requirements-dev.txt.

For conda

To ensure reproducibility, it’s recommended to export and share your Conda environment, especially if you are using Conda to manage your project dependencies. Follow these steps to export and share your Conda environment:

1. Activate your conda environment (if applicable):

If you’re working within a Conda environment, activate it using the following command:

1
conda activate myenv

2. Export environment to YAML file:

Use the following command to export your Conda environment to a YAML file (e.g., environment.yml):

1
conda env export --from-history > environment.yml

3. Review environment.yml:

Open the generated environment.yml file in a text editor. It should look similar to this:

1
2
3
4
5
6
7
name: myenv
channels:
  - defaults
dependencies:
  - python=3.8
  - other_dependency=1.2.3
  # Add other dependencies here

Ensure that all the necessary packages and their versions are listed.

Sharing conda environments

1. Include environment.yml in your repository:

Commit the environment.yml file to your project repository. This file will be used by others to recreate your conda environment.

2. To reproduce the conda environment:

Others can recreate your conda environment using the following command:

1
conda env create -f environment.yml

This command reads the environment.yml file and creates a new conda environment named myenv.

3. Activate the new environment:

Activate the new conda environment:

1
conda activate myenv

Now, the user’s Python environment should match yours, ensuring consistency across different machines.

Additional Considerations:

  • Update environment.yml Regularly: As you add or remove dependencies, remember to update your environment.yml file. This keeps it synchronized with your actual environment.

  • Include Conda Environment in Documentation: Mention in your documentation that users should refer to the environment.yml file for setting up the Conda environment.


Back to top

Copyright © BMBF Plattform MaterialDigital, 2021.