由于阿里云的ubuntu服務(wù)器 默認(rèn)有一個(gè)2.7一個(gè)3.5版本的python,如果又另外安裝和使用更高版本的話挂据,經(jīng)常會(huì)遇到運(yùn)行python程序出現(xiàn)已經(jīng)安裝依賴包但找不到對(duì)應(yīng)依賴包的情況滨彻。
給遠(yuǎn)程服務(wù)器安裝Anaconda蚪黑,然后用conda創(chuàng)建新的python虛擬環(huán)境丁侄,并且可以根據(jù)生產(chǎn)需求切換不同的python版本棋凳。
1、Anaconda下載安裝:
下載地址1:Anaconda官網(wǎng)
下載地址2:清華源地址
下載所需的Anaconda 版本至自己的服務(wù)器(隨便選一個(gè)自己的文件放就行)坠陈。
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系統(tǒng)里面.sh文件是可執(zhí)行的腳本文件萨惑,可以用命令bash來進(jìn)行安裝。
在安裝包文件目錄下輸入安裝命令:
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回車鍵繼續(xù)仇矾,接受許可條款庸蔼。注意按enter別安太快,接下來會(huì)提示是否添加環(huán)境變量贮匕,輸入yes姐仅,否則還得自己配環(huán)境變量。
輸入 python 命令進(jìn)入python環(huán)境,同時(shí)終端的頭部會(huì)出現(xiàn)(base),說明Anaconda已經(jīng)安裝成功了掏膏,接下來可以創(chuàng)建純凈python編譯環(huán)境了劳翰。
(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.
>>>
另外如果需要每次打開服務(wù)器終端時(shí)自動(dòng)激活conda的base環(huán)境,可以輸入如下命令:
conda config --set auto_activate_base false
2、使用conda創(chuàng)建新的python環(huán)境
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:~$
這時(shí)在終端輸入python馒疹,就可以看到我們剛剛建立好的環(huán)境:
(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佳簸、激活環(huán)境
激活之前先查看環(huán)境
(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:~$
激活環(huán)境:
(qiang) qiang@09230:~$ source activate base
(base) qiang@09230:~$
常用命令:
source activate yourEnv # 激活環(huán)境
source deactivate yourEnv # 退出環(huán)境
conda env remove -n yourEnv # 刪除環(huán)境
conda remove --name xxxx --all #徹底刪除舊環(huán)境
conda remove -n yourEnv package_name # 刪除環(huán)境中的某個(gè)包
如果你手動(dòng)修改過配置環(huán)境,需要執(zhí)行下面命令颖变,使用source命令執(zhí)行腳本/.bashrc(/.bashrc會(huì)調(diào)用 /etc/bashrc文件)
source ~/.bashrc