Python virtual environments - vsevolod-ivanov/Ivanov-Group-Wiki GitHub Wiki

Written by Makena Kaiman ([email protected])

What is a virtual environment?

A virtual environment, more specifically a Python virtual environment, is essentially a separate space where you can install Python packages and run code without affecting your system as a whole or interfering with other projects.

These environments become pretty useful as your tasks advance and we need to worry about separate calculations on VASP simulations.

Although a virtual environment lives in a directory, it’s different from just a regular folder. It contains its own copy of the Python interpreter along with an isolated location for installing packages.

Another important thing about virtual environments is that they must be activated. This is good because it keeps things separate, and you will always know when you have an environment activated, but you will also always have to remember to activate it before running code or installing packages and to deactivate it when you’re done.

Creating a virtual environment

  1. Navigate to your project directory.
  2. Create the environment using this command (replacing “myenv” with whatever name you want to call the environment):
python3 -m venv myenv

This will create a folder called myenv with an isolated Python environment.

Activating a virtual environment

Again, for either of these, you will replace “myenv” with whatever you named the environment you’d like to activate. Activation command for Linux/macOS:

source myenv/bin/activate

OR

source activate myenv

Activation command for Windows (PowerShell):

.\myenv\Scripts\Activate.ps1

To know that the activation worked, your terminal prompt will change, and you will see something like (myenv) at the start of the command line. This means Python will now use packages from that environment only.

To deactivate a virtual environment, simply type:

deactivate

Managing Python environments with Conda

Conda is a package and environment manager that lets you easily create isolated Python environments, install packages, and even manage multiple Python versions.

We like to use Miniconda, which is a mini version of Anaconda. Miniconda, unlike the larger Anaconda, just gives us Conda and Python, and lets us install only the packages we need. It helps keep the environments lightweight and more manageable, especially for beginner tasks in the group.

**Downloading Miniconda ** It’s best to just follow the steps directly on the Anaconda website, [linked here](https://www.anaconda.com/docs/getting-started/miniconda/install#mac-os). If you get stuck, there are lots of helpful resources online (I recommend Reddit 😄), and you can also reach out to another group member for help.

Pro tip: I recommend installing Miniconda in your home directory to avoid confusion and permission issues later. Make sure you keep track of where you install it, regardless, so you can find it easily in the future.

Creating an environment with Miniconda

  1. Navigate to your project directory.
  2. Use the conda create command, replacing myenv with whatever you want to call your environment and adjusting the Python version if wanted or necessary:
conda create --name myenv python=3.11
  1. Press "y" when prompted to confirm the package installation.

Activating an environment with Miniconda

The steps to activate an environment using Miniconda are pretty similar to activating a classic virtual environment, but you’ll notice a couple of differences.

  1. Initialize conda. Command:
conda init
  1. Restart your terminal or run the command:
source ~/.bashrc
  1. Activate your conda environment. Command (replacing myenv with whatever your environment is named):
conda activate myenv

Again, you’ll know if the activation was successful when your prompt line adds (myenv) to the beginning.