[英]Importing a python module/site-package (e.g. scipy) works from a script (a.py), but not from tkinter button command
Importing a python site-package (eg 'scipy') works fine from a script test.py, but not from tkinter button command. 从脚本test.py导入python站点包(例如'scipy')可以正常工作,但不能从tkinter button命令导入。 When click the button, error shows: 当点击按钮时,错误提示:
ModuleNotFoundError: No module named 'scipy' ModuleNotFoundError:没有名为“ scipy”的模块
test.py test.py
import scipy
print ("hello world")
GUI.py 图形用户界面
import sys
import os
import tkinter
top=tkinter.Tk()
def startCamera():
os.system('python test.py')
B=tkinter.Button(top,text="hello",command= startCamera)
B.pack()
top.mainloop()
Both the test.py and GUI.py are in a same folder: C:\\Users\\Breda\\PycharmProjects\\face_reg\\face test.py和GUI.py都位于同一文件夹中:C:\\ Users \\ Breda \\ PycharmProjects \\ face_reg \\ face
scipy package in: C:\\Users\\Breda\\Anaconda3\\Lib\\site-packages scipy包位于:C:\\ Users \\ Breda \\ Anaconda3 \\ Lib \\ site-packages
sys.path variable contains: C:\\Users\\Breda\\Anaconda3\\Lib\\site-packages sys.path变量包含:C:\\ Users \\ Breda \\ Anaconda3 \\ Lib \\ site-packages
I tried to import other site-packages in test.py and run via GUI button command , all face the same problem. 我试图在test.py中导入其他站点程序包,并通过GUI按钮命令运行,所有这些都面临相同的问题。 Any ideas? 有任何想法吗?
The reason why the scipy
module could not be found is because the python executable run by os.system
was not part of the python installation which had scipy
installed. 无法找到scipy
模块的原因是因为os.system
运行的python可执行文件不是已安装scipy
的python安装的一部分。 (There is more than one python distribution installed on this machine.) (此计算机上安装了多个python发行版。)
The issue was diagnosed by putting 该问题是通过放置
import sys
print(sys.executable)
at the top of the test.py
file (before import scipy
). 在test.py
文件的顶部(在import scipy
之前)。 This prints the path to the python executable. 这将打印python可执行文件的路径。 Then test.py
was run twice -- once from the command-line, and once through GUI.py
. 然后test.py
运行了两次-一次从命令行运行,一次通过GUI.py
。 The two test runs printed different paths. 两次测试运行打印了不同的路径。
The problem was fixed by setting Pycharm's default python executable to the Anaconda Python3 installation which has the scipy
module installed. 通过将Pycharm的默认python可执行文件设置为安装了scipy
模块的Anaconda Python3安装,可以解决此scipy
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.