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/
Related
I'm starting on a project using Clarifai. However, when I define the app, I'm getting a key error:
from clarifai.rest import ClarifaiApp
from clarifai.rest import Image as ClImage
import os
from glob import glob
api_key = 'my api key'
app = ClarifaiApp(api_key=api_key) # Error occurs here
model_id = 'model id'
concepts = ['concept1', 'concept2', 'concept3']
Traceback (most recent call last):
File "C:\Users\crayo\uShoe\main.py", line 6, in <module>
app = ClarifaiApp(api_key=api_key)
File "C:\Users\user\project\venv\lib\site-packages\clarifai\rest\client.py", line 124, in __init__
self.models = Models(self.api, self.solutions) # type: Models
File "C:\Users\user\project\venv\lib\site-packages\clarifai\rest\client.py", line 1068, in __init__
self.model_id_cache = self.init_model_cache()
File "C:\Users\user\project\venv\lib\site-packages\clarifai\rest\client.py", line 1088, in init_model_cache
model_type = m.output_info['type']
KeyError: 'type'
I'm not sure what's causing this error, so if someone could provide input I'd appreciate it! Thanks!
You are using the deprecated Python REST package: https://github.com/Clarifai/clarifai-python. Please replace your code with the new & updated Python gRPC client: https://github.com/Clarifai/clarifai-python-grpc
Make sure to uninstall the REST package to avoid conflicts.
You can find our API docs & code snippets here: https://docs.clarifai.com
When I try to save the file lines in a list I with the following code I get the below error:
Traceback (most recent call last):
File "C:\Users\emiel\AppData\Roaming\Sublime Text 3\Packages\User\Making_a_list_from_file.py", line 7, in <module>
with filename as file_object:
AttributeError: __enter__
This is the code:
with filename as file_object:
lines=file_object.readlines()
for line in lines:
print(line.strip())
The error is telling you that the object you're using in the with statement is not of the right type. __enter__ is one of the methods called as part of the context manager protocol, and the type you're using doesn't have that method.
Based on your variable name, it looks like you may be using a file name where you want to be using a file object. The error message will then make sense, as strings are not context managers the way file objects are. Try changing your with statement to:
with open(filename) as file_object:
Goal:
- Use django templateing language.
- Render the template in memory (no disk writes).
- Push rendered content to StringIO instance.
- Use instance in python-pdfkit.
Issue:
I keep getting TypeError: coercing to Unicode: need string or buffer, instance found when trying to pass more than one file in the list.
The below code works without the [] and just one StringIO instance.
from django.template import loader, Context
from django import template
import StringIO
STATIC_URL = "https://d1i1yohwujljp9.cloudfront.net/static/"
t = loader.get_template('pdf_coverpage.html')
c = template.Context( {'STATIC_URL': STATIC_URL })
output = StringIO.StringIO()
output.write(t.render(c))
output1 = StringIO.StringIO()
output1.write(t.render(c))
pdfkit.from_file([ output, output1 ] , 'out.pdf' )
Traceback.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\pdfkit\api.py", line 44, in from_file
configuration=configuration)
File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 37, in __init__
self.source = Source(url_or_file, type_)
File "C:\Python27\lib\site-packages\pdfkit\source.py", line 12, in __init__
self.checkFiles()
File "C:\Python27\lib\site-packages\pdfkit\source.py", line 28, in checkFiles
if not os.path.exists(path):
File "C:\Python27\lib\genericpath.py", line 18, in exists
os.stat(path)
TypeError: coercing to Unicode: need string or buffer, instance found
It's not your fault. This happens because pdf kit assumes each element in the list as a file path instead of file descriptor.
here is the relevant code.
I had a similar situation of HTML spread across multiple templates. I put them all in one string and pass the StringIO to pdfkit. I used CSS to manage page breaks and other wkhtmltopdf formatting options.
Hope that helps.
Using StringIO doesn't appear to be a recommended approach in the documentation.
I just tried this and it worked fine. Is there a reason you don't want to do it this way?
pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf')
https://pypi.python.org/pypi/pdfkit
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...
I am trying to use PyQt's Resource System but it appears I have no clue what I am doing! I already have to application created, along with its GUI I am just trying to import some images to use with the program.
I used the QtDesigner to create the resource file and I compiled it using pyrcc4.exe. But when I attempt to import the resource file I get this error:
Traceback (most recent call last):
File "C:\Projects\main.py", line 14, in <module>
import main_rc
File "C:\Projects\main_rc.py", line 482, in <module>
qInitResources()
File "C:\Projects\main_rc.py", line 477, in qInitResources
QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data)
TypeError: argument 2 of qRegisterResourceData() has an invalid type
What am I doing wrong?
pyrcc generates Python 2.x code by default.
Try regenerating your resource files using pyrcc with flag '-py3'