最近在部署一个flask项目,中间遇到的一些坑,记录一下:
想用virutalenv来控制项目的环境,毕竟跟别人使用同一台server,以免有冲突。这个时候就需要安装一些依赖包的,就需要pip,但是默认是python 2.7的pip,这就比较扯淡了。尝试了一下:
root@ubuntu:~/smtvenv$ pip3
-bash: pip3: command not found
没有pip3,那就安装,如下:
root@ubuntu:~/smtvenv$ sudo apt-get install pip3
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package pip3
说找不到这个叫pip3的包,查了一下,原来安装姿势不对。python3下,正确的打开姿势是:
root@ubuntu:~/smtvenv$ sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libpython3-dev libpython3.5-dev python3-dev python3-setuptools python3-wheel python3.5-dev
Suggested packages:
python-setuptools-doc
The following NEW packages will be installed:
libpython3-dev libpython3.5-dev python3-dev python3-pip python3-setuptools python3-wheel python3.5-dev
0 upgraded, 7 newly installed, 0 to remove and 95 not upgraded.
Need to get 38.0 MB of archives.
After this operation, 55.2 MB of additional disk space will be used.
Do you want to continue? [Y/n]
root@ubuntu:~/smtvenv$ pip3 install virtualenv
Collecting virtualenv
Downloading https://files.pythonhosted.org/packages/5e/6a/fa7e7f533595402040c831500bb10576e1f4b8f54d476f3994c7c55d8f5e/virtualenv-16.7.3-py2.py3-none-any.whl (3.3MB)
100% |████████████████████████████████| 3.3MB 551kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv
You are using pip version 8.1.1, however version 19.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
提示说pip version太老了,那就升级:
root@ubuntu:~/smtvenv$ pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/8d/07/f7d7ced2f97ca3098c16565efbe6b15fafcba53e8d9bdb431e09140514b0/pip-19.2.2-py2.py3-none-any.whl (1.4MB)
100% |████████████████████████████████| 1.4MB 1.2MB/s
Installing collected packages: pip
Successfully installed pip-19.2.2
配置使用python3启动virtualenv
root@ubuntu:~/smtvenv$ virtualenv -p /usr/bin/python3 ticket-kb
Running virtualenv with interpreter /usr/bin/python3
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/jbe1szh/smtvenv/ticket-kb/bin/python3
Also creating executable in /home/jbe1szh/smtvenv/ticket-kb/bin/python
Installing setuptools, pip, wheel...
done.
进去虚拟环境
root@ubuntu:~/smtvenv$ cd ticket-kb/
激活虚拟环境:
root@ubuntu:~/smtvenv$ source bin/activate
(ticket-kb) root@ubuntu:~/smtvenv/ticket-kb$
可以看到,最前面多了一个括号和文件夹名字,这个就证明你已经在虚拟环境里了。
(ticket-kb) root@ubuntu:~/smtvenv/ticket-kb$
查看一下python的版本:
(ticket-kb) root@ubuntu:~/smtvenv/ticket-kb$ python -V
Python 3.5.2
退出虚拟环境,只需要输入deactivate即可,如下:
(ticket-kb) root@ubuntu:~/smtvenv/ticket-kb$ deactivate