[英]Imported module does not import
I got 2 major issues, it was all fine when I had all scripts in one directory TwitterAnalyzer
, but when I started cleaning up.我遇到了两个主要问题,当我在一个目录
TwitterAnalyzer
中拥有所有脚本时一切都很好,但是当我开始清理时。 Some problems occured.出现了一些问题。
I must import TwitterApi into Analyzer
module.我必须将TwitterApi导入
Analyzer
模块。 I have found solution, but I looks silly to me ( if __name__ == '__main__'
).我找到了解决方案,但我看起来很傻(
if __name__ == '__main__'
)。 Otherwise import will not work in parent module _run_GUI_mode.py
否则导入在父模块
_run_GUI_mode.py
中不起作用
I also tried to make some __init__.py
file, that imports both things, but it is not working NameError: name 'TwitterApi' is not defined
How to import correctly, that will allow me to run Analyzer
independently.我还尝试制作一些
__init__.py
文件,它可以导入这两个东西,但它不起作用NameError: name 'TwitterApi' is not defined
如何正确导入,这将允许我独立运行Analyzer
。 I don't fully understand this topic, which may have answers: Why does importing a python module not import nested modules?这个话题我不是很懂,可能有答案: Why does importing a python module not import nested modules?
Should I move all scripts back to same directory?我应该将所有脚本移回同一目录吗? I will avoid additional work with path tracking to
tweets
directory.我将避免对
tweets
目录进行路径跟踪的额外工作。 Analyzer is downloading tweets in background. Analyzer 正在后台下载推文。
# My Project Tree
.TwitterAnalyzer
|---LICENSE
|---README.md
|---TwitterAnalyzer
|---Analyzer
|---Analyzer.py
|---Readme.md
|---secret_token.txt
|---TwitterApi.py
|---__init__.py
|---GUI
|---create_gui_py.py
|---GUI.py
|---GUI_QT.ui
|---tweets
|---_run_GUI_mode.py
# Analyzer\TwitterAnalyzer.py
# Grzegorz Krug
if __name__ == '__main__': # this is my solution, but I do not like it
from TwitterApi import TwitterApi, TwitterLoginFailed
else:
from Analyzer.TwitterApi import TwitterApi, TwitterLoginFailed
class TwitterAnalyzer(TwitterApi):
def __init__(self, autologin=True, log_ui=None):
...
# _run_GUI_mode.py
# Grzegorz Krug
from Analyzer.TwitterApi import TwitterApi, TwitterLoginFailed # <-- This does not work anyway
from Analyzer.Analyzer import TwitterAnalyzer
from GUI.GUI import Ui_MainWindow
class TwitterAnalyzerGUI(TwitterAnalyzer, Ui_MainWindow):
def __init__(self, mainWindow):
...
# Analyzer/__init__.py
from TwitterApi import TwitterApi
from Analyzer import TwitterAnalyzer
Add __init__.py
to TwitterAnalyzer directory将
__init__.py
添加到 TwitterAnalyzer 目录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.