如何让 virtualenv 在 Ubuntu 上看到默认的 python 版本
how to make virtualenv see the default python version on Ubuntu
我在 Ubuntu-16.04 上创建虚拟环境时遇到错误。
特别是 virtualenv -p python myenv
returns
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
returns 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
returns
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
我在 Ubuntu-16.04 上创建虚拟环境时遇到错误。
特别是 virtualenv -p python myenv
returns
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
returns 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
returns
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