We have images in our OpenStack named <OS> <version>:<build no> (e.g., CentOS 7.2.0:160708.0). With the Python novaclient, I can use client.glance.find_image with releases before Mitaka.
$ cat test.py
#! /usr/bin/env python3
import os
import sys
from novaclient import client
nova = client.Client("2",
os.environ["OS_USERNAME"],
os.environ["OS_PASSWORD"],
os.environ["OS_TENANT_ID"],
os.environ["OS_AUTH_URL"],
cacert=os.environ["OS_CACERT"])
print(nova.glance.find_image(sys.argv[1]))
With Liberty:
$ python3 test.py "CentOS 7.2.0:170210.0"
<Image: CentOS 7.2.0:170210.0>
With Mitaka:
$ python3 test.py "CentOS 7.2.0:170210.0"
Traceback (most recent call last):
File "test.py", line 11, in <module>
print(nova.glance.find_image(sys.argv[1]))
File "/usr/local/lib/python3.6/site-packages/novaclient/v2/images.py", line 53, in find_image
"images")
File "/usr/local/lib/python3.6/site-packages/novaclient/base.py", line 254, in _list
resp, body = self.api.client.get(url)
File "/usr/local/lib/python3.6/site-packages/keystoneauth1/adapter.py", line 223, in get
return self.request(url, 'GET', **kwargs)
File "/usr/local/lib/python3.6/site-packages/novaclient/client.py", line 80, in request
raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.BadRequest: Unable to filter by unknown operator 'CentOS 7.2.0'.<br /><br />
(HTTP 400)
Note that the error when an image of that name does not exist is different:
$ python3 test.py "CentOS 7.2.0"
Traceback (most recent call last):
File "test.py", line 11, in <module>
print(nova.glance.find_image(sys.argv[1]))
File "/usr/local/lib/python3.6/site-packages/novaclient/v2/images.py", line 58, in find_image
raise exceptions.NotFound(404, msg)
novaclient.exceptions.NotFound: No Image matching CentOS 7.2.0. (HTTP 404)
It's as if find_image is expecting a string of the form operator: value, but the documentation has only this to say about find_image:
find_image(name_or_id)
Find an image by name or id (user provided input).
How do I find an image whose name contains a colon when using Mitaka?
$ nova --version
8.0.0
The error is coming from the image service (Glance). In the newer versions of Glance, there is a change in the GET API syntax, where someone can specify an "in:" operator for filtering. You can read more about this at
https://developer.openstack.org/api-ref/image/v2/index.html?expanded=show-images-detail#show-images
For your code to work, you can enclose the image name with quotes and prefix it with "in:" string:
print(nova.glance.find_image('in:"' + sys.argv[1] + '"'))
Note that Glance is pretty strict about quotes; your image name has to be wrapped with double-quotes only -- single quotes won't work. Hence, I used the single quote for the string in the above command.
Another pretty inefficient but functional option is to use list() function in nova.images and then explicitly look for the image with name sys.argv[1]:
ilist = nova.images.list()
for image in ilist:
if image.name == sys.argv[1]:
print image
break
Related
I am trying to convert a pdf file to image file for this in my ubuntu server i have installed:
python2.7
poppler-utils
pdf2image==1.12.1
My code:
from pdf2image import convert_from_path, convert_from_bytes
images = convert_from_path("/home/user/pdf_file.pdf")
# OR
with open("/home/user/pdf_file.pdf") as pdf:
images = convert_from_bytes(pdf.read())
OUTPUT
When I am using the function "convert_from_path"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pdf2image/pdf2image.py", line 143, in convert_from_path
thread_output_file = next(output_file)
TypeError: ThreadSafeGenerator object is not an iterator
When I am using the function "convert_from_bytes"
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/local/lib/python2.7/dist-packages/pdf2image/pdf2image.py", line 268, in convert_from_bytes
paths_only=paths_only,
File "/usr/local/lib/python2.7/dist-packages/pdf2image/pdf2image.py", line 143, in convert_from_path
thread_output_file = next(output_file)
TypeError: ThreadSafeGenerator object is not an iterator
I have reinstalled all my utilities then i am facing these problems.
If you want to convert PDF to image you can try Python Ghostscript package:
pip install ghostscript
import ghostscript
import locale
def pdf2jpeg(pdf_input_path, jpeg_output_path):
args = ["pef2jpeg", # actual value doesn't matter
"-dNOPAUSE",
"-sDEVICE=jpeg",
"-r144",
"-sOutputFile=" + jpeg_output_path,
pdf_input_path]
encoding = locale.getpreferredencoding()
args = [a.encode(encoding) for a in args]
ghostscript.Ghostscript(*args)
pdf2jpeg(
"...Fixate/ActiveState/pdf/a.pdf",
"...Fixate/ActiveState/pdf/a.jpeg",
)
I failed in python2 too, but succeeded in python3.
There's a same issue happened on an other library:
TypeError: 'threadsafe_iter' object is not an iterator
As they said, it's a python 2 vs 3 issue, caused by next() function.
If modify __next__() -> next() in file/home/***/.local/lib/python2.7/site-packages/pdf2image/generators.py , it will run successful in py2.
BTW, i have create a new issue to pdf2image team.
TypeError: ThreadSafeGenerator object is not an iterator #133
Additional
pdf2image readme said it's a python (3.5+) module.
pdf2image v1.7.1 work on py27. try it by pip install pdf2image==1.7.1
Python code breaks in python3.
Vectorize.py contains the following line
path = os.path.join('..', path[:-1])
Error output:
$ python3 vectorize_text.py
Traceback (most recent call last):
File "vectorize_text.py", line 46, in <module>
path = os.path.join('..', path[:-1])
File "/usr/lib/python3.4/posixpath.py", line 89, in join
"components") from None
TypeError: Can't mix strings and bytes in path component
On the other hand, running in python2.7 it works fine. What am I missing here ? Is the command different ? I couldn't find anything.
Your path is a bytes object, not a str string. You can then only use more bytes strings to make a different path. Use a b'..' bytes literal:
path = os.path.join(b'..', path[:-1])
I am starting with Microsoft Azure SDK for Python (https://github.com/Azure/azure-sdk-for-python), but I have problems.
I am using Scientific Linux and I have installed the SDK for Python 3.4 following the next steps:
(instead of the SDK directory)
python setup.py install
after that I created a simple script just to test the connection:
from azure.storage import BlobService
blob_service = BlobService(account_name='thename', account_key='Mxxxxxxx3w==' )
blob_service.create_container('testcontainer')
for i in blob_service.list_containers():
print(i.name)
following this documentation:
http://blogs.msdn.com/b/tconte/archive/2013/04/17/how-to-interact-with-windows-azure-blob-storage-from-linux-using-python.aspx
http://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/#large-blobs
but is not working, I always receive the same error:
python3 test.py
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/storage/storageclient.py", line 143, in _perform_request
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/storage/storageclient.py", line 132, in _perform_request_worker
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/http/httpclient.py", line 247, in perform_request
azure.http.HTTPError: The value for one of the HTTP headers is not in the correct format.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 21, in <module>
blob_service.create_container('testcontainer')
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/storage/blobservice.py", line 192, in create_container
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/__init__.py", line 905, in _dont_fail_on_exist
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/storage/blobservice.py", line 189, in create_container
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/storage/storageclient.py", line 150, in _perform_request
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/storage/__init__.py", line 889, in _storage_error_handler
File "/usr/local/lib/python3.4/site-packages/azure-0.9.0-py3.4.egg/azure/__init__.py", line 929, in _general_error_handler
azure.WindowsAzureError: Unknown error (The value for one of the HTTP headers is not in the correct format.)
<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidHeaderValue</Code><Message>The value for one of the HTTP headers is not in the correct format.
RequestId:b37c5584-0001-002b-24b8-c2c245000000
Time:2014-11-19T14:54:38.9378626Z</Message><HeaderName>x-ms-version</HeaderName><HeaderValue>2012-02-12</HeaderValue></Error>
Thanks in advance and best regards.
I have this exact same issue. I believe it's a library bug, but the author/s haven't had their say yet.
It looks like the response states the version, but it's actually giving you the header that's wrong. Its value should be "2014-02-14", you can do the fix shown in https://github.com/Azure/azure-sdk-for-python/pull/289 .
Hopefully this will be fixed and nobody will ever read this answer. Cheers!
I've got PyPDF2 lib from here:
https://github.com/mstamy2/PyPDF2/tree/Python3-3
When trying to run script "Example 1:" from from there see it:
PyPDF2 python versions (2.5 - 3.3) compatibility branch
Traceback (most recent call last):
File "1.py", line 6, in <module>
input1 = PdfFileReader(open("document1.pdf", "rb"))
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 595, in __init__
self.read(stream)
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 1097, in read
streamData = StringIO(xrefstream.getData())
TypeError: initial_value must be str or None, not bytes
What is wrong?
It was a problem related to the compatibility within PyPDF2 and Python 3.
In my case, I have solved it by replacing pdf.py and utils.py with the ones you will find here, where they basically control if you are running Python 3 and, in case you are, receive data as bytes instead of strings.
Using win32com.client, I'm attempting to create a simple shortcut in a folder. The shortcut however I would like to have arguments, except I keep getting the following error.
Traceback (most recent call last):
File "D:/Projects/Ms/ms.py", line 153, in <module>
scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b ' + str(loop7)
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.TargetPath' can not be set.
My code looks like this. I've tried multiple different variates but can't seem to get it right. What am I doing wrong?
ws = win32com.client.Dispatch("wscript.shell")
scut = ws.CreateShortcut("D:/Projects/Ms/TestDir/testlink.lnk")
scut.TargetPath = '"C:/python27/python.exe" "D:/Projects/Ms/msd.py" -b 0'
scut.Save()
Your code works for me without error. (Windows XP 32bit, Python 2.7.5, pywin32-216).
(I slightly modified your code because TargetPath should contain only executable path.)
import win32com.client
ws = win32com.client.Dispatch("wscript.shell")
scut = ws.CreateShortcut('run_idle.lnk')
scut.TargetPath = '"c:/python27/python.exe"'
scut.Arguments = '-m idlelib.idle'
scut.Save()
I got AttributeError similar to yours when I tried following (assign list to Arguments property.)
>>> scut.Arguments = []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python27\lib\site-packages\win32com\client\dynamic.py", line 570, in __setattr__
raise AttributeError("Property '%s.%s' can not be set." % (self._username_, attr))
AttributeError: Property '<unknown>.Arguments' can not be set.
"..TargetPath should contain only [an] executable path." is incorrect in two ways :
The target may also contain the executable's arguments.
For instance, I have a file [ D:\DATA\CCMD\Expl.CMD ] whose essential line of code is
START Explorer.exe "%Target%"
An example of its use is
D:\DATA\CCMD\Expl.CMD "D:\DATA\SYSTEM - NEW INSTALL PROGS"
This entire line is the "executable" you are referring to.
The target doesn't have to be an "executable" at all. It may be any file in which the OS can act upon, such as those file types whose default actions run executable with the files as its arguments, such as :
"My File.txt"
The "default action" on this file type is to open it with a text editor. The actual executable file run isn't explicit.