18

Anaconda: Start here for data science in Python!

 4 years ago
source link: https://towardsdatascience.com/anaconda-start-here-for-data-science-in-python-475045a9627?gi=8f914ce50413
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Using Anaconda for Python virtual environments

iuayqaz.jpg!web

Mar 31 ·8min read

aeAZvaB.jpg!web

Photo by National Cancer Institute on Unsplash

If you’ve been following me or have read a few of my articles, you must know that I am a big fan of Python Virtual Environments. I’ve written about this before as well which you can readhere and since then almost all of my projects include requirements.txt file for anyone else to easily replicate my work.

I’ve worked with several virtual environment managers but never really gave much attention to Anaconda. However, now I need to work with the conda environments for my daily college work. Here, the recommended way to work is with virtual environments and my fellow colleagues use Anaconda for them. So, I decided to give it a try!

Anaconda is just like any other virtual environment manager but it packs a lot more than just managing environments. It has its own Python package manager called conda and it supports pip as well. It is not limited to Python and can also work with other languages as well. But there are some caveats as well such as some packages can only be installed using pip and not conda . So, in this post, we’ll explore a bit about Anaconda and then go over some of the most useful commands you’ll need to become an expert in working with virtual environments.

What is Anaconda? Why use it?

Sounds absurd to name a project after a snake, but hey, we know that Python is a snake too **wink wink**

Anaconda is all about Data Science. It focuses on encompassing features and packages that aid a data scientist to have a workbench where he/she can do it all. It hosts several data science packages from the very start to enable data scientists to start quickly. With the added advantage of environments, it becomes an ideal starting ground for anyone who is driving towards their Data Science journey.

Sounds interesting! But why would you prefer it over other virtual environment managers? I’ve been using it for a while and here is what I found really intriguing about it:

  • No directory needed: Unlike others like virtualenv , you do not need to specify a certain directory where you want the environment to be set up. This enables you to activate the virtual environment anywhere on your system without worrying about the location.
  • Select any Python version: As long as a version of Python exists on the server, it does not matter if it is installed on your system or not. Anaconda’s conda can easily create the environment by grabbing the exact version of Python from the server.
  • conda package manager: While pip is great at managing Python packages, conda does a similar job at retrieving the packages and installing them in the environment. The added benefit is that it comes in bundled with the Anaconda distribution and makes it easy.

However, I still feel that conda is incomplete without pip . If you would try to install some packages via conda , it might give an error but the same package can be easily installed using pip .

Use both conda and pip while working with Python environments.

Note on Python3 and Python2

VZraquR.jpg!web

Photo by Kelly Sikkema on Unsplash

Python2 is no longer being developed and all of the development efforts are being targeted towards Python3. However, several packages and projects still rely on Python2, so it’s a good practice not to lose touch with it. Check the project below which is based on Python 2.X and creates some phenomenal artwork.

This article works for both Python2 and Python3 as it would only essentially differ in selecting the Python version while setting up your environment but we’ll work with Python3 distribution for now.

Start any new project in Python3 to be future proof.

Installing Anaconda

If you’ve installed any software before, you’ll feel right at home. Go here , select your operating system (I’m using a Mac) and then select the Python 3.x version Download button which will download the installer to your machine.

ERZRniR.png!web

Select “Download” button below Python 3.7 version

Then, open the installer on your machine and follow along with all the steps, while reading and agreeing with the agreement. You do not need to change anything as everything gets set up all by itself and once it finishes, you’re all set to use Anaconda on your machine.

Working with Python packages

mumaYbq.jpg!web

Photo by Debby Hudson on Unsplash

As I mentioned before, the Anaconda package comes with conda manager which shall allow you to install packages, create and activate virtual environments and a lot more.

Open terminal

I use iTerm2 as my main terminal which I have designed based on how I like. You can create a similar (or even better) view of the terminal by following a guide I posted on Medium.

You will notice that the terminal now has (base) written in front of the computer name. It means that your base conda environment is set (meaning you’re working globally for the whole user and not a specific environment).

Ub6BveR.png!web

(base) in front of the command

