The most complete Python package management tool in history: Anaconda tutorial

e760264f8573f2953a92ff9c83ced5c4.jpeg

In fact Anaconda and Jupyter notebook have become the standard environment for data analysis.

Simply put, Anaconda is a package manager and environment manager, and Jupyter notebook can combine code, images, and documents for data analysis into one web document.

Next, I will introduce Anaconda in detail, and give the Jupyter notebook at the end:

1. What is Anaconda?

2. How to install?

3. How to manage packages?

4. How to manage the environment?

5. How to quickly get started with Jupyter notebook?

But before I start, I need to emphasize that you have to follow the steps below and practice them on your own computer. Although you will encounter many commands below, no one can remember them. But don't be afraid, and don't give up halfway, because you don't need to remember the order, because when you learn data analysis later, you will naturally remember it.

It doesn't matter if you can't remember, just learn where to look for it. You just need to follow the above steps and understand what each step does. When you encounter something to do later, just forget to check this document back.

There will be difficulties in the process of learning at the beginning, as long as you don't give up halfway and do it yourself, I guarantee that you can master it proficiently.

1. What is Anaconda?

Anaconda means "python" in English, and there is a song called "Anaconda", which means a sexy and enchanting body like a python.

35f855b9d4b0a81d811c74f1b52e1270.jpeg

All the Anaconda icons you see below are like a "boa constrictor" that bites each other at the end.

47b83b47139944defdd0ab30f0232e45.jpeg

You probably already have Python installed, so why do you need Anaconda? There are 3 reasons:

1) Anaconda comes with a large number of commonly used data science packages, it comes with conda, Python and more than 150 scientific packages and their dependencies. So you can start processing data right away.

2) Management Pack

Anaconda was developed on conda (a package manager and environment manager).

In data analysis, you will use many third-party packages, and conda (package manager) can help you install and manage these packages on your computer, including installing, uninstalling and updating packages.

3) Management environment

Why do we need to manage the environment? For example, if you use Python 2 in project A, and the boss of new project B requires Python 3, installing two Python versions at the same time may cause a lot of confusion and errors. At this time conda can help you create different operating environments for different projects.

There are also many projects that use different package versions, such as different pandas versions. It is impossible to install two Numpy versions at the same time. What you should do is to create an environment for each Numpy version, and then work in the corresponding environment of the project. At this time conda can help you do it.

2. How to install Anaconda?

Anaconda is available for multiple platforms (Windows, Mac OS X, and Linux). You can find the installer and installation instructions at the address below. Select the corresponding version to download according to whether your operating system is 32-bit or 64-bit.

(Anaconda no longer supports Windows XP; at the same time, check whether your computer is 32-bit or 64-bit, and don’t install it wrong.)

Official website address: https://www.continuum.io/downloads 

If the official website address is too slow to download, you can reply "information" from my official account to download from the network disk

d885e9fc9731b84ab96095e5498a1afb.jpeg

b4434f4993476b6c57170c280c9a2e8f.jpeg

The download for Anaconda is relatively large (about 500 MB) because it comes with the most commonly used data science packages in Python.

If Python is already installed on your computer, the installation will have no effect on you. In fact, the default Python used by scripts and programs is the one that ships with Anaconda.

Note: If you are a windows 10 system, pay attention to when installing the Anaconda software, right-click to install the software → choose to run as an administrator.

f0b144abb549e0fd444d1e554e4bf4fd.jpeg

After the installation is complete, if you are operating on windows, open Anaconda Prompt (or the terminal under Mac) as shown below. I will collectively refer to Anaconda Prompt as "terminal" later.

c6d9d48847202c6736f991133852543f.jpeg

Note: If you are a windows 10 system, please follow the picture below

d74dd798a0aa20a6dcf35c1f6b683bdf.jpeg

If the win10 system does not follow the above operation, the following error message will be reported later:

a2fec3fe96740f7ea1ea48de71aa41e3.jpeg

You can type conda list in a terminal or command prompt to see what you have installed.

6094985529e4f1e7d8e2c36454a081f6.jpeg

