An environment is sort of like a container that helps to keep dependencies required by different projects separate by creating isolated environments for them. This is one of the most important concept that most of the Python developers use.
Why do we need environment in Python projects?
Suppose you are working on two (or more) different tasks which might require different versions of specific python package. In these scenarios it is quite difficult to maintain other dependencies as well.
Environment helps us to install specific versions of packages and maintain dependencies in isolated manner.
There are various ways to keep your project dependencies isolated. Few of them are :
- Python virtual environment using virtualenv
- Conda environments using Anaconda
- Docker environment
(If you are aware of any other alternatives then please mention those in comments.)
In this article we’ll create environment using Anaconda (since anaconda is most popular choice for Python developers).
Step by step guide to create Anaconda environment:
One can create environment in Anaconda in 2 ways:
- Using Anaconda GUI
- Using command prompt
Let’s see how to create environment using Anaconda GUI:
Creating environment using Anaconda Navigator:
- Open Anaconda Navigator:
2. Click on Environments tab and then click on create
3. Type environment you want to create
4. and…Done ! It’s that simple.
When you simply click on your newly created environment, that will be activated.
Now you can go to Home tab, and install whatever tool you require.
Every time you want to work in your environment you have to launch your tool after activating the environment from environments tab.
You can also install packages in your environment from Environments tab.
Now let’s see how to create environment using command line.
Creating environment using Command Prompt
- Open command prompt and type (you can choose anyone from Anaconda prompt, command prompt or Windows powershell)
conda info --envs
This command is not mandatory step in creation but just to check the existing conda environments.
2. Create environment
conda create --name env_demo1
Type ‘y’ to proceed:
You can append your conda command with -y to avoid typing ‘y’ to proceed.
conda create --name env_demo1 -y
Your environment is now created. You can see the environment location below Package plan in the above image.
Also we can see commands to activate and deactivate the environment.
Along with environment creation command you can also specify python version or any other specific package. You can check commands for this on Anaconda’s official page. (Point 3, 4, 5 and 6)
3. Activating the environment
conda activate env_demo1
Now you have created conda environment but you won’t be having any packages in this other than default python packages.
This is the most tricky part and most of us make mistake here.
If you see the folder structure of our newly created environment, it will be like this (if you have created it without specifying python version or any specific command i.e. using simple #conda create –name <env_name>)
But if you see the folder structure of conda environment we just created using Anaconda navigator, it will be like this:
Why it is different?
Because, if you remember while creating environment using Anaconda Navigator we also checked Python and specified it’s version in the pop-up. (Step 3 in creation using Anaconda navigator)
But in creation using commands we have just created an environment.
You can check this by typing:
We don’t want to use any of these python executables but the one in our environment only.
To create python.exe for your environment:
conda install python=3.8
This will install python 3.8 in your environment. (Make sure you are entering all these conda commands after activating your environment or else all these changes will be made in base/root environment.)
Suppose you want to use jupyter-notebook in your conda environment and you have just created and environment using commands and you won’t bother to use the latest python version then it is not required to install python explicitly in your environment as conda resolves all the dependencies before installing any package.
conda install jupyter
After installing jupyter (or any other package) you will see your environment’s folder hierarchy has it’s own python.exe file
You can also check which python.exe is in use
You will get the list of all the available python.exe’s but it will use the first one by default.
Now, we have created environment using Navigator and command line. We have also seen how to get python executable of our environment.
You can launch your required tools like jupyter, spyder, orange etc. from command line as well Navigator. (I always prefer to use command line though there is almost nothing which you can’t do from Navigator and vice-versa).
Can't see your environment in jupyter kernel? Check my post here.
I hope now you will be able to create your conda environment. I will also try to cover other alternatives of creating virtual environment.
If you have any suggestion/ query then please let me know in the comments.