Home > Article > Backend Development > Python environment configuration analysis
This time I will bring you Python environment configuration analysis. What are the precautions for Python environment configuration analysis? The following is a practical case, let’s take a look.
If you plan to learn Python for data analysis, are you encountering various troubles at the beginning?
Should I install Python2 or Python3?
Why do I always get errors when installing Python?
How to install the toolkit?
Why is it prompted that a bunch of other unknown tools must be installed before installing this tool?
I believe that most Python beginners have had headaches due to environmental issues, but you are not alone, everyone has suffered like this. In order to avoid detours when getting started, and to prevent the high enthusiasm from being too dampened, it is recommended to use Anaconda to manage your installation environment and various tool packages.
This article introduces the use of Anaconda. The full text outline is as follows:
Why choose Anaconda
* What is Anaconda
* What is conda
* Advantages of Anaconda
How to install Anaconda
How to manage Python packages
How to manage the Python environment
Anaconda is a Python distribution focused on data analysis, including more than 190 scientific packages such as conda and Python and their dependencies. As a curious baby, have you discovered a new term conda? Then you will definitely ask what is conda?
conda is a management system for open source packages and virtual environments.
packages management: You can use conda to install, update, and uninstall tool packages, and it focuses more on data science-related tool packages. When installing anaconda, packages commonly used in data analysis such as Numpy, Scipy, pandas, and Scikit-learn are pre-integrated. It is also worth mentioning that conda not only manages Python tool packages, it can also install non-python packages. For example, the R language integrated development environment Rstudio can be installed in the new version of Anaconda.
Virtual environment management: Multiple virtual environments can be established in conda to isolate different versions of tool packages required by different projects to prevent version conflicts. For students who are confused about the Python version, we can also create two environments, Python2 and Python3, to run different versions of Python code respectively.
While knowing what it is, we also need to ask why. So, why should you choose Anaconda?
The advantages of Anaconda can be summed up in eight words: saving time and worry, and being a powerful analysis tool.
Save time and worry: Anaconda greatly simplifies your workflow by managing tool packages, development environments, and Python versions. Not only can the tool package be easily installed, updated, and uninstalled, but also the corresponding dependency packages can be automatically installed during installation. At the same time, different virtual environments can be used to isolate projects with different requirements.
Analysis tool: Anaconda promotes itself on its official website as follows: a Python tool suitable for enterprise-level big data analysis. It contains more than 720 open source packages related to data science, covering many aspects such as data visualization, machine learning, and deep learning. Not only can it be used for data analysis, it can even be used in the fields of big data and artificial intelligence.
After solving the "what" and "why" questions, let's take a look at the "How".
You can download the Anaconda installer and view the installation instructions from here. Whether it is Windows, Linux or MAC OSX system, you can find the corresponding installation software. If your computer is 64-bit, try to choose the 64-bit version. As for whether the Python version is 2.7 or 3.x, it is recommended that you use Python3 because Python2 will eventually stop maintenance. Maybe most of the tutorials on the market currently use Python2, but don’t worry, because Anaconda can manage two Python versions of the environment at the same time.
Install according to the prompts. After completion, you may be surprised to find that there are many more applications on your computer. Don’t worry, we will look at them one by one:
Anaconda Navigtor: A graphical user interface for managing toolkits and environments. Many subsequent management commands can also be implemented manually in Navigator.
Jupyter notebook: A web-based interactive computing environment that can edit documents that are easy for people to read and used to display 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 execution, and have many built-in Useful features and functions.
spyder: A cross-platform, scientific computing integrated development environment using Python language.
After the installation is completed, we also need to upgrade all tool packages to avoid possible errors. Open the terminal of your computer and enter in the command line:
conda upgrade --all
When the terminal asks whether to install the following upgrade version, enter y.
In some cases, you may encounter an error message that the conda command cannot be found. This is likely to be a problem with the environment path setting. You need to add the conda environment variable: export PATH=xxx/anaconda/bin:$PATH, Replace xxx with the installation path of anaconda.
Now that the installation is complete, let’s take a look at how to use Anaconda to manage tool packages and environments.
Install a package:
conda install package_name
Here package_name is the name of the package that needs to be installed. You can also install multiple packages at the same time, such as installing numpy, scipy and pandas at the same time, then execute the following command:
conda install numpy scipy pandas
You can also specify the version to be installed, such as installing 1.1 version of numpy:
conda install numpy=1.10
Remove a package:
conda remove package_name
Upgrade package version:
conda update package_name
View all packages:
conda list
If you can’t remember the specific name of the package, you can also perform a fuzzy query:
conda search search_term
The default environment is root, you can also create a new environment:
conda create -n env_name list of packages
where -n represents name, env_name is the name of the environment that needs to be created, and list of packages lists the tool packages that need to be installed in the new environment.
For example, when I installed the Python3 version of Anaconda, the default root environment is naturally Python3, but I also need to create a Python 2 environment to run the old version of Python code. It is best to install pandas package, so we run the following command to create it:
conda create -n py2 python=2.7 pandas
If you are careful, you will definitely find that not only is the py2 environment installed pandas and a series of packages such as numpy are also installed. This is the convenience of using conda. It will automatically install the corresponding dependency packages for you without requiring you to install them manually one by one.
Enter the environment named env_name:
source activate env_name
Exit Current environment:
source deactivate
Also note that in Windows systems, use activate env_name and deactivate to enter and exit an environment.
Delete the environment named env_name:
conda env remove -n env_name
Display all environments:
conda env list
When sharing code, you also need to share the running environment with everyone. Execute the following command to save the package information in the current environment into a YAML file named environment.
conda env export > environment.yaml
Similarly, when executing other people's code, you also need to configure the corresponding environment. At this time, you can use the YAML file shared by the other party to create an identical running environment.
conda env create -f environment.yaml
At this point, you have entered the door of Anaconda, and you can wander in the ocean of Python.
Happy studying!
Note: The code examples in this article are referenced from the Anaconda chapter of the Udacity data analysis course.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Operation excel to read and write data
##How to perform automated unit testing with Python Unittest
The above is the detailed content of Python environment configuration analysis. For more information, please follow other related articles on the PHP Chinese website!