PS: If you can’t use Conda commands in Anaconda Prompt after following the above operations, try the solutions in the following order (friends who use them normally ignore this step and continue):

1) Check whether you have installed Python before, if you have installed it, please delete Python completely (and delete the environment variables at the same time) and then reinstall Anaconda

2) Check yourself that you added the Conda command to the environment variables.

3) Make sure your Anaconda installation path does not contain Chinese or other non-English common characters

4) After the above steps, there is still no improvement, please uninstall Anaconda and reinstall it again

047f2a0047d00319c82d2314cfee2698.jpeg

If the conda command can be used in Anaconda Prompt, then continue below.

In order to avoid error reporting later, you need to update all packages first. Enter the command to update all packages in the terminal:

conda upgrade    --all

And when prompted whether to update, enter y (Yes) to let the update continue. The software package version installed for the first time is generally relatively old, so updating in advance can avoid unnecessary problems in the future.

If you run the above command or the installation package reports the following error message:

fcb22a3a8c6b12b6a676beb93cae2576.jpeg

You can use the following solution (run the following command in the terminal):

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --set show_channel_urls yes

If the command line method cannot be added, you can find the file .condarc in the "notebook working folder",

What is your "notebook working folder"?

By default, it is the folder where you start the Anaconda Prompt terminal. For example, the following folder is on my computer (Windows is C://Users/username/.condarc, Linux/Mac is ~/.condarc):

f12808af2595367ca362e917b6196c83.jpeg

Open the folder and modify the contents of the file as follows:

(If you don’t find .condarc, use the notepad++ software to create a new file under the “notebook working folder” and add the following content)

67a1d0f0b7a403764b205dcac1662c1d.jpeg

For convenience, you can copy the following section directly into the above file:

channels:

- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 

- defaults

show_channel_urls: yes

3. How to manage packages?

After installing Anaconda, you can easily manage packages (install, uninstall, update).

1) Install the package

Type in terminal:

conda install package_name

For example, to install pandas, type in a terminal:

conda install pandas

You can also install multiple packages at the same time. A command like conda install pandas numpy will install all of these packages at once. You can also specify the desired package version by adding the version number (e.g. conda install numpy=1.10 ).

conda will also automatically install dependencies for you. For example, scipy depends on numpy because it uses and requires numpy. If you only install scipy (conda install scipy), conda will also install numpy (if it is not already installed).

2) Uninstall the package

Type in terminal:

conda remove package_names

The package_names in the above command refers to the name of the package you want to uninstall, for example, if you want to uninstall the pandas package: conda remove pandas

3) Update package

Type in terminal:

conda update package_name

If you want to update all packages in your environment (which is often useful), use: conda update --all.

4) List installed packages

# list installed packages

conda list

For example, I have successfully installed two commonly used packages, numpy and pandas.

d0ee7fe47c0e8f4b7d30ae778014566f.jpeg

If you don't know the exact name of the package you're looking for, try searching with conda search search_term. For example, I know I want to install numpy, but the exact package name is unclear to me. I can try this: conda search num.

4. How to manage the environment?

conda can create different runtime environments for your different projects.

0) Install nb_conda for the notebook to automatically associate with the nb_conda environment.

b2672cd8f8fcd4813113a03f8847281b.jpeg

1) Create an environment

In terminal use:

conda create -n env_name package_names

In the above command, env_name is the name of the setting environment (-n means that the env_name after the command is the name of the environment you want to create), and package_names is the name of the package you want to install in the creation environment.

For example, to create an environment named py3 and install numpy in it, enter conda create -n py3 pandas in a terminal.

df7f06ef69af981c5cea3d5ed7846a4f.jpeg

2) When creating an environment, you can specify the Python version to be installed in the environment

This is useful when you are working with code in both Python 2.x and Python 3.x. To create an environment with a specific Python version, for example, create an environment named py3, and install the latest version of Python3 Type in the terminal:

conda create -n py3 python=3 

Or you can also create an environment named py2 and install the latest version of Python2:

create create -n py2 python=2

