运行一个脚本通过另一个脚本不理解PIL
Running a script through another script does not understand PIL
当我 运行 并单击预览按钮时,os.system() 运行s 并执行 test.py 但不识别我已经有 PIL。并给出此错误:
Traceback (most recent call last):
File "/Users/adamd/Desktop/psychedelic_euphoria/test.py", line 2, in <module>
from PIL import ImageTk, Image
ImportError: No module named PIL
下面是当我按下预览时将打开文件 test.py 的脚本。
from tkinter import *
from tkinter import filedialog
import shutil
import os
location = '/Users/adamd/Desktop/psychedelic_euphoria'
def UploadAction(event=None):
filename = filedialog.askopenfilename()
shutil.move(filename, location )
def OpenPreview():
os.system('python /Users/adamd/Desktop/psychedelic_euphoria/test.py')
root = Tk()
root.title('Phychedelic Euphoria')
root.geometry("300x200")
bt_import = Button(root, text='Open', command=UploadAction)
bt_quit = Button(root, text='Quit',command=root.quit)
bt_preview = Button(root, text= 'preview',command=OpenPreview)
bt_import.pack(padx=20, pady=(20,0))
bt_quit.pack(padx=20, pady=(20,0))
bt_preview.pack(padx=20, pady=(20,0))
root.mainloop()
下面那个是test.py:
from tkinter import *
from PIL import ImageTk, Image
import cv2
view = Tk()
app = Frame(view, bg="white")
app.grid()
lmain = Label(app)
lmain.grid()
cap = cv2.VideoCapture('output.avi')
def video_stream():
_, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(1, video_stream)
video_stream()
view.mainloop()
如果我单独 运行 test.py 它工作正常但是一旦我 运行 它通过上面的第一个脚本它不明白模块 PIL 在哪里。
有没有办法解决这个问题,还是我必须改变整个事情?
如果有帮助,我用macos。
运行 VS 代码中的新脚本:
import sys
print(sys.executable)
现在在您的 os.system()
中使用该路径
当我 运行 并单击预览按钮时,os.system() 运行s 并执行 test.py 但不识别我已经有 PIL。并给出此错误:
Traceback (most recent call last):
File "/Users/adamd/Desktop/psychedelic_euphoria/test.py", line 2, in <module>
from PIL import ImageTk, Image
ImportError: No module named PIL
下面是当我按下预览时将打开文件 test.py 的脚本。
from tkinter import *
from tkinter import filedialog
import shutil
import os
location = '/Users/adamd/Desktop/psychedelic_euphoria'
def UploadAction(event=None):
filename = filedialog.askopenfilename()
shutil.move(filename, location )
def OpenPreview():
os.system('python /Users/adamd/Desktop/psychedelic_euphoria/test.py')
root = Tk()
root.title('Phychedelic Euphoria')
root.geometry("300x200")
bt_import = Button(root, text='Open', command=UploadAction)
bt_quit = Button(root, text='Quit',command=root.quit)
bt_preview = Button(root, text= 'preview',command=OpenPreview)
bt_import.pack(padx=20, pady=(20,0))
bt_quit.pack(padx=20, pady=(20,0))
bt_preview.pack(padx=20, pady=(20,0))
root.mainloop()
下面那个是test.py:
from tkinter import *
from PIL import ImageTk, Image
import cv2
view = Tk()
app = Frame(view, bg="white")
app.grid()
lmain = Label(app)
lmain.grid()
cap = cv2.VideoCapture('output.avi')
def video_stream():
_, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
lmain.after(1, video_stream)
video_stream()
view.mainloop()
如果我单独 运行 test.py 它工作正常但是一旦我 运行 它通过上面的第一个脚本它不明白模块 PIL 在哪里。
有没有办法解决这个问题,还是我必须改变整个事情? 如果有帮助,我用macos。
运行 VS 代码中的新脚本:
import sys
print(sys.executable)
现在在您的 os.system()