caronc / nzb-notify

Push Notifications to a large number of supported services for NZBGet and SABnzbd (based on Apprise)
GNU General Public License v3.0
129 stars 15 forks source link

Import error in Python 2.7 and 3.6 #29

Closed jwallden closed 6 years ago

jwallden commented 6 years ago

Hello!

When trying to run python sabnzbd-notify.py complete 'Hello' 'World' tgram://$TOKEN/$CHAT_ID i get:

Traceback (most recent call last): File "/home/sabnzbd/nzb-notify/Notify.py", line 305, in from urllib import unquote ImportError: cannot import name 'unquote'

Upon fixing the import error by changing it to urllib.parse iget:

Traceback (most recent call last): File "/home/sabnzbd/nzb-notify/Notify.py", line 309, in from nzbget import SCRIPT_MODE File "/home/sabnzbd/nzb-notify/Notify/nzbget/init.py", line 23, in from ScriptBase import * ModuleNotFoundError: No module named 'ScriptBase'

I find this really strange since ScriptBase are clearely available. I have tried running it with both Python 2.7 and 3.6.

caronc commented 6 years ago

That is strange, I'm not able o reproduce your issue at all. The scripts definitely won't work with Python 3.6. So for now you'll want to stick with Python 2.7.x. What happens if you don't use the sabnzbd-notify.py all together (just as a test). Try running:

# -D  : Debug mode
# -t   : title
# -b  : body
# -s  : Server URL
python Notify.py -D -t 'Hello' -b 'World' -s tgram://$TOKEN/$CHAT_ID

What OS are you using (if you don't mind me asking?).

jwallden commented 6 years ago

Strange indeed. Here's the output from the command above executed with python2.7: (obviously stripped of auth info)

[INFO] [DEBUG] Script mode forced off. 2017-09-14 20:01:22,133 - 19252 - DEBUG - get(default) SERVERS=None 2017-09-14 20:01:22,133 - 19252 - DEBUG - set(config) SERVERS="tgram://$TOKEN/$CHAT_ID" 2017-09-14 20:01:22,133 - 19252 - DEBUG - set(environment) SERVERS="tgram://$TOKEN/$CHAT_ID" 2017-09-14 20:01:22,133 - 19252 - DEBUG - get(default) TITLE=None 2017-09-14 20:01:22,134 - 19252 - DEBUG - set(config) TITLE="Hello" 2017-09-14 20:01:22,134 - 19252 - DEBUG - set(environment) TITLE="Hello" 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(default) BODY=None 2017-09-14 20:01:22,134 - 19252 - DEBUG - set(config) BODY="World" 2017-09-14 20:01:22,134 - 19252 - DEBUG - set(environment) BODY="World" 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(default) INCLUDEIMAGE=None 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(config) SERVERS="tgram://$TOKEN/$CHAT_ID" 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(config) SERVERS="tgram://$TOKEN/$CHAT_ID" 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(config) TITLE="Hello" 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(config) BODY="World" 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(default) INCLUDEIMAGE=None 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(default) INCLUDEIMAGE=None 2017-09-14 20:01:22,134 - 19252 - DEBUG - get(config) SERVERS="tgram://$TOKEN/$CHAT_ID" 2017-09-14 20:01:22,134 - 19252 - DEBUG - Server parsed to: {'host': 'XXXX', 'user': None, 'query': '$CHAT_ID', 'url': 'tgram://$TOKEN/$CHAT_ID', 'path': '/$TOKEN/', 'fullpath': '/$TOKEN/$CHAT_ID', 'password': None, 'port': None, 'qsd': {}, 'schema': 'tgram'} 2017-09-14 20:01:22,206 - 19252 - DEBUG - Telegram POST URL: https://api.telegram.org/botXXXX:$TOKEN/sendMessage 2017-09-14 20:01:22,206 - 19252 - DEBUG - Telegram POST URL: https://api.telegram.org/botXXXX:$TOKEN/sendMessage (cert_verify=True) 2017-09-14 20:01:22,206 - 19252 - DEBUG - Telegram Payload: {'text': u'Hello\r\nWorld', 'parse_mode': 'HTML', 'chat_id': '-$CHAT_ID'} 2017-09-14 20:01:22,380 - 19252 - DEBUG - Exiting with return code: 93

Of course I dont mind you asking about the OS! I'm running Arch with 4.12.10-1 kernel.

caronc commented 6 years ago

Interesting... so it would appear that it's working from the command line using the Notify.py. 93 is NZBGet's success response. I assume the message came through okay on your side (in Telegram)?

I've been out of town this week (thus slow responding) but will be traveling home tomorrow. Hopefully I can have a closer at this sometime before this weekend is up.

caronc commented 6 years ago

While Googling around, I saw this post which might (or might not) be applicable. I wonder if you're missing some __init__.py files? Make sure sabnzb-notify.py is in the same directory as the Notify.py script and Notify directory. It will definitely break if it's placed elsewhere. Make sure your permissions aren't too restrictive either (I'm not sure what user you're using to run SABnzbd as).

# you could try
pushd /path/to/sabnzbd/scripts
# don't worry, this command only removes .pyo and .pyc files
# I assume pushd worked here and you're inside
# of the scripts directory here
find .  -type f -regex '.*/.*\.py[oc]' -delete
# permissions
find .  -type f -exec chmod 644 {} \;
find .  -type d -exec chmod 755 {} \;

#edit: forgot this
chmod 755 sabnzbd-notify.py Notify.py

#optional, but safe and ideal to do
chown root.root *
popd

Then try again and keep me posted.

jwallden commented 6 years ago

Hello! Thank you for taking the time trying to solve this! Much appreciated.

I have tried all of the above and executed both from the commandline and from insde SabNzb. However I still get the

Traceback (most recent call last): File "/home/sabnzbd/nzb-notify/Notify.py", line 309, in from nzbget import SCRIPT_MODE File "/home/sabnzbd/nzb-notify/Notify/nzbget/init.py", line 23, in from ScriptBase import * ModuleNotFoundError: No module named 'ScriptBase'

Notify.py and sabnzb-notify.py is in the same directory, as is Notify

caronc commented 6 years ago

Have a go at the master branch and let me know your results!

jwallden commented 6 years ago

Hmm. Tried it on a Elementary OS system (Ubuntu based) with Python 2.7.12. Works flawlessly! Back to my Arch system and running the script with python2.7 (which is 2.7.13) i then it doesn't work. I get

File "/home/sabnzbd/nzb-notify/Notify.py", line 964 except TypeError, e: ^ SyntaxError: invalid syntax

I actually got that before also but fixed it (with except TypeError as e:). Not working on Arch though...

caronc commented 6 years ago

Looks like this is my issue; found this stack overflow reference which points out that using a comma (,) as i'm doing is for Python v2.5 support which you and I definitely don't care about. I should have been using the as keyword everywhere all along. I'll do a mass search and replace throughout the code push the changes this evening (have to just step out for a bit). But if you've already done your own custom fix; you're work-around is great for now! At this point you're ahead of me now in coding styles :blush:.

jwallden commented 6 years ago

Haha! Yeah! Bleeding edge. As with Arch :)

