使用virtualenv和virtualenvwrapper

当使用virtualenv或者mkvirtualenv进行建立虚拟环境时,有时候会出现如下错误:
pzr@pzr-All-Series:~$ mkvirtualenv myenvv
New python executable in /home/pzr/.virtualenvs/myenvv/bin/python
Installing setuptools, pip, wheel…
Complete output from command /home/pzr/.virtualenvs/myenvv/bin/python - setuptools pip wheel:
Collecting setuptools
Exception:
Traceback (most recent call last):
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/basecommand.py”, line 215, in main
status = self.run(options, args)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/commands/install.py”, line 324, in run
requirement_set.prepare_files(finder)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_set.py”, line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_set.py”, line 554, in _prepare_file
require_hashes
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/req/req_install.py”, line 278, in populate_link
self.link = finder.find_requirement(self, upgrade)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/index.py”, line 465, in find_requirement
all_candidates = self.find_all_candidates(req.name)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/index.py”, line 423, in find_all_candidates
for page in self._get_pages(url_locations, project_name):
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/index.py”, line 568, in _get_pages
page = self._get_page(location)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/index.py”, line 683, in _get_page
return HTMLPage.get_page(link, session=self.session)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/index.py”, line 792, in get_page
“Cache-Control”: “max-age=600”,
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py”, line 488, in get
return self.request(‘GET’, url, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/download.py”, line 386, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py”, line 475, in request
resp = self.send(prep, **send_kwargs)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/sessions.py”, line 596, in send
r = adapter.send(request, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/cachecontrol/adapter.py”, line 47, in send
resp = super(CacheControlAdapter, self).send(request, **kw)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/adapters.py”, line 390, in send
conn = self.get_connection(request.url, proxies)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/adapters.py”, line 290, in get_connection
proxy_manager = self.proxy_manager_for(proxy)
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/adapters.py”, line 184, in proxy_manager_for
**proxy_kwargs
File “/usr/local/lib/python2.7/dist-packages/virtualenv_support/pip-9.0.1-py2.py3-none-any.whl/pip/_vendor/requests/adapters.py”, line 43, in SOCKSProxyManager
raise InvalidSchema(“Missing dependencies for SOCKS support.”)

InvalidSchema: Missing dependencies for SOCKS support.

…Installing setuptools, pip, wheel…done.
Traceback (most recent call last):
File “/usr/local/bin/virtualenv”, line 11, in
sys.exit(main())
File “/usr/local/lib/python2.7/dist-packages/virtualenv.py”, line 713, in main
symlink=options.symlink)
File “/usr/local/lib/python2.7/dist-packages/virtualenv.py”, line 945, in create_environment
download=download,
File “/usr/local/lib/python2.7/dist-packages/virtualenv.py”, line 901, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File “/usr/local/lib/python2.7/dist-packages/virtualenv.py”, line 797, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /home/pzr/.virtualenvs/myenvv/bin/python - setuptools pip wheel failed with error code 2

一直以为是下面的OSerror的错误原因,结果后来发现当将ubuntu系统的网络中不使用代理,并且关闭s以及浏览器所有的socks代理,就可以了;
这个问题一直在装opencv以及tensorflow,以及在建立独立的pyton环境都会遇到,建立了如下的cv工作

pzr@pzr-All-Series:~$ workon cv
(cv) pzr@pzr-All-Series:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2

注意使用virtualenv和mkvirtualenv建立虚拟环境时不一样的如下:
1、virtualenv建立时,指定python版本,通过-p指定

pzr@pzr-All-Series:~$ virtualenv env1
New python executable in /home/pzr/env1/bin/python
Installing setuptools, pip, wheel...done.
pzr@pzr-All-Series:~$ virtualenv -p python3 my1
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/pzr/my1/bin/python3
Also creating executable in /home/pzr/my1/bin/python
Installing setuptools, pip, wheel...done.
pzr@pzr-All-Series:~$ source ~/my1/bin/activate
(my1) pzr@pzr-All-Series:~$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

2、进入virtualenv的环境,使用 source ~<虚拟环境名>/bin/activate
3、退出虚拟环境使用deactivate
4、删除虚拟环境使用rm -rf <虚拟环境>

二、virtualenvwrapper
virtualenvwrapper是virtualenv的扩展管理包,可以更方便管理虚拟环境

A、将所有虚拟环境整合在一个目录下
B、管理(新增,删除,复制)虚拟环境
C、切换虚拟环境

1、建立虚拟环境使用mkvirtualenv ,可以使用-p指定python类型

pzr@pzr-All-Series:~$ mkvirtualenv myenv1
New python executable in /home/pzr/.virtualenvs/myenv1/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/pzr/.virtualenvs/myenv1/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/pzr/.virtualenvs/myenv1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/pzr/.virtualenvs/myenv1/bin/preactivate
virtualenvwrapper.user_scripts creating /home/pzr/.virtualenvs/myenv1/bin/postactivate
virtualenvwrapper.user_scripts creating /home/pzr/.virtualenvs/myenv1/bin/get_env_details
(myenv1) pzr@pzr-All-Series:~$ deactivate
pzr@pzr-All-Series:~$ workon myenv1
(myenv1) pzr@pzr-All-Series:~$ 

还需要设置环境变量

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
 echo "export WORKON_HOME=$HOME/.<存放虚拟环境的文件夹名>" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

source ~/.bashrc #更新

2、使用workon <虚拟环境名> 进入
3、使用deactivate退出
4、删除虚拟环境使用rm -rf <虚拟环境>

你可能感兴趣的:(linux)