Virtual environment quick start guide with Anaconda.
What’s the point of having a virtual environment? In short, it is meant to isolate your installed modules/packages such that to avoid any conflicts with other modules/packages. Some packages may be incompatible due to their versions or other underlying issues. So it is smart to avoid any possible conflicts.
For example, when you start a new project for Data Analysis and Machine Learning most likely you’ll use packages such as Pandas, NumPy and Scikit-Learn. Each one comes with its own dependencies and it may be possible that you’ll encounter conflicts between some packages.
A virtual environment allows you to install only the packages that you need and nothing you don’t. It’s kind of like having a desk with only the book, notebook, and a pen that you need. Nothing extra and no distractions.
You can easily manage your virtual environments with Anaconda. Using the Anaconda Prompt (command line) will give you control over which packages and versions that you want to install for your project. You can access the Anaconda Prompt in a couple of ways.
You can launch the Navigator and select the ‘Environments’ tab on the left. Then, select
base (root) and press play.
Alternatively, you can search your system for the Anaconda Prompt as shown below.
Once opened, we can begin. It automatically starts in your “base” environment.
Create a new virtual environment with some name, <VENV-NAME>.
conda create --name <VENV-NAME>
Additionally, you may want to specify what to install.
conda create --name <VENV-NAME> python pandas numpy scikit-learn
The above commands will download and install the latest version of all specified packages and their dependencies.
conda activate <VENV-NAME>
Once you have activated your environment, you can install additional packages or IDEs like Jupyter Lab, Jupyter Notebook, Spyder, etc. Once your IDE is installed you can launch it from the command line as well.
For example, you can install Spyder IDE (if you haven’t yet):
conda install spyder=4.0.0
After it’s installed, you can launch with a simple command:
By using the command line instead of the Anaconda Navigator you actually save some of your precious RAM.
New Environment from File
There is a more efficient way of creating a new environment, and that is to create a yml file using any text editor.
Create your yml file in any text editor and save it with the .yml extension, then you can use it to create a new virtual environment. Now, let’s create a new environment using the YML file. Note, you need to navigate into the same directory as your YML file.
This will create a new environment with the chosen name and specified packages will be installed. From your command line:
conda env create -f new-env.yml
Your virtual environment is now created and the specified packages installed. Now you just have to activate it.
At some point, you may want to export your environment to a file and push to Github to share with others so they can replicate your work.
conda env export > environment.yml
This will create a new YML file with all installed dependencies listed and their versions. This is very useful if/when, at some point in the future, you find that some new package that you installed is not be compatible with some of the currently installed packages. You export environment file serves as a backup and enables you to essentially restore a working virtual environment with no issues.
You are now able to quickly setup your own virtual environment using Anaconda Prompt. There is more learn but this will get you going. Remember to create a new environment for every project. This is a necessary practice if you want to share your work with others so it can be replicated. It also serves as a backup when you encounter conflicts between packages. Just remember to install only what you need for your project and nothing extra.
Thank you for reading.
Below are some useful links to further your knowledge.