简体   繁体   English

从脚本(a.py)导入python模块/站点包(例如scipy),但不能从tkinter button命令导入

[英]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.

相关问题 Python 站点包从脚本根目录导入子模块 - Python site-package imports submodule from script root directory 无法从站点包导入模块 - unable to import module from site-package 未找到 Python 站点包模块 - Python site-package module not found 如何从rJython导入站点包? - How to import a site-package from rJython? 来自站点包的模板中的 URL - Url in template from site-package 如何从conda的site-package目录中添加一个Python模块进行Spark-Submit? - How do I add a Python module from inside conda's site-package directory to spark-submit? Linux程序(例如bash或python脚本)如何知道它是如何启动的:从命令行还是交互式GUI? - How can Linux program, e.g. bash or python script, know how it was started: from command line or interactive GUI? 如何将站点包从文件夹 Python37 移动到文件夹 Python310? - How to move site-package from folder Python37 to folder Python310? 从命令行导入 Python 模块有效,但不能从 PyCharm - Importing a Python module works from command line, but not from PyCharm 从python更新命令到mongodb数据库返回查询结果(例如布尔值或整数) - does update command to mongodb database from python returns result of query(e.g. boolean or integer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM