Introduction to Python environments (using Conda) (2023)

Introduction to Python environments (using Conda) (1)

Whether you want one or have no idea what it is, you're going to have to deal with it.
Finally, environments in Python. If you are a python newbie like me
or you're new to general setup of your workspace for any programming language, you may have dealt with the pain of setting up your environment for the first time.

Between the lack of knowledge of what you're doing and why it's necessary, the many different guides online that may not contain exactly the same instructions, and numerous Stack Overflow posts that help in very specific situations, I found it confusing to understand exactly what environments they were, when they were needed, and how to configure mine. This guide is intended to help build an intuition for environments and provide some examples of how to deal with them, and in particular how to use the mine, the package manager forhe liked it.

Introduction to Python environments (using Conda) (2)

Piton, like many other programming languages, has different versions. And sometimes when we build software, the software needs to run in a specific language version because our software expects specific behavior that is present in older versions but changes in newer versions. Likewise, we may need to use specific versions of the libraries for similar reasons. But we can have many projects on our computer, maybe oneBottleThe app runs on version 0.11 (the first one you created!) andPython 2.7and even more modernBottleApplication running on version 0.12 and higherPython 3.4. When I try they both start at the same timepython 2opython 3, one of them may get corrupted due to some code being executedpython 2Does not continuepython 3Or vice versa. This is where virtual environments come in handy.

(Video) Anaconda (Conda) for Python - What & Why?

Virtual environments keep these dependencies in separate "sandboxes" so you can easily switch between both applications and get them working. For those more familiar with programming, virtual environments are analogous tostevedoreContainer. Additional package manager for other languages, such as JavaScriptMNP(Node Package Manager), will take care of most of these details for you, but you'll have to get your hands dirtyPitonand deal with the environment yourself.

Introduction to Python environments (using Conda) (3)

There are several ways to create an environment, including using virtualenv, venv (built into the Python 3 standard library), and conda.
the associated package managerhe liked it. There are some arguments
Why should you choose Conda?virtual environmentas described in myth #5this blog post, but I will only focus on the usein the minein this guide because it's a popular tool for data science, which is what I'm focusing on now.
This guide assumes that you have already done so.he liked itoMinicondaFurnished; All instructions are also on the bash command line.
For reference, I'm running my commands in the terminalMac OS X.

To quickly create an environment, usein the mine, you can enter the command:

conda create --name your_env_name python=3.7 -y

In this command, the 'Python=3.7' part indicates which version of Python
I want to set the environment to; You can change the version according to your needs. In other snippets you see online, you can see "-NORTE' rather '- Name'; they mean exactly the same thing. He '-y' essentially tells the command line to say "yes" to all subsequent requests; It's not strictly necessary, but it will save you a little trouble.

conda create --name your_env_name python=3.7 scipy=0.15.0 astroid babel

He 'create countiesThe command effectively loads all the packages at once, which is better than loading them one at a time, as it can cause dependency conflicts. You could then add all the packages you need manually, but that can be tedious if you have a lot of packages;
It would also require a lot of typing on the command line
and swiping can make you retype the command. Worse yet, the command may not remain in your shell history, and if you want to recreate the exact same environment in the future, it would be
very tedious, if not difficult.

If, for these or other reasons, you do not want to create the environment from the command line, you can use aYAML(YAML Ain't Markup Language) which behaves like a configuration file.
They areYAMLThe file could look like this:

(Video) Anaconda Beginners Guide for Linux and Windows - Python Working Environments Tutorial

Nombre: your_env_name
- Pre-adjusted
— certificates-ca=2018.03.07=0
Prefix: /Users/your_username/anaconda3/envs/your_env_name

If this file was called:Environment.yml', then you could create the environment with the following command:

conda env create -f environment.yml

He '-F' flag means file and the filename of theYAMLfile must
Immediately follow the '-F' bandera.

That's great if you can make oneYAMLfile and you know all the packages you need. But what if you had an existing environment that you wanted to duplicate? Perhaps you want to duplicate the application on another server and want the exact same configuration for consistency. If this is the case, you can run the following command.

conda env export > my_environment.yml

The greater than symbol ">" indicates that the output is writing
in a file called 'my_environment.yml'. If there was any content.
In 'my_environment.yml'before this command would be overwritten.

As a note, the mineyou need oneYAMLArchive; if you choose to use itvirtual environment,
A txt file would also suffice for everything done here, butin the minespecifically need aYAMLArchive.

Introduction to Python environments (using Conda) (4)

Now that you've created an environment and assume you're using conda, let's quickly verify that it exists with the command:

(Video) Anaconda Tutorial 2019 - Python Virtual Environment Manager

conda info --envs

This command should display the current environments, which might look like this:

practice /users/your_username/anaconda3/envs/practice
base /users/your_user/anaconda3
your_env_name /Users/your_user_name/anaconda3/envs/your_env_name

Now that you've confirmed that you've created the environment, you're ready to use it. We can achieve this by writing (assuming your environment is below your base):

habilitar conda your_env_name

At this point, your terminal prompt should look like this:

(your_env_name) Your_Machine:your_directory nombre de usuario$

If for some reason this command does not result in a similar output,
You can specify the full path by typing something similar to the following command:

conda active /Users/your_username/anaconda3/envs/your_env_name

If, like me, you want to know exactly what happens when you type the word "activate", you run a bash script that resides in a subdirectory of the environment.
In my case the file path to the script looked something like this:


To stop using the environment, enter

konda disabled

Similar to the Activate command, the Deactivate command executes a function from the Activate bash script.

(Video) Master the basics of Conda environments in Python

If you want to update the environment, type:

conda env update –f environment.yml –n your_env_name

If you want to get rid of the whole environment, just type:

conda remove --name your_env_name --all

He '- in' flag is to remove all packages from the environment and
it is necessary to completely clean the environment.

As a quick overview, this guide covers how to create your virtual environment by using packages both on the command line and within aYAMLfile, how to enter and exit the virtual environment, how to update it, and how to delete it when you no longer need it.

At this point, you should know enough to independently configure your environment as you see fit, and have enough context as to why you should create a virtual environment. While this guide did not go into detail about the differencesvirtual environment,venv, Yin the minevirtual environments, I have provided some links below to help you get started.

Please let me know if you have any questions or suggestions on how to improve this.


1. Python Tutorial: Anaconda - Installation and Using Conda
(Corey Schafer)
2. Managing Python Environments with Conda
(Mark Jay)
3. Create Python Environments Using conda
(Fluidic Colours)
4. Anaconda Environments - What you need to know.
(Python Programmer)
5. Introduction to Python Virtual Environments Using Anaconda
(Data Science Dot py)
6. The only CONDA tutorial you'll need to watch to get started
(Coding Professor)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated: 06/05/2023

Views: 5827

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.