I’ll first describe a few commands in this base environment but they work inside any particular environment as well. We will see how to set up and work with environments later down the article.

View installed packages

One of the most basic commands is to know the list of all installed packages inside your environment. Note that Anaconda comes with many packages built in, so you’ll see a lot more packages than you expect. The command is as follows:

conda list

MZNbQvQ.png!web

List of installed packages

Search and install a package

conda is very intuitive because what you want to do is exactly what the command will probably look like. Let’s say we want to install numpy but we do not know the version. We can use the following command to search for its versions:

conda search numpy

vyiay2A.png!web

Search for a package in conda

Let’s say we plan to install numpy with version 1.18.1 , we’ll use the following command to do so:

conda install numpy==1.18.1

QrQ3Urj.png!web

Installing numpy version 1.18.1

Remove package

Suppose in a certain case, you no longer need a particular package, or you installed a wrong version, you can simply use the following command to remove it. Let’s do it with numpy .

conda remove numpy

BV36vmB.png!web

Removing numpy

Working with Python environments

V7zmEjq.jpg!web

Photo by Calvin Hanson on Unsplash

conda also enables you to create, activate and deactivate virtual environments as needed. All these environments are isolated from one other and can host very different combination of packages and package versions without interfering with one another.

Create virtual environment

A virtual environment can be created with the following command with an option to directly install a few packages while creating the environment:

conda create -n env_name python=3.7.6 <list_of_packages>

The word after -n becomes the name of the environment. In our case, it is env_name . We then specify the version of python as defined above. We can then specify a list of packages that we want to install while creating the environment. This means the packages will be listed in place of <list_of_packages> . For example, to create the environment and install numpy and pandas we will use the following command:

conda create -n env_name python=3.7.6 numpy pandas

RNJre2i.png!web

Create virtual environment named “env_name”

Activate the environment

It’s very easy to activate the environment. Simply use the following command:

conda activate env_name

If you happen to use a prior version of conda , you will need to use the command source activate on Linux/MacOS or activate on Windows.

bYrARvi.png!web

Activating the environment

As we can see in the image above, the name of the environment precedes the command line indicating that we are inside the environment.

Now, we can install packages as we need using conda install or pip install and they will not interfere with any packages outside this environment.

Deactivate environment

Deactivating is equally simple. Use the command

conda deactivate

Again, if you are on a prior version of conda use source deactivate on Linux/MacOS or deactivate on Windows.

imu6reJ.png!web

Deactivating the environment

You’ll notice that the text in the front changes back to (base) indicating that we are no longer in our environment.

Share environments

One of the prime reasons (apart from managing isolated environments) for using environments is the ability to share the exact Python packages with their exact version with others. conda uses YAML files to share the environment information in contrast to requirements.txt generally used with pip . To export all packages, whether installed via conda or pip , we use the following command inside our environment:

conda env export > environment.yml

The file will look something like the image below:

Uju6z2B.png!web

environment.yml

I recommend that we also create the requirements.txt file so users who do not use conda can still use our environment configuration. For this, use the command inside the environment:

pip freeze > requirements.txt

You can simply share the files environment.yml and requirements.txt with your project for easy replication.

Once you have a environment.yml file with you, you can simply use it while creating the environment and set it up all ready to be used like below:

conda create env --file 
 environment.yml

List all environments

When you work on multiple projects, you create many environments. You might forget the environments that you might already have created. There is a quick command to view all available environments:

conda env list

AZf6byn.png!web

List of all environments

As you can see, I have quite a few environments with a special base which we normally have.

Remove environment

Once you no longer work on a project, you might want to remove its environment as well. Use the command to do so:

conda env remove -n env_name

Here, env_name should be replaced by the name of the environment being deleted. I’m deleting env_name so I’ll keep the same.

Rzu2Mb6.png!web

Removing “env_name” environment

Conclusion

In this article, we explored what is Anaconda and how it is better than other virtual environment managers. We explored ways to use conda to work with packages as well as create and run Python virtual environments.

You can also refer to the conda cheatsheet once you get familiar with the basics in this article.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK