creating python virtualenv keep showing error of syntax

Viewed 3356

After a successful installation of virtualenv on centos7

pip install virtualenv

while creating new virtualenv it keeps showing below mentioned error even if I am checking

virtualenv --version

This also shows the same error.

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

I tried to uninstall and re-install it again but so far no luck.

2 Answers

You're using Python 2.7 (which is end-of-life) with a version of Virtualenv that doesn't support Python 2.7.

In order of preference,

  • stop using Python 2.7 and use Python 3.x (currently 3.8) instead
  • or install an older version of Virtualenv (e.g. pip install "virtualenv<20.0", or whatever major version of Virtualenv ends up working for you).

In my case I get this similar error when running 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
...

The error showing python2.7/runpy.py at the beginning but I expect to use python 3 in whole process.

Then I get same error when source /usr/share/virtualenvwrapper/virtualenvwrapper.sh too.

So I check the file and noticed it use python 2 based on result of which python command, which /usr/bin/python symlink to python2.7 in my system:

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

So simply edit this line to hard-coded path /usr/bin/python3 (my python3 path), no more error either of source or 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$ 

And of course, you can set VIRTUALENVWRAPPER_PYTHON environment variable if you want to use python 2 or 3 dynamically without edit this script.

Related