Skip to main content

Create Python environment (conda, venv)

Conda environments

IMPORTANT:

Anaconda and miniconda both use the conda command on the terminal, so you can install one or the other. Do not install both, since that creates conflict on your terminal.

Once you have installed either anaconda or miniconda on your computer, if you are using MacOS you should be able to see the (base) tag on your terminal. On Windows, you can use the Anaconda Prompt terminal. If you only type conda and click Enter you will see all of the commands you have for conda. One of them is create

conda create -n name_environment python=3.10 -y

In this line of code is important to note:

  • [-n or --name]: is the flag necesary to name the environment.
  • [name_environment]: Next we have the name of the environment. You can name it whatever you want, just remember to not name it the same as other environments, since they will rewrite themselves. Also, do not name it python or any python library name, since that also causes conflicts. Lastly, they have to be named a single word (if you want a long name, you will have to hypenate it)
  • [python=3.10]: This part is where you specify the python version you need for your environment. If you do not add this, it will create the issue of python version 0.0.0. Check the current supported versions of python to check which one you need.
  • [-y] is just to agree to create the environment. If you do not add it, it will not do it automatically, and you will have to agree to it in the next step.

After you create the environment (it should take only a few seconds to complete, if not, check your internet connection) you can activate and start installing your libraries.

# To activate the environment
conda activate name_environment

# To deactivate
conda deactivate

To show that you are out of your (base) environment, you should see it change to (name_environment)

Troubleshoot 1:

When you start installing your libraries, with conda environments you can either use the conda command or pip. We recommend prioritazing pip for installing libraries, since its the official Python package manager, and it creates less bugs.

Please do not install anything on the base environment, since that would create a lot of conflicts on your library versions. See our environments explanations to find more about it.

Troubleshoot 2:

If you have problems of compatibility with your current environment python version, its way easier to just create a new environment with the python version you need.

Python environments (venv):

For this type of environment, the only requirement for your computer is to have a version of Python installed on it. venv is a Python module that supports lightweight virtual environments.

NOTE:

If you have not yet installed Python on your computer, we strongly recommend you install it via distribution software manager. This way of installing python is outdated.

For this type of environment, you need to be familiar with how the terminal works, how you can move from one folder to another, and the Python versions you have installed. If you are unfamiliar with these requirements, please refer to the How to use Anaconda section.

From Python 3.3 onwards, venv should be included in the commands available. To create a virtual environment with this, please open your terminal:

macOS

To enter the terminal, you can search it directly from the Launchpad or application folder. Type Command + Space bar and type terminal for a shortcut. First, we must ensure you are in the folder where you want to save the environment. When you open the terminal, you should see only your user name:

Python_terminal

For this example, I am going to access my Documents folder. You can access whatever folder you wish to save your environment on.

WHY IS THIS IMPORTANT?

If you save a virtual environment with the same name in the same folder, the terminal will interpret it as you want to rewrite it, and you will lose the information from the previous one. Before creating new environments, make sure that the name and folder you choose differ from previous ones.

python -m venv [name of the environment]

Inside of the brackets, you can change it to whatever name you want. Just make sure that the name of the environment is something easy to remember, or write it down somewhere. The name should also follow the terminal rules: if you are going to name something with more than one word, you need to hyphenate the words with an underscore (_).

  • Example: python -m venv example_environment.

The way you activate it is while inside the folder where you created the environment, call source [name of the environment]/bin/activate. The name in front of the dollar sign should change to the name of the environment you are currently in.

  • Example:
Captura de Pantalla 2024-02-13 a la(s) 9 29 28 a m

WINDOWS OS

For the Windows OS you also need to have previously installed Python.

IMPORTANT:

If you installed Python by downloading the installer directly from the Python page, you might need to add the path to the environment variables of your computer. Please see the section on how to do that in the "Add path to environment" section. If you are not familiar with the path and what it means to add it to the environment variables, please continue with the next steps.

If you have Python already in your Path variables, you can just use the same arguments as the macOS instructions.

  • Example: python -m venv example_environment

Please note that the same rules apply to Windows, so make sure to name the environment something unique and easy to remember, and also select the correct folder for your environment.