由于导入错误,无法同时运行python 2和3

时间:2022-05-26 10:35:59

I am trying to be able to run python2 and python3 simultaneously on a server that I do not have root access to and which does not have a recent python3 implementation. This works fine on my own server which I do have root access to, but I can't figure out what I am doing wrong.

我正在尝试在服务器上同时运行python2和python3,我没有根访问权限,并且它没有最近的python3实现。这在我自己的服务器上运行得很好,我有根访问权限,但是我不知道我做错了什么。

For reference, the python2 implementation has been installed by administrators on the centos/rocks system in question, and they have both version 2.6.6 and 2.7.5. They also installed python 3.3.2, but there are some things in version 3.4 that I use, so I custom installed version 3.4.3 and put it in my PATH, LIBPATH, and PYTHONPATH. I use that python version exclusively for my own code, and I install libraries there with pip. I have created my own custom rocks module for the python3 version that sets the PATH, LIBPATH and LD_LIBRARY_PATH.

作为参考,python2的实现已经被管理人员安装在centos/rocks系统上,他们的版本是2.6.6和2.7.5。他们也安装了python 3.3.2,但是我在3.4版本中使用了一些东西,所以我自定义安装了version 3.4.3,并将它放在我的路径、LIBPATH和PYTHONPATH中。我将python版本专门用于我自己的代码,并在那里使用pip安装库。我已经为python3版本创建了我自己的自定义rocks模块,它设置了路径、LIBPATH和LD_LIBRARY_PATH。

Everything works fine when I am just running python3 code. The problem comes when I try and simultaneously run python3 and python2 code in the same session.

当我运行python3代码时,一切都运行得很好。当我尝试在同一个会话中同时运行python3和python2代码时,问题就出现了。

I have written code in python3 that makes a system call to run a python2 program1. The problem is that in order to run my python3 code, I have my PYTHONPATH set to only contain python3 libraries. If python2 libraries are in that PATH, I get errors similar to this:

我用python3编写了代码,它执行一个系统调用来运行一个python2程序1。问题是,为了运行我的python3代码,我将我的PYTHONPATH设置为只包含python3库。如果python2库在这条路径中,我就会得到类似的错误:

Failed to import the site module
Traceback (most recent call last):
  File "/opt/scipy/2.7/lib/python2.7/site-packages/site.py", line 73, in <module>
    __boot()
  File "/opt/scipy/2.7/lib/python2.7/site-packages/site.py", line 2, in __boot
    import sys, imp, os, os.path   
  File "/oasis/projects/nsf/sua137/peanut/usr/lib/python3.4/imp.py", line 22, in <module>
    from importlib import util
  File "/oasis/projects/nsf/sua137/peanut/usr/lib/python3.4/importlib/util.py", line 12, in <module>
    from contextlib import contextmanager
  File "/oasis/projects/nsf/sua137/peanut/usr/lib/python3.4/contextlib.py", line 4, in <module>
    from collections import deque
  File "/oasis/projects/nsf/sua137/peanut/usr/lib/python3.4/collections/__init__.py", line 17, in <module>
    from reprlib import recursive_repr as _recursive_repr
  File "/opt/biotools/qiime/lib/python2.7/site-packages/reprlib/__init__.py", line 8, in <module>
    raise ImportError('Cannot import module from python-future source folder')
ImportError: Cannot import module from python-future source folder

However, if I strip out all of the python2 stuff from the PYTHONPATH, then python2 scripts fail with errors like this:

但是,如果我从PYTHONPATH中去掉所有的python2的东西,那么python2脚本就会出现这样的错误:

File "/oasis/projects/nsf/sua137/peanut/usr/lib/python3.4/site.py", line 176
file=sys.stderr)
    ^
SyntaxError: invalid syntax

The only way I have found to avoid this is to include an explicit PYTHONPATH in the call to the python2 code. That does work, but it is hugely cumbersome and ugly.

我发现避免这种情况的唯一方法是在调用python2代码时包含一个显式的PYTHONPATH。这是可行的,但它非常笨重和丑陋。

I used to keep all of my python3 stuff in the PYTHON3PATH, and the python2 stuff in the PYTHONPATH, but now python3 appears to completely ignore the PYTHON3PATH and only use the PYTHONPATH.

我以前把所有的PYTHON3PATH中的勾股定理和PYTHONPATH中的勾股定理,但是现在python3完全忽略了PYTHON3PATH,只使用了PYTHONPATH。

Again, this problem doesn't occur on my own personal server, which runs arch linux and has python 3.4 and python 2.7 running side-by-side happily even though my PYTHONPATH contains directories for both.

同样,这个问题不会出现在我自己的个人服务器上,它运行arch linux,并且python 3.4和python 2.7并排运行,尽管我的PYTHONPATH包含这两个目录。

I am sure I am doing something stupid here, but I don't know what it is.

我肯定我在这里做了件蠢事,但我不知道那是什么。


1 Footnote to stave off silly comments: They python2 script is not written by me, the python3 code is a simple job management system that coordinates multi-threading none-parallel code on a remote machine. It is supposed to run any standalone program in parallel, but it is failing with python2 scripts. So I can't just switch one or the other to either version 2 or 3, and I can't merge the two by using 2to3 and importing.

为了避免愚蠢的评论,脚注:它们不是我写的,python3代码是一个简单的作业管理系统,在远程机器上协调多线程的非并行代码。它应该并行运行任何独立的程序,但是在python2脚本中它失败了。所以我不能只把一个或另一个转换为版本2或版本3,也不能通过使用2to3和导入合并两个版本。

1 个解决方案

#1


2  

Calling python with the explicit path or version resolved the issue. For example:

使用显式路径或版本调用python解决了这个问题。例如:

$ python3.x
$ path_to/python

#1


2  

Calling python with the explicit path or version resolved the issue. For example:

使用显式路径或版本调用python解决了这个问题。例如:

$ python3.x
$ path_to/python