Anaconda: Start here for data science in Python!

栏目: IT技术 · 发布时间: 4年前

内容简介: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 includeI’ve worked with

Using Anaconda for Python virtual environments

Anaconda: Start here for data science in Python!

Mar 31 ·8min read

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

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.

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

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).

Anaconda: Start here for data science in Python!

(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

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

Removing numpy

Working with Python environments

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

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.

Anaconda: Start here for data science in Python!

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.

Anaconda: Start here for data science in Python!

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:

Anaconda: Start here for data science in Python!

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

Anaconda: Start here for data science in Python!

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.

Anaconda: Start here for data science in Python!

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.


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大型网站技术架构演进与性能优化

大型网站技术架构演进与性能优化

许令波 / 电子工业出版社 / 2018-6 / 79

《大型网站技术架构演进与性能优化》从一名亲历者的角度,阐述了一个网站在业务量飞速发展的过程中所遇到的技术转型等各种问题及解决思路。从技术发展上看,网站经历了Web应用系统从分布式、无线多端、中台到国际化的改造;在解决大流量问题的方向上,涉及了从端的优化到管道到服务端甚至到基础环境优化的各个层面。 《大型网站技术架构演进与性能优化》总结的宝贵经验教训可以帮助读者了解当网站遇到类似问题时,应如何......一起来看看 《大型网站技术架构演进与性能优化》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具