0

I got 2 major issues, it was all fine when I had all scripts in one directory TwitterAnalyzer, but when I started cleaning up. Some problems occured.

I must import TwitterApi into Analyzer module. I have found solution, but I looks silly to me (if __name__ == '__main__'). Otherwise import will not work in parent module _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. I don't fully understand this topic, which may have answers: 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. Analyzer is downloading tweets in background.

# 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

Grzegorz Krug
  • 186
  • 2
  • 11

1 Answers1

2

Add __init__.py to TwitterAnalyzer directory

Ishinomori
  • 229
  • 1
  • 6