python - 并发使用多个 Python 版本
问题描述
我有一个需要 Python2 的脚本(speedtest-cli),但需要将 Python3 用于 Ansible。
通过 update-alternatives 我设置了“自动模式”和 Python3。我想在我的天真中,我曾以某种方式期望这会自动将 Python2 用于需要它的脚本,但它不起作用。
处理这个问题的最佳方法是什么?
Debian 克星
更新 1:尝试了 furas 的建议。它没有改变任何东西,脚本仍然抛出相同的错误......
rcd@gw:~$ python2 speedtest.py
Traceback (most recent call last):
File "/usr/bin/speedtest-cli", line 6, in <module>
from pkg_resources import load_entry_point
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3019, in <module>
@_call_aside
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3003, in _call_aside
f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 655, in _build_master
ws.require(__requires__)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 963, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 849, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'speedtest-cli==1.0.0' distribution was not found and is required by the application
Traceback (most recent call last):
File "speedtest.py", line 12, in <module>
ping = ping[0].replace(',', '.')
IndexError: list index out of range
请注意它如何解决 /usr/lib/python3 虽然我运行 python2 ....
rcd@gw:~$ type python2
python2 is hashed (/usr/bin/python2)
rcd@gw:~$ type python
python is /usr/bin/python
rcd@gw:~$ type python3
python3 is /usr/bin/python3
rcd@gw:~$ ls -al /usr/bin/python*
lrwxrwxrwx 1 root root 24 Jun 10 19:03 /usr/bin/python -> /etc/alternatives/python
lrwxrwxrwx 1 root root 9 Jan 24 2017 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3779512 Sep 26 2018 /usr/bin/python2.7
lrwxrwxrwx 1 root root 9 Jan 20 2017 /usr/bin/python3 -> python3.5
-rwxr-xr-x 2 root root 4751184 Sep 27 2018 /usr/bin/python3.5
lrwxrwxrwx 1 root root 33 Sep 27 2018 /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config
-rwxr-xr-x 2 root root 4751184 Sep 27 2018 /usr/bin/python3.5m
lrwxrwxrwx 1 root root 34 Sep 27 2018 /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config
lrwxrwxrwx 1 root root 16 Jan 20 2017 /usr/bin/python3-config -> python3.5-config
lrwxrwxrwx 1 root root 10 Jan 20 2017 /usr/bin/python3m -> python3.5m
lrwxrwxrwx 1 root root 17 Jan 20 2017 /usr/bin/python3m-config -> python3.5m-config
然后我尝试了 virtualenv ....
rcd@gw:~$ virtualenv -p /usr/bin/python2.7 venv2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/rcd/venv2.7/bin/python2.7
Also creating executable in /home/rcd/venv2.7/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
rcd@gw:~$ source venv2.7/bin/activate
用python2.7运行,结果一模一样...
(venv2.7) rcd@gw:~$ python2 speedtest.py
Traceback (most recent call last):
File "/usr/bin/speedtest-cli", line 6, in <module>
from pkg_resources import load_entry_point
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3019, in <module>
@_call_aside
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3003, in _call_aside
f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3032, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 655, in _build_master
ws.require(__requires__)
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 963, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 849, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'speedtest-cli==1.0.0' distribution was not found and is required by the application
Traceback (most recent call last):
File "speedtest.py", line 12, in <module>
ping = ping[0].replace(',', '.')
IndexError: list index out of range
我不知道这里发生了什么,无论我做什么,它似乎都想用 Python3 运行....
解决方案
标准方法是在脚本的第一行使用shebang
( #!
) 来通知系统它必须使用什么程序来执行此脚本 - Python 2
, Python 3
, bash
, perl
,PHP
等。
#!/full/path/to/python2
#!/full/path/to/python2.7
#!/full/path/to/python3
#!/full/path/to/python3.6
#!/full/path/to/python3.7
或更普遍和更受欢迎
#!/usr/bin/env python2
#!/usr/bin/env python2.7
#!/usr/bin/env python3
#!/usr/bin/env python3.6
#!/usr/bin/env python3.7
并且脚本必须是可执行的,所以它需要chmod +x script.py
当然你可以随时手动执行
python2 script.py
python2.7 script.py
python3 script.py
python3.6 script.py
python3.7 script.py
推荐阅读
- sql - 尝试将 oracle sql 转换为 Sql Server 日期以进行迁移活动
- javascript - 如何修复 React Native Agora 异步错误
- c - 如何开发包含 PostGIS 库的 PostgreSQL 扩展
- ios - _dispatch_assert_queue_fail 在 iOS 14 上崩溃
- java - 如何将 .equals 答案(对或错)更改为(是或否)?
- proxy - {"message":"没有与这些值匹配的路由"}
- python - **AttributeError:“NoneType”错误。BS的问题
- mysql - 没有用户在线时MySql显示800个连接?
- php - 如何配置 vscode 实时服务器以正确处理 php 文件(与 .html 文件配合得很好)?
- python - Docker 容器中的无效进程