subprocess.popen 和 subprocess.run 在调用另一个 file.py 时会在 linux 上出现导入错误。 Python 3.6.4
subprocess.popen and subprocess.run when calling another file.py gives import error on linux. Python 3.6.4
subprocess.popen 和 subprocess.run,当调用 "file.py" 时,两者都会在 Linux 上出现导入错误。 Python 3.6.4 Linux。 Python 3.6.5 Windows。 Windows 工作没有问题。 Linux 抛出错误。这些调用旨在独立且对原始 process/script 不可见。
For 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)
For 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)
运行 bar.py 和 foo.py 在 Linux 上出现错误,但在 windows 上没有。
跳过 bar.py 和 运行 foo.py 虽然在 Linux.
上直接工作正常
我找到的唯一解决方案是创建一个 shell 脚本并使用 subprocess.Popen
或 subprocess.run
调用它。
示例:
subprocess.Popen('Foo.sh')
和 Foo.sh 文件:
#!/usr/bin/env bash
python3 Foo.py > log.log &
在 Linux 中分叉进程需要 &。否则,它是阻塞的,直到关闭才会继续。
subprocess.popen 和 subprocess.run,当调用 "file.py" 时,两者都会在 Linux 上出现导入错误。 Python 3.6.4 Linux。 Python 3.6.5 Windows。 Windows 工作没有问题。 Linux 抛出错误。这些调用旨在独立且对原始 process/script 不可见。
For 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)
For 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)
运行 bar.py 和 foo.py 在 Linux 上出现错误,但在 windows 上没有。 跳过 bar.py 和 运行 foo.py 虽然在 Linux.
上直接工作正常我找到的唯一解决方案是创建一个 shell 脚本并使用 subprocess.Popen
或 subprocess.run
调用它。
示例:
subprocess.Popen('Foo.sh')
和 Foo.sh 文件:
#!/usr/bin/env bash
python3 Foo.py > log.log &
在 Linux 中分叉进程需要 &。否则,它是阻塞的,直到关闭才会继续。