Python环境搭建

1. Python

Ubuntu 中已自动安装 Python。请花点时间确认(通过发出 python -V 命令)您的系统中是否已经安装了下列某个 Python 版本:

xiaomeng@xiaomeng:~$ python -V
Python 3.5.2
xiaomeng@xiaomeng:~$ python2 -V
Python 2.7.12
xiaomeng@xiaomeng:~$ python3 -V
Python 3.5.2

2. pip安装与卸载

Ubuntu 上通常已安装 pip 或 pip3 软件包管理器。请花点时间确认(通过发出 pip -Vpip3 -V 命令)是否已经安装 pip 或 pip3。

关于pip2与pip3的问题:https://www.zhihu.com/question/21653286

xiaomeng@xiaomeng:~$ pip -V
pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
xiaomeng@xiaomeng:~$ pip2 -V
pip 10.0.1 from /home/xiaomeng/.local/lib/python3.5/site-packages/pip (python 3.5)
xiaomeng@xiaomeng:~$ pip3 -V
pip 10.0.1 from /home/xiaomeng/.local/lib/python3.5/site-packages/pip (python 3.5)
xiaomeng@xiaomeng:~$ python2 -m pip -V
pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

如果未安装 pip 或 pip3 版本 8.1 或更高版本,请发出以下命令来安装 pip 或升级到最新版本:

sudo apt-get install python-pip python-dev   # for Python 2.7
sudo apt-get install python3-pip python3-dev # for Python 3.n

卸载:
sudo apt-get remove python-pip
sudo apt-get remove python3-pip

更新:
sudo pip install --upgrade pip
sudo pip3 install --upgrade pip

使用pip3 出现以下错误:

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

或者pip 时也出现这个问题
这是pip 10.0.0版本的BUG

解决办法

修改 /usr/bin/pip3 文件

from pip import main
if __name__ == '__main__':
    sys.exit(main())

改为:

from pip import __main__
if __name__ == '__main__':
    sys.exit(__main__._main())

3. 虚拟环境的搭建

1 安装 pip install virtualenv pip install virtualenvwrapper
2 查找:通过sudo find / -name virtualenvwrapper.sh查找文件得到路径
/home/xiaomeng/.local/bin/virtualenvwrapper.sh
3 配置 vi ~/.bashrc
在末尾添加

export WORKON_HOME=$HOME/.virtualenvs
source /home/xiaomeng/.local/bin/virtualenvwrapper.sh 

4 更新 source ~/.bashrc
5 创建虚拟环境

mkvirtualenv test(环境名) #该环境只有python2
mkvirtualenv --python=/usr/bin/python3 test1 #该环境只有python3

即可直接进入虚拟环境(test) xiaomeng@xiaomeng:~$
6 退出 deactivate
7 再次进入虚拟环境workon test
8 虚拟环境的安装目录为~/.virtualenv
9 删除虚拟环境:rmvirtualenv test
10 查看现有虚拟环境:workon

4. 软件包的安装与卸载

python软件包官网:https://pypi.org
python安装官方文档:https://docs.python.org/3/installing/

## 对于 Linux ##

sudo pip install sth
# 或者明确版本
sudo pip2 install sth
sudo pip3 install sth
sudo python2 -m pip install sth
sudo /path/to/python -m pip install sth

## 对于 Windows NT ##

# 如果仅安装 python2
pip install sth
# 如果安装有 python3, 则需要明确 pip 版本
py -2 -m pip install sth
py -3 -m pip install sth

== 针对 Linux ==
在 pip, pip2, 或者 pip3 install 失败时,可以尝试如下操作:

# 以安装 youtube-dl 举例
# proxy_host:proxy_port 为代理地址
export https_proxy=proxy_host:proxy_port
sudo -H  pip install --upgrade youtube-dl
# 或 sudo -H  pip2 install --upgrade youtube-dl
# 或 sudo -H  pip3 install --upgrade youtube-dl
# 或 sudo -H  python3.5 -m pip install youtube-dl

pip下载的数据包默认的安装目录
pip:/usr/local/lib/python2.7/dist-packages
pip3:/usr/local/lib/python3.5/dist-packages

5. 常用的命令

whereis python
which python2
which python3.6
vi ~/.bashrc
pip list
pip freeze
python -V = python--version
sudo pip install --upgrade pip 或者 sudo pip install -U pip 

6. 设置默认python3

Ubuntu默认Python为2.7,所以安装Python包时安装的为py2的包。

可以利用alternatives机制更改py3为默认。
shell里执行:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

此时,在命令端输入安装命令则会自动安装支持py3的包。
如:
sudo apt-get install python-numpy
sudo apt-get install python-scipy

如果要切换到Python2,执行:
sudo update-alternatives --config python
按照提示输入选择数字回车即可。在py2下执行以上命令安装的是py2对应的安装包,系统会自动选择下载。

7. Python的-m参数

通过python --help显示-m的意思为:将库中的python模块用作脚本去运行

posted @ 2019-09-17 21:50  kolane  阅读(212)  评论(0编辑  收藏  举报