Because the projects I do are different, sometimes I use Python2, and sometimes I use Python3. So I created these two environments on my own computer, and took such environment names: py2, py3. This way I can easily use different versions of python depending on the project.

If you want to install a specific version (e.g. Python 3.6), use conda create -n py python=3.6

3) Enter the environment

On Windows, you can enter with activate my_env. Enter the environment with source activate my_env on OSX/Linux.

After entering the environment, you will see the environment name in the terminal prompt. The picture below is the environment where I entered py3 (here py3 is the name I gave when I created the environment above, you can name it as you like).

f1bce5dc4f9565ba7bba9931bf0970be.jpeg

After entering the environment, I can use conda list to view several packages installed by default in the environment:

749ff31db34faa7559320d9d4bb9f821.jpeg

The command to install a package in the environment is the same as before: conda install package_name.

This time, though, the specific package you installed will only be available once you're in the environment.

3) leave the environment

On Windows, type in Terminal: 

deactivate

On OSX/Linux type:

source deactivate

4) Shared environment

Shared environments are useful because they allow other people to install all the packages used in your code and make sure they are at the correct versions. For example, if you develop a pharmacy data analysis system, you have to submit it to Wang Ergou of the project deployment system to deploy your project, but Wang Ergou does not know which python version you were using when developing, and which packages you used and package version. What to do about this?

You can use conda env export > environment.yaml in the terminal in your current environment to save your current environment to a file. Packages are saved as YAML files (including the Python version and the names of all packages).

The first part of the command conda env export is used to output the names of all packages in the environment (including the Python version).

a6c9dacbb82bbb5ef40124edabd47a5e.jpeg

Under the "notebook working folder" (and the path you see above in the terminal), you can see the exported environment file:

db54595ed38f8e3daf67b5e71f5ce68a.jpeg

When sharing code on GitHub, it's a good idea to also create an environment file and include it in the code repository. This makes it easier for others to install all of your code's dependencies.

How to use the exported environment file in other computer environments?

First enter your environment in conda, such as activate py3

Then update your environment with the following command:

#Where -f means that you want to export the local path of the file, so /path/to/environment.yml should be replaced with your local actual path

conda env update -f=/path/to/environment.yml

For users who don't use conda, I usually also use pip freeze > environment.txt to export a txt file and include it.

See here for details: https://pip.pypa.io/en/stable/reference/pip_freeze/

It may be easier to understand this usage scenario with an example:

First, I export the package of the project into an environment.txt file in conda on my computer:

d356019e6bc947fdec7c92f0836e0507.jpeg

Then I include this file in the project's code repository, and other project members can use this file to install the same development environment as me even if they don't have conda installed on their computers:

He enters the python command environment on his computer, and then runs the following command to install the packages required by the project:

pip install -r /path/requirements.txt

Where /path/requirements.txt is the actual path of the file on your computer.

939ea5146ba5a8ef42e247cb2db7e258.jpeg

5) List the environment

Sometimes I forget the name of the environment I created. At this time, use conda env list to list all the environments you created.

You'll see a list of environments with an asterisk next to the environment you're currently in. The default environment (that is, the environment used when you are not in the selected environment) is named root.

61d2ea5e9cf457eaa35fc03847079137.jpeg

6) Delete the environment

If you no longer use an environment, you can use conda env remove -n env_name to delete the specified environment (here the environment is named env_name).

Finally, re-emphasize, don't be intimidated by the above command. Although there are many commands above, no one can remember them. Later, you will follow me to do more projects on Zhihu, and you will remember it when you use it a lot. You just need to follow the above steps and understand what each step does. When you encounter something to do later, just forget to check this document back.

Conda's official documentation: https://conda.io/docs/user-guide/tasks/index.html

After following the steps above, you have learned Anaconda and installed your data analysis Pyhton environment. Then you can happily use Jupyter notebook for data analysis.

b5c7164d33febc23225d915bb2fb4d04.jpeg ⬇️Click "Read the original text"

 Sign up for free Data analysis training camp

Guess you like

Origin blog.csdn.net/zhongyangzhong/article/details/129019708