在我的情况下,当运行
workon <foo>
时,我会得到类似的错误:
xb@dnxb:/tmp$ workon foo
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/xiaobai/.local/lib/python3.6/site-packages/virtualenvwrapper/hook_loader.py", line 16, in <module>
from stevedore import ExtensionManager
File "/home/xiaobai/.local/lib/python3.6/site-packages/stevedore/__init__.py", line 11, in <module>
from .extension import ExtensionManager
File "/home/xiaobai/.local/lib/python3.6/site-packages/stevedore/extension.py", line 19, in <module>
from . import _cache
File "/home/xiaobai/.local/lib/python3.6/site-packages/stevedore/_cache.py", line 31, in <module>
import importlib_metadata
File "/home/xiaobai/.local/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 9, in <module>
import zipp
File "/home/xiaobai/.local/lib/python3.6/site-packages/zipp.py", line 153
SyntaxError: Non-ASCII character '\xe2' in file /home/xiaobai/.local/lib/python3.6/site-packages/zipp.py on line 154, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
...
错误显示在开头的是 python2.7/runpy.py
,但我希望整个过程都使用 python 3。
然后当我执行 source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
时,也出现了相同的错误。
所以我检查了文件,并注意到它基于 which python
命令的结果使用 python 2,在我的系统中 /usr/bin/python
符号链接到 python2.7:
xb@dnxb:/tmp$ grep -n which /usr/share/virtualenvwrapper/virtualenvwrapper.sh
50: VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
316: typeset exe_path="$(command \which "$1" | (unset GREP_OPTIONS; command \grep -v "not found"))"
xb@dnxb:/tmp$ which python
/usr/bin/python
所以只需编辑此行,将路径硬编码为/usr/bin/python3
(我的python3路径),就不会再出现source
或workon
的错误了:
xb@dnxb:/tmp$ sudo sed -i 's/VIRTUALENVWRAPPER_PYTHON="$(command \\which python)"/VIRTUALENVWRAPPER_PYTHON=\/usr\/bin\/python3/g' /usr/share/virtualenvwrapper/virtualenvwrapper.sh
xb@dnxb:/tmp$ grep -n which /usr/share/virtualenvwrapper/virtualenvwrapper.sh
316: typeset exe_path="$(command \which "$1" | (unset GREP_OPTIONS; command \grep -v "not found"))"
xb@dnxb:/tmp$ grep -n python3 /usr/share/virtualenvwrapper/virtualenvwrapper.sh
50: VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
xb@dnxb:/tmp$ workon foo
(foo) xb@dnxb:/tmp$
当然,如果你想动态地使用Python 2或3而不编辑此脚本,你可以设置
VIRTUALENVWRAPPER_PYTHON
环境变量。