Accocrding this manual I shoud use thise pice of code:
from azure.storage.blob import ContentSettings
block_blob_service.create_block_blob_from_path(
'mycontainer',
'myblockblob',
'sunset.png',
content_settings=ContentSettings(content_type='image/png')
)
But got this error:
AttributeError: 'BlockBlobService' object has no attribute 'create_block_blob_from_path'
Tried from git, as well as from pip
pip install azure-storage
I believe the tutorial is out of date, compared with the latest Python SDK. I don't think there's a create_block_blob_from_path anymore - I looked at the sdk code (here). There are separate imports for block blobs and page blobs, with the method being create_blob_from_path.
So with a simple correction:
from azure.storage.blob import BlockBlobService
from azure.storage.file import ContentSettings
blob_service = BlockBlobService(account_name="<storagename>",account_key="<storagekey>")
content_settings = ContentSettings(content_type = "image/png")
blob_service.create_blob_from_path("mycontainer","myblockblob","sunset.png",content_settings)
Related
Is the H2OModelSelectionEstimator deprecated? When I run the code
from h2o.estimators import H2OModelSelectionEstimator
I get the message: ImportError: cannot import name 'H2OModelSelectionEstimator' from 'h2o.estimators'
Try this instead:
from h2o.estimators.model_selection import H2OModelSelectionEstimator
If you can't import it, then you probably don't have the latest version of H2O, so you should download it. ModelSelection was just released in 3.36.0.1.
I came across this python class ResourcesMoveInfo for moving resources(Azure Images) from one subscription to another with Azure python SDK.
But it's failing when I use it like below:
Pattern 1
reference from https://buildmedia.readthedocs.org/media/pdf/azure-sdk-for-python/v1.0.3/azure-sdk-for-python.pdf
Usage:
metadata = azure.mgmt.resource.resourcemanagement.ResourcesMoveInfo(resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))
Error:
AttributeError: module 'azure.mgmt.resource' has no attribute 'resourcemanagement'
Pattern 2
reference from - https://learn.microsoft.com/en-us/python/api/azure-mgmt-resource/azure.mgmt.resource.resources.v2019_07_01.models.resourcesmoveinfo?view=azure-python
Usage:
metadata = azure.mgmt.resource.resources.v2020_06_01.models.ResourcesMoveInfo(resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))
Error:
AttributeError: module 'azure.mgmt.resource.resources' has no attribute 'v2020_06_01'
Any help on this requirement/issue would be helpful. Thanks!
Adding code snippet here:
import sys
import os
import time
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
import azure.mgmt.resource
#from azure.mgmt.resource.resources.v2020_06_01.models import ResourcesMoveInfo
from azure.identity import ClientSecretCredential
from cred_wrapper import CredentialWrapper
class Move():
def __init__(self):
self.nonprod_subscription_id = "abc"
self.prod_subscription_id = "def"
self.credential = ClientSecretCredential(
client_id= os.environ["ARM_CLIENT_ID"],
client_secret= os.environ["ARM_CLIENT_SECRET"],
tenant_id= os.environ["ARM_TENANT_ID"]
)
#resource client for nonprod
self.sp = CredentialWrapper(self.credential)
self.resource_client = ResourceManagementClient(self.sp,self.nonprod_subscription_id)
self.resource_group = "imgs-rg"
def getresourceids(self):
resource_ids = list(resource.id for resource in self.resource_client.resources.list_by_resource_group("{0}".format(self.resource_group)) if resource.id.find("latest")>=0)
return resource_ids
def getresourcenames(self):
resource_names = list(resource.name for resource in self.resource_client.resources.list_by_resource_group("{0}".format(self.resource_group)) if resource.id.find("latest")>=0)
return resource_names
def deleteoldimages(self,name):
#resource client id for prod
rc = ResourceManagementClient(self.sp,self.prod_subscription_id)
for resource in rc.resources.list_by_resource_group("{0}".format(self.resource_group)):
if resource.name == name:
#2019-12-01 is the latest api_version supported for deleting the resource type "image"
rc.resources.begin_delete_by_id(resource.id,"2020-06-01")
print("deleted {0}".format(resource.name))
def moveimages(self):
rnames = self.getresourcenames()
for rname in rnames:
print(rname)
#self.deleteoldimages(rname)
time.sleep(10)
rids = self.getresourceids()
rid = list(rids[0:])
print(rid)
metadata = azure.mgmt.resource.resources.v2020_06_01.models.ResourcesMoveInfo(resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))
#moving resources in the rid from nonprod subscription to prod subscription under the resource group avrc-imgs-rg
if rid != []:
print("moving {0}".format(rid))
print(self.resource_client.resources.move_resources(source_resource_group_name="{0}".format(self.resource_group),parameters=metadata))
#self.resource_client.resources.move_resources(source_resource_group_name="{0}".format(self.resource_group),resources=rid,target_resource_group='/subscriptions/{0}/resourceGroups/{1}'.format(self.prod_subscription_id,self.resource_group))
#self.resource_client.resources.begin_move_resources(source_resource_group_name="{0}".format(self.resource_group),parameters=metadata)
if __name__ == "__main__":
Move().moveimages()
From your inputs we can see that the code looks fine. From your error messages, the problem is with importing the modules.
Basically when we import a module few submodules will get installed along with and few will not. This will depend on the version of the package, to understand which modules are involved in a specific version we need to check for version-releases in official documentation.
In your case, looks like some resource modules were missing, if you could see the entire error-trace, there will be a path with sitepackages in our local. Try to find that package and its subfolder(Modules) and compare them with Azure SDK for Python under Resource module, you can find this here.
In such situation we need to explicitly add those sub modules under our package. In your case you can simple download the packaged code from Git link which I gave and can merge in your local folder.
I just installed caldav 0.5.0 using pip on Windows. I tried to use this code from the documentation:
from datetime import datetime
import caldav
from caldav.elements import dav, cdav
# Caldav url
url = "https://user:pass#hostname/caldav.php/"
client = caldav.DAVClient(url)
But I get this error:
AttributeError: module 'caldav' has no attribute 'DAVClient'
Does someone know what causes this issue?
It is because your file is named calendar.py, which causes some kind of collision somewhere. Renaming your file to something else will do the trick (it did for me).
I'm trying to connect to biometric device. I have installed 'Zklib' using (Pip). My code as follows
`import sys
import zklib
import time
from zklib import zkconst
zk = zklib.ZKLib("192.168.0.188", 4370)
ret = zk.connect()
print "connection:", ret`
When I execute this, I get an error
AttributeError: 'module' object has no attribute 'ZKLib'
Help me to run this code successfully.
Try the following instead:
import sys
import time
from zklib import zklib, zkconst
zk = zklib.ZKLib("192.168.0.188", 4370)
ret = zk.connect()
print "connection:", ret
The ZKlib class is in zklib.zklib not zklib. It appears that there is a typo in the Getting Started section of their GitHub page (or they expect you to be in the zklib directory when running your code?).
I'm trying to access AWS using Boto, and it's not working. I've installed Boto, and the boto.cfg in /etc. Here's my code:
import requests, json
import datetime
import hashlib
import boto
conn = boto.connect_s3()
Here's the error:
Traceback (most recent call last):
File "boto.py", line 4, in <module>
import boto
File "/home/mydir/public_html/boto.py", line 6, in <module>
conn = boto.connect_s3()
AttributeError: 'module' object has no attribute 'connect_s3'
What the hell? This isn't complicated.
It looks like the file you're working on is called boto.py. I think what's happening here is that your file is importing itself--Python looks for modules in the directory containing the file doing the import before it looks on your PYTHONPATH. Try changing the name to something else.
Use the Connection classes.
e.g.
from boto.s3.connection import S3Connection
from boto.sns.connection import SNSConnection
from boto.ses.connection import SESConnection
def connect_s3(self):
return S3Connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
def connect_sns(self):
return SNSConnection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
def connect_ses(self):
return SESConnection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
#valdogg21
I am following your instructions and put this into my code:
from boto.s3.connection import S3Connection
conn = S3Connection('<aws access key>', '<aws secret key>')
But despite my good intentions, it results in a small error. I just did
sudo pip install boto --upgrade to ensure I have the latest version installed.
This is the error message. Just wondering if I am a lone wolf or if others encounter this issue...
from boto.s3.connection import S3Connection ImportError: cannot import
name S3Connection
You may need to do something similar to how I had to utilize the EC2Connection class in some of my code, which looks like this:
from boto.ec2.connection import EC2Connection
conn = EC2Connection(...)
Also, from their docs (http://boto.s3.amazonaws.com/s3_tut.html):
>>> from boto.s3.connection import S3Connection
>>> conn = S3Connection('<aws access key>', '<aws secret key>')
EDIT: I know that doc page has the shortcut function you're trying to use, but I saw a similar problem when trying to do the same type of shortcut with EC2.
I have tried all of your solutions, but none of them seem to work. I keep going over StackOverFlow as I cannot see anyone else not having this rather small issue. Kind of weird fact is that in the server it works like a charm. The issue is on my Mac
I had this issue and was facing the same error when using boto3 and moto to mock s3 bucket.
boto3.connect_s3()
I switched back my library to boto and it worked fine. It looks like boto3 has migrated connect_s3() to resources():
boto.connect_s3() //works
boto3.resources('s3') //works
I could resolve similar issue for AWS Lambda too:
boto.connect_awslambda() //works
boto3.client('lambda') //works