However, I still get the import error on Arch... I got a bit ahead of myself and first posted a version of my previous comment that said it worked. Turned out I had exited out of the Arch box and was back on the Ubuntu machine :(

Isn't this mighty strange though? Feels like it's me that's doing something weird. Can it have anything to do with some environment variable or something like that? It's totally unreasonable that it works in 2.7.12 but not in 2.7.13!

caronc commented 6 years ago

It should definitely work with both versions (.12 and .13), I agree! Hard to say if you're doing anything wrong. Arch is bleeding we edge as you say. Maybe you could try installing 2.7.12 on it? Are you building Python yourself, or using package management?

caronc commented 6 years ago

Give it another shot (via the master branch) if you don't mind. Post the error(s) you're getting again too because this time around the imports were updated. If you are getting any import errors, I have to presume they're different; or at least from a new line (and/or related to something else), no? Keep me posted either way; hopefully we'll figure this out!

jwallden commented 6 years ago

Hello! Sorry for the delay. I finally got around to some more testing today and this is what i found.

I did a git pull to get up to date with the latest changes. Also tried pyton 2.7.12 but with no luck. Get the exact same errors as on 2.7.13. When i try to run the script as per above I now get:

Traceback (most recent call last): File "/home/sabnzbd/nzb-notify/Notify.py", line 309, in from nzbget import SCRIPT_MODE File "/home/sabnzbd/nzb-notify/Notify/nzbget/init.py", line 23, in from .ScriptBase import * File "/home/sabnzbd/nzb-notify/Notify/nzbget/ScriptBase.py", line 889 makedirs(self.tempdir, 0700) ^ SyntaxError: invalid token

caronc commented 6 years ago

Although you may have installed Python 2.7.x that error is 100% related to Python v3.x.

Python v2 accepts octals with having a zero as the first entry (in this case, you're getting the error on 0777). In Python v3 octals have to be represented as 0o777 (with respect to this example) otherwise you get the SyntaxError you just described. Python v2.7 supports this new format as well, but adding Python v3 support will be us going back and forth for a while; someday i'll have to add support for it though; it's getting pretty mature.

Anyway... back to the original subject: One thing you could try to do is open up /home/sabnzbd/nzb-notify/Notify.py and change the very top line from: #!/usr/bin/env python to #!/usr/bin/env python2.

Keep me posted!

caronc commented 6 years ago

Hi,

I'm going to close this ticket as this ticket as it's been 2 weeks now. Feel free to re-open it if you're still having problems; I can do my best to help troubleshoot. At this time your problem is due to Python 3 which at this time is unsupported.