creating python virtualenv 一直显示语法错误
creating python virtualenv keep showing error of syntax
在centos7上成功安装virtualenv后
pip install virtualenv
在创建新的 virtualenv 时它一直显示下面提到的错误,即使我正在检查
virtualenv --version
这也显示了同样的错误。
Traceback (most recent call last):
File "/usr/bin/virtualenv", line 7, in <module>
from virtualenv.__main__ import run_with_catch
File "/usr/lib/python2.7/site-packages/virtualenv/__init__.py", line 3, in <module>
from .run import cli_run
File "/usr/lib/python2.7/site-packages/virtualenv/run/__init__.py", line 12, in <module>
from .plugin.activators import ActivationSelector
File "/usr/lib/python2.7/site-packages/virtualenv/run/plugin/activators.py", line 6, in <module>
from .base import ComponentBuilder
File "/usr/lib/python2.7/site-packages/virtualenv/run/plugin/base.py", line 9, in <module>
from importlib_metadata import entry_points
File "/usr/lib/python2.7/site-packages/importlib_metadata/__init__.py", line 9, in <module>
import zipp
File "/usr/lib/python2.7/site-packages/zipp.py", line 153
SyntaxError: Non-ASCII character '\xe2' in file /usr/lib/python2.7/site-packages/zipp.py on line 154, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
我尝试卸载并重新安装它,但到目前为止没有成功。
您正在使用 Python 2.7(已停产)和不支持 Python 2.7 的 Virtualenv 版本。
按优先顺序排列,
- 停止使用 Python 2.7 并使用 Python 3.x(当前为 3.8)
- 或安装旧版本的 Virtualenv(例如
pip install "virtualenv<20.0"
,或任何最终适合你的 Virtualenv 主要版本)。
在我的例子中,当 运行 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
时也得到同样的错误。
所以我检查文件并注意到它使用 python 2 基于 which python
命令的结果,/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 "" | (unset GREP_OPTIONS; command \grep -v "not found"))"
xb@dnxb:/tmp$ which python
/usr/bin/python
所以只需将此行编辑为 hard-coded 路径 /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 "" | (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
环境变量。
在centos7上成功安装virtualenv后
pip install virtualenv
在创建新的 virtualenv 时它一直显示下面提到的错误,即使我正在检查
virtualenv --version
这也显示了同样的错误。
Traceback (most recent call last):
File "/usr/bin/virtualenv", line 7, in <module>
from virtualenv.__main__ import run_with_catch
File "/usr/lib/python2.7/site-packages/virtualenv/__init__.py", line 3, in <module>
from .run import cli_run
File "/usr/lib/python2.7/site-packages/virtualenv/run/__init__.py", line 12, in <module>
from .plugin.activators import ActivationSelector
File "/usr/lib/python2.7/site-packages/virtualenv/run/plugin/activators.py", line 6, in <module>
from .base import ComponentBuilder
File "/usr/lib/python2.7/site-packages/virtualenv/run/plugin/base.py", line 9, in <module>
from importlib_metadata import entry_points
File "/usr/lib/python2.7/site-packages/importlib_metadata/__init__.py", line 9, in <module>
import zipp
File "/usr/lib/python2.7/site-packages/zipp.py", line 153
SyntaxError: Non-ASCII character '\xe2' in file /usr/lib/python2.7/site-packages/zipp.py on line 154, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
我尝试卸载并重新安装它,但到目前为止没有成功。
您正在使用 Python 2.7(已停产)和不支持 Python 2.7 的 Virtualenv 版本。
按优先顺序排列,
- 停止使用 Python 2.7 并使用 Python 3.x(当前为 3.8)
- 或安装旧版本的 Virtualenv(例如
pip install "virtualenv<20.0"
,或任何最终适合你的 Virtualenv 主要版本)。
在我的例子中,当 运行 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
时也得到同样的错误。
所以我检查文件并注意到它使用 python 2 基于 which python
命令的结果,/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 "" | (unset GREP_OPTIONS; command \grep -v "not found"))"
xb@dnxb:/tmp$ which python
/usr/bin/python
所以只需将此行编辑为 hard-coded 路径 /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 "" | (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
环境变量。