Skip to content

Python Tips and Tricks

Bill Hereth edited this page Apr 16, 2024 · 2 revisions

Cloning Python Environments

Why create an established virtual environment?

Python software and libraries are constantly upgrading. The combinations of these items that were installed together yesterday, may not install nicely today. Recreating a working modeling environment could take hours. Why recreate a modeling environment entirely from scratch, when you can automatically install it using a “recipe”? Creating an established environment is not only great documentation, but also allows you to quickly reinstall modeling environments.

If at least some of the environment was built using Conda:

  • Open Anaconda Prompt
  • Run the following commands:
    • conda activate myEnvironment
    • conda env export > environment.yml
    • conda env create -n new-env-name -f environment.yml
  • Conda will create a new environment, with the new name, same python version, and same libraries

If only pip was used:

  • Use the command line tools or sys library in python to get the version number of the environment you will be copying (e.g. 3.9.19)
  • Open Command Prompt
  • Change directory to the desired python scripts folder
  • Run the following commands:
    • pip freeze > requirements.txt
  • Create or download the new python environment with the desired version number
  • Change directory to new python environment
  • Run the following commands:
    • pip install -r requirements.txt

Suggestions:

Keep a copy of the environment.yaml or requirements.txt in the modeling repository (Optional) Add the name of the python version to the environment.yaml or requirements.txt file e.g. remm_environment_v3_9_19.yaml

Using Requirements.txt

Create conda env documentation

How to install Conda from yaml