在Ubuntu-16.04上创建虚拟环境时遇到错误。特别是,virtualenv -p python myenv
返回
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 7, in <module>
from virtualenv.__main__ import run_with_catch
File "/usr/local/lib/python3.5/dist-packages/virtualenv/__init__.py", line 3, in <module>
from .run import cli_run, session_via_cli
File "/usr/local/lib/python3.5/dist-packages/virtualenv/run/__init__.py", line 7, in <module>
from ..app_data import make_app_data
File "/usr/local/lib/python3.5/dist-packages/virtualenv/app_data/__init__.py", line 9, in <module>
from platformdirs import user_data_dir
File "/usr/local/lib/python3.5/dist-packages/platformdirs/__init__.py", line 29
result: type[PlatformDirsABC] = getattr(importlib.import_module(module), name)
^
SyntaxError: invalid syntax
意味着Python 3.5中的语法不正确。然而,我已经将Ubuntu上的默认python版本设置为python 3.8.9,即python -V
返回Python 3.8.9
。那么,为什么virtualenv
引用Python 3.5,尽管Python 3.8.9是默认的呢?
附加信息:
我的virtualenv
版本是15.0.1
回溯中的第一行指的是/usr/local/bin/virtualenv
,它看起来像这个
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from virtualenv.__main__ import run_with_catch
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script.pyw|.exe)?$', '', sys.argv[0])
sys.exit(run_with_catch())
更新:
正在运行python3 -m pip install virtualenv
返回
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/lib/python3.8/runpy.py", line 144, in _get_module_details
return _get_module_details(pkg_main_name, error)
File "/usr/lib/python3.8/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "/usr/lib/python3/dist-packages/pip/__init__.py", line 14, in <module>
from pip.utils import get_installed_distributions, get_prog
File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in <module>
from pip.locations import (
File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>
from distutils import sysconfig
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.8/distutils/__init__.py)
您可以使用以下命令来确保virtualenv在默认情况下引用python3.8
python3.8 -m virtualenv myenv