Linux学习笔记:安装python
一般linux自帶python2,如果需要python3以上版本,可以不需要卸載自帶的python2,二者可以共存。只需要配置相應的環境變量即可。
具體回答可以參考這篇文章?https://stackoverflow.com/questions/26282986/how-to-update-python-2-7-to-python-3-in-linux
其內容是:
Python 2 and 3 can safely be installed together. They install most of their files in different locations. So if the prefix is /usr/local, you'll find the library files in /usr/local/lib/pythonX.Y/ where X.Y are the major and minor version numbers.
The only point of contention is generally is the file python itself, which is generally a symbolic link.
Currently it seems most operating systems still use Python 2 as the default, which means that python is a symbolic link to python2. This is also recommended in the Python documentation.
It is best to leave it like that for now. Some programs in your distributions may depend on this, and might not work with Python 3.
So install Python 3 (3.5.1 is the latest version at this time) using your favorite package manager or compiling it yourself. And then use it by starting python3 or by putting #!/usr/bin/env python3 as the first line in your Python 3 scripts and making them executable (chmod +x <file>)
上面內容的大意是:python2和3可以共存,可以安裝在不同的位置。例如 /usr/local/lib/pythonX.Y,這X.Y代表了主要版本。而需要配置全局環境變量其實就是python這個文件自身,它可以是一個快捷方式。同時建議不要隨意修改系統內置的python版本。(之前我在centos7上面修改了python版本,導致yum命令使用出錯)
如果需要執行依賴python3的python腳本,只需要要在文件第一行加上?#!/usr/bin/env python3 ,或者加上#!/usr/bin/python3。二者的區別可以見這篇文章?https://www.jianshu.com/p/400c612381dd
如果提示 /usr/bin/python^M: bad interpreter: No such file or directory,這是因為windows和linux行尾標識符不同,可以用文本編輯工具File - Conversions - DOS - UNIX,或者 Edit - EOL Conversion -UNIX(LF)。
如何安裝python3版本:
yum install epel-release
yum install python36
上面的python36是倉庫中的最新版本,后面可能會更新到37等更高的版本。
如果依賴python3的腳本,同時依賴其他模塊,此時用pip install package就會出現問題,因為這個pip命令其實是去執行系統內置的python2中的pip,如果模塊不兼容python2就會報錯。
在這個問題用有解答?https://stackoverflow.com/questions/50408941/recommended-way-to-install-pip3-on-centos7
解決辦法是安裝pip3,這個pip3是python3的包管理器,如果使用pip3 install?package,就可以安裝相應的模塊到python3環境下。
安裝pip3步驟(接著上面python36安裝完成后):
yum install python36-devel
yum install python36-setuptools
easy_install-3.6 pip
然后查看pip3的版本
pip3 --version
盡量不要刪除和修改linux內置的python版本。python2和python3是可以共存的。
轉載于:https://www.cnblogs.com/colin220/p/10669064.html
總結
以上是生活随笔為你收集整理的Linux学习笔记:安装python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: file_get_contents 在本
- 下一篇: linux 其他常用命令