Use Anaconda to manage python development environment under windows

Overview

Many beginners who learn python or even learn for a while feel that they can’t get started when they come into contact with anaconda or other virtual environment tools. The main reason is that they don’t understand what these tools are for, what they are used for, and why they are so Do, for example, the author didn’t understand at the beginning why I needed such a thing besides python, what is the connection and difference between it and python, and why it can be used to manage python.

After using it, I gradually discovered what environmental management tools such as anaconda are actually doing, and why we need them to manage our python environment

First of all, we need to understand the purpose of Anaconda’s birth. Then we need to understand how to use Anaconda.

Python itself

First of all, we need to start with python itself and find the problem from the root. We need to download a python interpreter before writing programs in python language. This is the essence of python. Without the python interpreter, even if we write extremely correct and elegant There is no way to run the python script, so where is the interpreter? It is where you installed python, for example, mine is in C:\Users\Acring\AppData\Local\Programs\Python\Python36 -32

The project structure is shown in the figure above, here is the familiar python.exe, which is the Python interpreter

In addition, there is another very important thing, Lib, which is the python package file, including the built-in package and the third-party package

Lib folder

The Lib directory is shown in the figure above, where there are packages that come with python, such as the logging package commonly used by the author, the asynchronous package concurrent, and all third-party packages are placed in the site-packages folder

After understanding these, we have a general understanding of the entire python environment. In fact, the most important thing is that a python environment needs an interpreter and a package collection.

Interpreter

The interpreter is roughly divided into 2 and 3 according to the python version. Python2 and 3 are not compatible with each other, that is to say, scripts written in python2 syntax may not be able to run in the python3 interpreter.

Package Collection

The package collection contains built-in packages and third-party packages. We usually download third-party packages through pip or easy_install. When a python environment does not contain this package, the program that references this package cannot run in this python environment. .

For example, a crawler script uses a third-party requests package, and the other computer has just installed the original python, that is to say, there is no third-party package at all, then the crawler script cannot be run on another machine .

What is the problem

After explaining the python environment, the next step is to explain what problems arise in such an environment, because anaconda was officially born to solve these problems

Should I install Python2 or Python3?

Python2 and python3 are grammatically incompatible, so should I install python2 or python3 on my machine, maybe I can choose one to learn at the beginning, but if the program you want to develop must use python2 but not python3, then this Sometimes you have to download another python2, then whose directory should the environment variable be set at this time, wouldn’t it be very troublesome if you still switch the environment variable.

Package Management

If I only have one python environment locally, the various packages used by all my programs can only be placed in the same environment, causing the environment to be chaotic. In addition, when I put the written program on another computer and run it, it will It is really annoying to encounter the lack of related packages and need to manually download them one by one. If each program can be developed in a different environment, and after the development is completed, the environment (third-party package) required by the program can be independent Just pack it out.

Anaconda

Then it’s time for our anaconda to play, first let us install Anaconda and then I will tell you how to use Anaconda to solve our above problems one by one.

Download

Official website download

It is recommended to download the python3 version, after all, python2 will stop maintenance in the future.

Install

Just follow the installation program prompts to install step by step. After the installation is complete, there will be a few more applications

  • Anaconda Navigtor: a graphical user interface for managing toolkits and environments, and many management commands involved in the follow-up can also be implemented manually in Navigator.
  • Jupyter notebook: A web-based interactive computing environment that can edit human-readable documents to demonstrate the process of data analysis.
  • qtconsole: A terminal-like graphical interface program that can execute IPython. Compared with the Python Shell interface, qtconsole can directly display the graphics generated by the code, realize multi-line code input and execution, and have many built-in useful functions and functions.
  • spyder: A Python language, cross-platform, scientific computing integrated development environment.

Don’t worry about it for now, just understand

Configure environment variables

If it is windows, you need to go to the control panel\system and security\system\advanced system settings\environment variables\user variables\PATH to add the Scripts folder of the anaconda installation directory, for example, my path is D: \Software\Anaconda\Scripts, depending on the personal installation path, you need to adjust it yourself.

Then you can open the command line (preferably in administrator mode) and enter conda –version

