cherrypy jsondecoder not found - python

I tried this example:
import cherrypy
class Root(object):
#cherrypy.expose
#cherrypy.tools.json_out()
def getrange(self, limit=4):
return list(range(int(limit)))
cherrypy.quickstart(Root())
when I run this, I get:
Traceback (most recent call last):
File "D:/Dev/BaronPOS/baronpos/Server/tester.py", line 1, in <module>
import cherrypy
File "C:\Python27\lib\site-packages\cherrypy\__init__.py", line 62, in <module>
from cherrypy._cpcompat import urljoin as _urljoin, urlencode as _urlencode
File "C:\Python27\lib\site-packages\cherrypy\_cpcompat.py", line 244, in <module>
json_decode = json.JSONDecoder().decode
AttributeError: 'module' object has no attribute 'JSONDecoder'
and I have absolutely no idea why. I have checked if the module is present and the class and both are. Please help!

OK, I figured it out.
I was using SimpleJSONRPCServer previously (http://www.freenet.org.nz/dojo/pyjson/) but decided to move to a more flexible REST compatible architecture.
Apparantly I left an artifact called json.py, which was causing this...

Related

Cant import a PushKit framework into pyobjus

I am trying to import UserNotifications from the PushKit framework but i don't know how to call it because the only Include function i know of in pyobjus is AppKit. I have not seen of any way to call frameworks from the PushKit when i try to call it from the AppKit. it returns an error. Below is my code and the error. I would appreciate any help.
from pyobjus import autoclass
from pyobjus.dylib_manager import load_framework, INCLUDE
load_framework(INCLUDE.AppKit)
UserNotifications = autoclass('UserNotifications')
Traceback
Traceback (most recent call last):
File "/Users/temitayoadefemi/PycharmProjects/test7/mainfile.py", line 5, in <module>
UserNotifications = autoclass('UserNotifications')
File "pyobjus/pyobjus.pyx", line 726, in pyobjus.autoclass
File "pyobjus/pyobjus.pyx", line 85, in pyobjus.MetaObjcClass.__new__
File "pyobjus/pyobjus.pyx", line 116, in pyobjus.MetaObjcClass.resolve_class
pyobjus.ObjcException: Unable to find class b'UserNotifications'
AppKit doesn't have a class called UserNotifications. You could use UNNotificationSettings for normal Push Notification or PKPushRegistry for VoIP Push Notification.
References:
https://developer.apple.com/documentation/pushkit?language=objc
https://developer.apple.com/documentation/usernotifications?language=objc

Problem with urllib source code in Eclipse?

I am having a bizarre problem in which Eclipse is raising an error for the source code of a python Lib package. I am using Python 3.7.1.
The relevant line is:
import urllib.request.urlopen as uReq
I don't believe the python interpreter I'm using is relevant to this particular problem. Nevertheless, here is the code for it. (I know, it's probably horrendous. If you know a better way to do this, please kindly point me in the right direction; this is my first time using jython and I'm sure it shows.)
interpreter = new PythonInterpreter();
interpreter.exec("import sys");
interpreter.exec("import os");
interpreter.exec("sys.path.insert(0, os.getcwd() + '\\src\\python')");
interpreter.exec("sys.path.insert(0, os.getcwd() + '\\Lib')");
interpreter.exec("sys.path.insert(0, os.getcwd() + '\\Lib\\site-packages')");
interpreter.exec("print(sys.path[0])");
interpreter.exec("from myscraper import validateUrl");
The error:
Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\...\ThreadTracker\src\python\myscraper.py", line 6, in <module>
import urllib.request.urlopen as uReq
SyntaxError: ("mismatched input ',' expecting NAME",
('C:\\Users\\...\\ThreadTracker\\Lib\\urllib\\request.py', 140, 13, '
*, cafile=None, capath=None, cadefault=False, context=None):\n'))
Which seems to be having a problem with this line of code from the source file:
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
*, cafile=None, capath=None, cadefault=False, context=None):
I saw on another post that urllib3 could be used as well, so I tried:
from urllib3 import request as uReq
But this came up with its own error:
Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Christina\ThreadTracker\src\python\myscraper.py", line 6, in <module>
from urllib3 import request as uReq
File "C:\Users\Christina\ThreadTracker\Lib\site-packages\urllib3\__init__.py", line 8, in <module>
from .connectionpool import (
File "C:\Users\Christina\ThreadTracker\Lib\site-packages\urllib3\connectionpool.py", line 3, in <module>
import logging
SyntaxError: ("no viable alternative at input '='", ('C:\\Users\\Christina\\ThreadTracker\\Lib\\logging\\__init__.py', 261, 55, ' method_name, "method:", err, file=sys.stderr)\n'))
I'm extremely confused as to why I'm getting errors from the source code... And I've been having a heck of a time just trying to get the java-python integration to work.

python3 ImportError: cannot import name 'rand_str'

Im getting this error when starting my software i dont know how to repair this
Traceback (most recent call last):
File "/root/findex/lib/python3.5/site-packages/findex_gui/main.py", line 208, in web
run_async()
File "/root/findex/lib/python3.5/site-packages/findex_gui/main.py", line 201, in run_async
from findex_gui.web import app
File "/root/findex/lib/python3.5/site-packages/findex_gui/web.py", line 41, in <module>
from findex_gui.orm.connect import Database
File "/root/findex/lib/python3.5/site-packages/findex_gui/orm/connect.py", line 12, in <module>
from findex_gui.orm.models import BASE
File "/root/findex/lib/python3.5/site-packages/findex_gui/orm/models.py", line 22, in <module>
from findex_common.utils import rand_str
ImportError: cannot import name 'rand_str'
I'm not familiar with the libraries in question, but it seems the maintainer renamed this function in 0.15.14 of findex-common here: development
This has been adjusted for in findex-gui on the master branch though it seems it has not seen a release. Here's the commit which fixes the import: Forgot what I was doing - Ill just push it all ¯_(ツ)_/¯
One short term fix would be to downgrade to findex-common==0.15.13 and perhaps follow up by raising an issue on either project since the change seems non-ideal.

Django Issues with Uploading a Python File

My code so far:
from django.core.files import File
file = open('ChicagoTimesScores.py')
djangofile = File(file)
myfile = File(file)
myfile.save('new', djangofile)
file.close()
I'm aware of repeating myfile, but I was a bit lost and tried what I could to see if it work.
My error code:
Traceback (most recent call last):
File "C:/Python33/TestRun.py", line 6, in <module>
myfile.save('new', djangofile)
AttributeError: 'File' object has no attribute 'save'
I'm trying to save this python file with django, but it appears that python doesn't recognize these attributes.
Oh, and yeah, I installed django correctly. No issues.
the Django File class doesn't have a method "save" Here are the Django Docs for the File class:
https://docs.djangoproject.com/en/dev/ref/files/file/

python - AttributeError: 'module' object has no attribute

I'm trying this simple code:
import requests
print requests.__file__
r = requests.get('https://github.com/timeline.json')
It works flawlessly on the command line when I type the lines one by one, but not whenen when I execute it as a script or in Sublime Text 2. Here's the stack trace:
C:\Python27\lib\site-packages\requests\__init__.pyc
Traceback (most recent call last):
File "C:\Users\Bruce\Desktop\http.py", line 1, in <module>
import requests
File "C:\Python27\lib\site-packages\requests\__init__.py", line 53, in <module>
from requests.packages.urllib3.contrib import pyopenssl
File "C:\Python27\lib\site-packages\requests\packages\__init__.py", line 3, in <module>
from . import urllib3
File "C:\Python27\lib\site-packages\requests\packages\urllib3\__init__.py", line 16, in <module>
from .connectionpool import (
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 15, in <module>
from http.client import HTTPConnection, HTTPException
File "C:\Users\Bruce\Desktop\http.py", line 3, in <module>
r = requests.get('https://github.com/timeline.json')
AttributeError: 'module' object has no attribute 'get'
[Finished in 0.2s with exit code 1]
Answers on 'Module object has no attribute 'get' Python error Requests? didn't help much.
Could this be some error in my ST2 Python build system? I tried removing all requests modules in case there were multiples of them by using pip and reinstalled them.
Edit After reading the stacktrace again, you can see that urllib3 tries to import something from the http module. Your file is called http.py and is thus imported instead of the expected one.
The actual error happens because of the circular nature of the import. Since requests hasn't finished importing completely yet. The get function in requests isn't defined yet when the http import reaches import requests again.
Note: You will also want to always guard your entry point with the if __name__ == '__main__' construct. This will often avoid nasty errors for unsuspecting future developers (including yourself).

Categories

Resources