Use Anaconda Python version of the virtual environment management

1 Introduction

Bowen describes the first few too virtualenv, virtualenvwrapper and several other virtual environment management tools, anaconda Benpian to introduce also has a very strong management capabilities of the virtual environment, even compared to virtualenv, virtualenvwrapper and other tools, more powerful.
Anaconda release is focused on data analysis, including the conda, Python and more than 190 scientific packages and their dependencies. We Benpian anaconda virtual environment management is to introduce the use of conda them. conda is a very special environment management tools, the reason it special, conda design ideas --conda almost all of the tools, third-party packages are treated as a package, even python and conda itself! Therefore, conda breaking the constraints package management and environmental management, can easily install various versions of python, a variety of package and easily switch. That said, during the management of virtual environments, conda can be independent of the system created the original version of Python any version of the Python interpreter, which is virtualenv, virtualenvwrapper and other tools are not available.

2 Installation

2.1 Download

The installed version is anaconda3. First into a directory used to store the downloaded file, and then install anaconda:
$ cd /home//download
$ wget https://repo.anaconda.com/archive/Anaconda3-2018.12-Linux-x86_64.sh

2.2 Installation and Configuration

If there is no accident, after the completion of the command, in the / home / ubuntu / download directory will download a file named Anaconda3-2018.12-Linux-x86_64.sh of success.
Started the installation with the following command:
$ bash Anaconda3-2018.12-Linux-x86_64.sh
During the installation process, you need to allow the use of protocols, yes you can enter:
Do you accept the license terms? [yes|no]
[no] >>>
Please answer 'yes' or 'no':'
>>> yes
Then you want to enter into the anaconda installation directory, such as the anaconda installation to / usr / bin / anaconda3:
[/home/ubuntu/anaconda3] >>> /usr/bin/anaconda3
Of course, this time I chose the default installation / home / ubuntu / anaconda3 directory, so you can directly enter.
After the input is complete, the system will continue to complete the installation, the following output appears to know tips:
Do you wish the installer to initialize Anaconda3
in your /home/ubuntu/.bashrc ? [yes|no]
[no] >>>
Here, you can output yes. Meaning, add the bin directory / home / ubuntu / anaconda3 under the current user's environment variable configuration. If you missed this step configuration or input is no, you will need to manually configure the environment variables:
$ cd ~
$ vim .bashrc
Then enter the following in the last line:
# Anaconda3 configuration environment 
Export the PATH = " / Home / Ubuntu / anaconda3 / bin: $ the PATH "
After completion of all steps above, run the following command to make the configuration take effect:
$ source .bashrc

2.3 verify the installation

If all the above steps are not accidental, the successful completion of the case, in fact anaconda has been installed successfully. Let's verify.
Python entered directly on the command line to see the version of Python:
$ python
Python 3.7.1 (default, Dec 14 2018, 19:28:38)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
You can see, Python version is already not the same as the original.
View conda version, run the command:
$ Conda - version 
conda 4.5.12
Output anaconda installed version is 4.5.12 conda. Above are indications that, anaconda really successful installation.

3 Environmental Management

3.1 to create a virtual environment

conda create -n env_name python=version package_names
Among them, the parameter -n refers to the next parameter to specify the name of the virtual environment, and --name equivalent, so the virtual environment is to specify the name python36 will be created. python = version Python version is designated for virtual environments, after specified version, conda will install the latest version available for this version, such as specifying the version is 3.6, conda will install the latest 3.6.x version. package_names is to create a new virtual environment to install the package, here you can not install other packages, this parameter is omitted, the subsequent use of the virtual environment to install third-party packages.
Example: create a virtual environment, Python version 3.6, a virtual environment called python36
conda create --name python36 python=3.6

View 3.2 virtual environment

conda env list
After running the following results:
# conda environments:
#
base * /home/chb/anaconda3
python36 /home/chb/anaconda3/envs/python36
The results are listed in the current system in a virtual environment created for all conda that directory environment lies. installed base is specified during installation anaconda Python environment. Model (*) refers to the Python environment, said the current system was used.

3.3 activate the virtual environment

conda activate env_name
For example: just enter the virtual environment created python36
conda activate python36
If you enter the virtual environment python36, after the rise of the command line will have a word "(python36)" is.

Exit Virtual Environment 3.4

Not need to specify the name of the virtual environment when you exit the virtual environment, you can directly run the following line:
conda deactivate

3.5 shared virtual environment

What is a shared virtual environment? When we developed a set of code on the machine, and then upload the code to github project team or send other people, it is people do not know the code above, we have developed the use of which depend, conda provides a convenient feature to get a virtual environment All dependencies unified export a configuration file, use this code on another machine, you can rebuild the virtual environment based on conda exported configuration file, which is shared virtual environment capabilities. In the case of multiplayer cooperative development and open source, shared virtual environment is undoubtedly a very useful feature.
First to export-dependent to a profile:
CONDA env export --file python36_env.yml
environment.yaml is dependent on the export target file, run the command, the current directory back to generate a environment.yaml file that contains all the dependency information.
Create a virtual environment according to the configuration file:
conda env create -f /home/chb/code/python36_env.yml
After running the above command will create a virtual environment exactly the same on the new machine.
3.6 Deleting a virtual environment
 conda remove -n python36 --all 
or
conda env remove -n python36

4 Package Manager

4.1 installation package

conda install package_name
or
pip install package_name

4.2 List all packages

conda list

4.3 update package

conda update package_name
If you want a one-time update all packages:
conda update --all

4.4 Find package

conda search keyword
For example: we want to install pandas, but forgot the exact name, you can look like this:
conda search pan

4.5 Delete Package

conda remove package_name

5 summary

Although compared to virtualenv, virtualenvwrapper and other tools, anaconda install complicated moment, but no doubt anaconda more powerful, can be more easily get package management.

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160160.htm