If the output is like conda 4.4.11, it means that the environment variable is set successfully.

In order to avoid possible errors, we enter conda upgrade –all on the command line to upgrade all toolkits first

Managing virtual environments

Next, we can use anaconda to create our independent python environments. The following examples are all operated on the command line, please open your command line.

activate

activate can introduce us into the virtual environment set by anaconda. If you don’t add any parameters later, you will enter the base environment that comes with anaconda.

You can try typing python, which will enter the python interpreter in the base environment. If you remove the python environment in the original environment, you will understand better. At this time, what you use in the command line is not your original python but Python in the base environment. There will be an extra (base) in front of the command line, indicating that we are currently in the base environment.

activate

Create your own virtual environment

Of course we are not satisfied with a base environment, we should install a separate virtual environment for our own programs.

Create a virtual environment named learn and specify the python version as 3 (here conda will automatically find the latest version of 3 to download)

conda create -n learn python=3


So we have a learn virtual environment, and then we switch to this environment, or use the activae command followed by the name of the environment to be switched

Switch environment

activate learn

If we forget the name we can first use

conda env list

to view all environments

The current learn environment has no other packages except some official packages that come with python. We can try a relatively clean environment.

First enter python to open the python interpreter and then enter

>>> import requests

It will report an error that the requests package cannot be found, which is normal. Next we will demonstrate how to install the requests package

exit()

exit python interpreter

Install third-party packages

enter

conda install requests

or

pip install requests

To install the requests package.

After the installation is complete, we enter python to enter the interpreter and import the requests package. This time it must be successful.

Uninstall third-party packages

So how to uninstall a package?

conda remove requests

or

pip uninstall requests

That’s it.

View environment package information

To view all installed packages in the current environment you can use

conda list

Import and export environment

If you want to export the package information of the current environment, you can use

conda env export > environment.yaml

Store the package information in the yaml file.

When you need to recreate the same virtual environment, you can use

conda env create -f environment.yaml

In fact, the command is very simple, right? I will give some commonly used ones below, and I believe that I can remember them by typing twice more.

activate // switch to the base environment

activate learn // switch to learn environment

conda create -n learn python=3 // Create an environment named learn and specify the python version as 3 (the latest version)

conda env list // list all environments managed by conda

conda list // list all packages in the current environment

conda install requests install requests package

conda remove requests Uninstall the requets package

conda remove -n learn –all // Delete the learn environment and all subordinate packages

conda update requests update requests package

conda env export > environment.yaml // Export the package information of the current environment

conda env create -f environment.yaml // Create a new virtual environment with a configuration file

A little deeper

Maybe you will wonder why anaconda can do these things, what is its principle, let’s take a look at the installation directory of anaconda

Only a part is intercepted here, but we compare it with the python environment directory at the beginning of this article, and we can find that it is very similar. In fact, this is the base environment. There is a basic python interpreter in it, and lLib also contains various files in the base environment. kind of package file.

Then where is the environment we created, we can see an envs, here is the entrance of various virtual environments we created, click to see

You can find that the learn directory we created before is below, and then click in

Isn’t this a standard python environment directory?

Looking at it this way, anaconda’s so-called creating a virtual environment is actually installing a real python environment, but we can switch our current python environment at will through activate, conda and other commands, using different versions of interpreters and different packages environment to run python scripts.

Connect with pycharm

In the working environment, we will integrate the development environment to code. Here we recommend pycharm of JB company, and pycharm can also be easily combined with the virtual environment of anaconda

Modify the Project Interpreter in Setting => Project => Project Interpreter, click the gear icon and then click Add Local for the python.exe interpreter of your environment

For example, if you want to write a program in the learn environment, then modify it to D:\Software\Anaconda\envs\learn, and you can see that the following dependent packages have also become packages in the learn environment. Then Next, we can happily code in pycharm.

Conclusion

Now have you found that using anaconda can solve the disadvantages of the single python environment mentioned above very elegantly and simply, and also understand that the realization of all this is not so magical.

Of course, in addition to package management, anaconda also lies in its rich data analysis package, but that is another content. We first learn to use anaconda to manage our own development environment in a different way, which is already a great progress.