由于阿里云的ubuntu服务器 默认有一个2.7一个3.5版本的python,如果又另外安装和使用更高版本的话,经常会遇到运行python程序出现已经安装依赖包但找不到对应依赖包的情况。
给远程服务器安装Anaconda,然后用conda创建新的python虚拟环境,并且可以根据生产需求切换不同的python版本。
1、Anaconda下载安装:
下载地址1:Anaconda官网
下载地址2:清华源地址
下载所需的Anaconda 版本至自己的服务器(随便选一个自己的文件放就行)。
qiang@09230:~$ cd /home/qiang
qiang@09230:~$ wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
--2022-05-09 20:02:26-- https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
Resolving repo.anaconda.com (repo.anaconda.com)... 104.16.131.3, 104.16.130.3, 2606:4700::6810:8303, ...
Connecting to repo.anaconda.com (repo.anaconda.com)|104.16.131.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 570853747 (544M) [application/x-sh]
Saving to: ‘Anaconda3-2021.05-Linux-x86_64.sh’
Anaconda3-2021.05-Linux-x86_64.sh 100%[========================>] 544.41M 11.8MB/s in 1m 51s
2022-05-09 20:04:18 (4.91 MB/s) - ‘Anaconda3-2021.05-Linux-x86_64.sh’ saved [570853747/570853747]
qiang@09230:~$ ls
Anaconda3-2021.05-Linux-x86_64.sh
qiang@09230:~$
在Linux系统里面.sh文件是可执行的脚本文件,可以用命令bash来进行安装。
在安装包文件目录下输入安装命令:
bash Anaconda3-2019.10-Linux-x86_64.sh
qiang@09230:~$ ls
AlarmReport.log BoxCommunicationRun.log flask 'Lif tControlTest.txt' MQTT mysql_bak.sh protect.sh wget-log.1
anaconda3 django.log flask.log LiftControlTest.txt mqtt.log NaviCommunicationRun.log qiang yangrui
Anaconda3-2021.05-Linux-x86_64.sh django_projects LiftCommunicationRun.log log mysql_bak2.sh nginx-auth-ldap start_server.sh
BackGarden element.log LiftControlNode.log mapbackup mysql_bak3.sh nohup.out test
bak FlashCommunication.log LiftControlTest.log mapsync mysql_bak4.sh php wget-log
qiang@09230:~$ sudo bash Anaconda3-2021.05-Linux-x86_64.sh
Welcome to Anaconda3 2021.05
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
===================================
End User License Agreement - Anaconda Individual Edition
===================================
Copyright 2015-2021, Anaconda, Inc.
All rights reserved under the 3-clause BSD License:
This End User License Agreement (the "Agreement") is a legal agreement between you and Anaconda, Inc. ("Anaconda") and governs your use of Anaconda Individual Edition (which was formerly known as Anaconda Distributi
on).
Subject to the terms of this Agreement, Anaconda hereby grants you a non-exclusive, non-transferable license to:
...........................
Export; Cryptography Notice
===========================
You must comply with all domestic and international export laws and regulations that apply to the software, which include restrictions on destinations, end users, and end use. Anaconda Individual Edition includes cr
yptographic software. The country in which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software
, please check your country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted. See the Wassenaar Arrangement http://www.was
senaar.org/ for more information.
Anaconda has self-classified this software as Export Commodity Control Number (ECCN) 5D992.c, which includes mass market information security software using or performing cryptographic functions with asymmetric algo
rithms. No license is required for export of this software to non-embargoed countries.
The Intel Math Kernel Library contained in Anaconda Individual Edition is classified by Intel as ECCN 5D992.c with no license required for export to non-embargoed countries.
The following packages listed on https://www.anaconda.com/cryptography are included in the repository accessible through Anaconda Individual Edition that relate to cryptography.
Last updated April 5, 2021
Do you accept the license terms? [yes|no]
[no] >>> yes
Anaconda3 will now be installed into this location:
/root/anaconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/root/anaconda3] >>> yes
......................................
ujson pkgs/main/linux-64::ujson-4.0.2-py38h2531618_0
unicodecsv pkgs/main/linux-64::unicodecsv-0.14.1-py38_0
unixodbc pkgs/main/linux-64::unixodbc-2.3.9-h7b6447c_0
urllib3 pkgs/main/noarch::urllib3-1.26.4-pyhd3eb1b0_0
watchdog pkgs/main/linux-64::watchdog-1.0.2-py38h06a4308_1
wcwidth pkgs/main/noarch::wcwidth-0.2.5-py_0
webencodings pkgs/main/linux-64::webencodings-0.5.1-py38_1
werkzeug pkgs/main/noarch::werkzeug-1.0.1-pyhd3eb1b0_0
wheel pkgs/main/noarch::wheel-0.36.2-pyhd3eb1b0_0
widgetsnbextension pkgs/main/linux-64::widgetsnbextension-3.5.1-py38_0
wrapt pkgs/main/linux-64::wrapt-1.12.1-py38h7b6447c_1
wurlitzer pkgs/main/linux-64::wurlitzer-2.1.0-py38h06a4308_0
xlrd pkgs/main/noarch::xlrd-2.0.1-pyhd3eb1b0_0
xlsxwriter pkgs/main/noarch::xlsxwriter-1.3.8-pyhd3eb1b0_0
xlwt pkgs/main/linux-64::xlwt-1.3.0-py38_0
xmltodict pkgs/main/noarch::xmltodict-0.12.0-py_0
xz pkgs/main/linux-64::xz-5.2.5-h7b6447c_0
yaml pkgs/main/linux-64::yaml-0.2.5-h7b6447c_0
yapf pkgs/main/noarch::yapf-0.31.0-pyhd3eb1b0_0
zeromq pkgs/main/linux-64::zeromq-4.3.4-h2531618_0
zict pkgs/main/noarch::zict-2.0.0-pyhd3eb1b0_0
zipp pkgs/main/noarch::zipp-3.4.1-pyhd3eb1b0_0
zlib pkgs/main/linux-64::zlib-1.2.11-h7b6447c_3
zope pkgs/main/linux-64::zope-1.0-py38_1
zope.event pkgs/main/linux-64::zope.event-4.5.0-py38_0
zope.interface pkgs/main/linux-64::zope.interface-5.3.0-py38h27cfd23_0
zstd pkgs/main/linux-64::zstd-1.4.5-h9ceee32_0
Preparing transaction: done
Executing transaction: done
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>>yes
......................................
no change /home/qiang/yes/lib/python3.8/site-packages/xontrib/conda.xsh
no change /home/qiang/yes/etc/profile.d/conda.csh
modified /root/.bashrc
==> For changes to take effect, close and re-open your current shell. <==
If you'd prefer that conda's base environment not be activated on startup,
set the auto_activate_base parameter to false:
conda config --set auto_activate_base false
Thank you for installing Anaconda3!
===========================================================================
Working with Python and Jupyter notebooks is a breeze with PyCharm Pro,
designed to be used with Anaconda. Download now and have the best data
tools at your fingertips.
PyCharm Pro for Anaconda is available at: https://www.anaconda.com/pycharm
qiang@09230:~$
一直按Enter回车键继续,接受许可条款。注意按enter别安太快,接下来会提示是否添加环境变量,输入yes,否则还得自己配环境变量。
输入 python 命令进入python环境,同时终端的头部会出现(base),说明Anaconda已经安装成功了,接下来可以创建纯净python编译环境了。
(base) qiang@09230:~$ python
Python 3.8.8 (default, Apr 13 2021, 19:58:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
另外如果需要每次打开服务器终端时自动激活conda的base环境,可以输入如下命令:
conda config --set auto_activate_base false
2、使用conda创建新的python环境
conda create --name qiang python=3.8.10
qiang@09230:~$ source ~/.bashrc
(base) qiang@09230:~$ conda config --set auto_activate_base false
(base) qiang@09230:~$ conda config --set auto_activate_base true
(base) qiang@09230:~$ conda create --name qiang python=3.8.10
Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.10.1
latest version: 4.12.0
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /home/qiang/tools/anaconda3/envs/qiang
added / updated specs:
- python=3.8.10
The following packages will be downloaded:
package | build
---------------------------|-----------------
python-3.8.10 | h12debd9_8 57.7 MB
------------------------------------------------------------
Total: 57.7 MB
The following NEW packages will be INSTALLED:
_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
_openmp_mutex pkgs/main/linux-64::_openmp_mutex-4.5-1_gnu
ca-certificates pkgs/main/linux-64::ca-certificates-2022.4.26-h06a4308_0
certifi pkgs/main/linux-64::certifi-2021.10.8-py38h06a4308_2
ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.35.1-h7274673_9
libffi pkgs/main/linux-64::libffi-3.3-he6710b0_2
libgcc-ng pkgs/main/linux-64::libgcc-ng-9.3.0-h5101ec6_17
libgomp pkgs/main/linux-64::libgomp-9.3.0-h5101ec6_17
libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-9.3.0-hd4cf53a_17
ncurses pkgs/main/linux-64::ncurses-6.3-h7f8727e_2
openssl pkgs/main/linux-64::openssl-1.1.1o-h7f8727e_0
pip pkgs/main/linux-64::pip-21.2.4-py38h06a4308_0
python pkgs/main/linux-64::python-3.8.10-h12debd9_8
readline pkgs/main/linux-64::readline-8.1.2-h7f8727e_1
setuptools pkgs/main/linux-64::setuptools-61.2.0-py38h06a4308_0
sqlite pkgs/main/linux-64::sqlite-3.38.3-hc218d9a_0
tk pkgs/main/linux-64::tk-8.6.11-h1ccaba5_1
wheel pkgs/main/noarch::wheel-0.37.1-pyhd3eb1b0_0
xz pkgs/main/linux-64::xz-5.2.5-h7f8727e_1
zlib pkgs/main/linux-64::zlib-1.2.12-h7f8727e_2
Proceed ([y]/n)? y
Downloading and Extracting Packages
python-3.8.10 | 57.7 MB | ############################################################################################################################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate qiang
#
# To deactivate an active environment, use
#
# $ conda deactivate
(base) qiang@09230:~$ conda active qiang
CommandNotFoundError: No command 'conda active'.
Did you mean 'conda activate'?
(base) qiang@09230:~$ conda activate qiang
(qiang) qiang@09230:~$
这时在终端输入python,就可以看到我们刚刚建立好的环境:
(qiang) qiang@09230:~$ python
Python 3.8.10 (default, Jun 4 2021, 15:09:15)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
(qiang) qiang@09230:~$
3、激活环境
激活之前先查看环境
(qiang) qiang@09230:~$ conda env list
# conda environments:
#
yourEnv /home/qiang/.conda/envs/yourEnv
base /home/qiang/tools/anaconda3
qiang * /home/qiang/tools/anaconda3/envs/qiang
(qiang) qiang@09230:~$
激活环境:
(qiang) qiang@09230:~$ source activate base
(base) qiang@09230:~$
常用命令:
source activate yourEnv # 激活环境
source deactivate yourEnv # 退出环境
conda env remove -n yourEnv # 删除环境
conda remove --name xxxx --all #彻底删除旧环境
conda remove -n yourEnv package_name # 删除环境中的某个包
如果你手动修改过配置环境,需要执行下面命令,使用source命令执行脚本/.bashrc(/.bashrc会调用 /etc/bashrc文件)
source ~/.bashrc