【发布时间】:2018-05-24 22:41:49
【问题描述】:
subprocess.popen 和 subprocess.run,在调用“file.py”时,在 Linux 上都会出现导入错误。 Linux 上的 Python 3.6.4。 Windows 上的 Python 3.6.5。 Windows 没有问题。 Linux 抛出错误。这些调用旨在对原始流程/脚本独立且不可见。
对于 Linux(以下任一行):
proc = subprocess.run("python3 foo.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
proc = subprocess.Popen("python3 foo.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
对于 Windows(以下任一行):
proc = subprocess.run("python bar.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
proc = subprocess.Popen("python bar.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
另外,请注意,运行各个脚本不会发生意外;只有当您从另一个脚本调用它时,它才会给出错误。有什么想法吗?
编辑: 例如,假设这是 foo.py:
import requests, urllib.request as urllib2, wget, ftplib, sys, tkinter as tk, tkinter.ttk as ttk, serial, threading, platform, time, socket, os, json, struct, logging, pickle, re, queue, subprocess, uuid, netifaces as nif, zipfile, ctypes
from platform import system as OS_VER
from PIL import Image, ImageTk
LibList = [requests, urllib2, wget, ftplib, sys, tk, ttk, serial, threading, platform, time, socket, os, json, struct, logging, pickle, re, queue, subprocess, uuid, nif, zipfile, ctypes, Image, ImageTk, platform]
for x in range(len(LibList)):
try: print(LibList[x].__file__)
except: print(LibList[x])
这是 bar.py
proc = subprocess.Popen("python3 foo.py", shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
在 Linux 上运行 bar.py 和 foo.py 会出错,但在 Windows 上不会。 跳过 bar.py 并直接运行 foo.py 虽然在 Linux 上运行良好。
【问题讨论】:
-
这两个都给出了
AttributeError,因为没有什么叫做subprocess.popen。此外,Windows 运行两个不同的脚本,其中一个使用python运行,另一个使用python3运行,而 Linux 运行相同的脚本并在两者中使用相同的解释器。而且它们都没有运行名为file.py的东西,这使得调试file.py遇到的任何错误都非常困难。 -
这实际上是一个错字,现在已修复。脚本的名称是通用的。
-
python3是否保证与您用于运行此脚本的 Python 安装相同? (通常,您使用sys.executable而不是"python3"。当然,如果您不需要它,则不要使用shell=True。但是如果您想以艰难的方式做到这一点,则必须手动验证无论如何,它具有相同的效果,因为我们无法为您检查。) -
是的,应该是。也尝试了 sys.executable 没有任何变化。
-
它只能在 Linux 中使用实时 python 解释器。 'subprocess.Popen("/home/
/Documents/foo.py")'
标签: python subprocess python-import