Unable to import storage from google.cloud - python

I'm new to Google Cloud Platform, and have uploaded some machine learning code on Jupyter notebook in DataLab.
My issue is although, I installed Google Cloud Storage (using the command: pip install --upgrade google-cloud-storage), I'm unable to import this.
The following is how I'm importing this package:
>>import numpy
>>import pandas as pd
>>from google.cloud import storage
But I'm getting the following error:
ImportErrorTraceback (most recent call last)
in ()
----> 1 from google.cloud import storage
ImportError: cannot import name storage
Note:
This is the content of my JSON config file: {"TokenSources":["env"]}
I tried export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file.json", but the error persists.
I verified that this package is indeed installed in my environment by typing pip freeze in the command shell:
google-cloud==0.34.0
google-cloud-datastore==1.7.0
google-cloud-spanner==1.4.0
google-cloud-storage==1.10.0
What am I missing here?

Have you installed the google-cloud-storage package in your DataLab environment, or on your local machine? You'll need to run the following command within DataLab:
!pip install google-cloud-storage
See https://cloud.google.com/datalab/docs/how-to/adding-libraries for more details
Also, the google-cloud package is deprecated, you shouldn't need to install it, see https://pypi.org/project/google-cloud/.

So I got it working upon importing storage as follows:
import google.datalab.storage as storage

To make your notebooks resilient to both datalab and non-datalab environments you can use one of the the following methods for handling your import statements:
try:
from google.cloud import storage
except ImportError:
from google.datalab import storage
or
if 'google.datalab' in sys.modules:
from google.datalab import storage
else:
from google.cloud import storage
Alternatively if you would like to switch datalab to using from google.cloud import storage
Run the following in a cell
!pip install google-cloud-storage
Followed by this cell to reset the IPython kernel
# Reset the IPython kernel
from IPython.core.display import HTML
HTML("<script>Jupyter.notebook.kernel.restart()</script>")
Note: You need to reset the Python kernel after installation otherwise you will a ContextualVersionConflict error from naming conflicts

Related

ModuleNotFoundError: No module named 'google' while using google-cloud-vision api

i ran the following code
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
the error i get is the following
Traceback (most recent call last):
File "F:\coding\pythonfolder\main.py", line 4, in <module>
from google.cloud import vision
ModuleNotFoundError: No module named 'google'
the library is installed is the following
pip install google-cloud-vision
I am fairly new to the google vision api so maybe there's some point I am missing
kindly let me know what the issue is, I tried pip install --upgrade google-cloud-vision as well but does not seems to work
my python version is Python 3.10.0
kindly let me know if there's any issue or missing information in my question
thank you!!
I'm pretty sure you messed up with the venv, since it worked fine for me. Try using PyCharm (if you don't already do) and setup a venv, or install virtualenv and launch these commands inside your project folder:
virtualenv venv
source venv/bin/activate
pip install google-cloud-vision
python your_file.py
deactivate
Last one is to properly exit the venv

Python ModuleNotFoundError: No module named 'azure.cognitiveservices'

I am following the tutorial from Microsoft ( https://learn.microsoft.com/nl-nl/azure/cognitive-services/Computer-vision/quickstarts-sdk/client-library?pivots=programming-language-python ) to use cognitive service. I use herefor Visual Code and install the Azure with pip using the commandline:
pip install azure-cognitiveservices-vision-customvision
I use the first peace of code(see code below) and try to run the code. But it returns the follwowing error:
(myvenv) PS C:\Users\erikh\OneDrive\Documenten\Git\Python Testlab> & "c:/Users/erikh/OneDrive/Documenten/Git/Python Testlab/myvenv/Scripts/python.exe" "c:/Users/erikh/OneDrive/Documenten/Git/Python Testlab/readText.py"
Traceback (most recent call last):
File "c:/Users/erikh/OneDrive/Documenten/Git/Python Testlab/readText.py", line 1, in <module>
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
ModuleNotFoundError: No module named 'azure.cognitiveservices'
And here is the code which i try to execute:
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
from array import array
import os
from PIL import Image
import sys
import time
# Add your Computer Vision subscription key to your environment variables.
if 'COMPUTER_VISION_SUBSCRIPTION_KEY' in os.environ:
subscription_key = os.environ['COMPUTER_VISION_SUBSCRIPTION_KEY']
else:
print("\nSet the COMPUTER_VISION_SUBSCRIPTION_KEY environment variable.\n**Restart your shell or IDE for changes to take effect.**")
sys.exit()
# Add your Computer Vision endpoint to your environment variables.
if 'COMPUTER_VISION_ENDPOINT' in os.environ:
endpoint = os.environ['COMPUTER_VISION_ENDPOINT']
else:
print("\nSet the COMPUTER_VISION_ENDPOINT environment variable.\n**Restart your shell or IDE for changes to take effect.**")
sys.exit()
I can reproduce your issue, you installed the wrong package, it should be azure-cognitiveservices-vision-computervision instead of azure-cognitiveservices-vision-customvision.
Run the line below, then it will work fine.
pip install azure-cognitiveservices-vision-computervision

How to import AdversarialModel, simple_gan, gan_targets in colab

I am importing keras_adversarial and cloning its git too but it cannot import 'AdversarialModel'
i am working on gans, built generator and discriminator. now i want to combine them but colab is giving error while importing the modules of keras_adversarial
import keras_adversarial
from keras_adversarial import AdversarialModel, simple_gan, gan_targets
from keras_adversarial import AdversarialOptimizerSimultaneous,
normal_latent_sampling
1 import keras_adversarial
----> 2 from keras_adversarial import AdversarialModel, simple_gan,
gan_targets
3 from keras_adversarial import AdversarialOptimizerSimultaneous,
normal_latent_sampling
ImportError: cannot import name 'AdversarialModel'
The Colab environment only have a core set of packages installed. To add some third-party packages as keras_adversarial, you can execute installation script direcrly in Colab cells, with ! symbol before each command to indicate that these are command line bash code, not Python.
In your case, you need to do:
!git clone https://github.com/bstriner/keras_adversarial.git
!cd keras_adversarial
!python setup.py install

How to use Github Package in Jupyter Notebooks

I am trying to use the dygraphs plot function from [here] (https://github.com/dinkelk/PyDyGraphs) in my jupyter notebook. The documentation says to:
Installation
Simply clone this repository and include the dygraphs.graph module in >>your Jupyter Notebooks. Note: PyDyGraphs only supports Python 3.
The documentation says to use "import dygraphs.graph as dy"
I'm not sure where to put the repository once I've downloaded it.
The package I've downloaded and unzipped. I've included the "import dygraphs.graph" in my notebook.
This package requires pandas to be installed and imported which I have done.
import numpy as np
import dygraphs.graph as dy
_____
ModuleNotFoundError Traceback (most recent
call last)
<ipython-input-3-29a3c1e17595> in <module>
4 import pandas as pd
5 import time
----> 6 import dygraphs.graph as dy
ModuleNotFoundError: No module named 'dygraphs'
_____
I'm wondering where I should have put the files downloaded from github and if there is anything else I should be doing in order to use this package.
You need to add a system path (or install the package to the default python libary) before using "import dygraphs.graph". See the example .
Add something like this in your jupyter notebook:
sys.path.append("../")
Then change "../" to the relative path to "dygraphs" folder you downloaded.

from google.cloud import monitoring_v3 ImportError: cannot import name monitoring_v3

I am trying to utilize the Google Cloud Platform's Stack Driver API with the following Python/Flask code below...
view.py
import google.cloud
from google.cloud import monitoring_v3
# from google.cloud.monitoring_v3 import query
requirements.txt
Flask==0.12.2
oauth2client==2.0.1
google-api-python-client==1.6.2
facebookads==2.11.1
httplib2==0.10.3
enum==0.4.6
requests-toolbelt==0.8.0
google-cloud-storage==1.6.0
google-resumable-media==0.3.1
google-auth
google-cloud-monitoring==0.28.0
google-cloud
For whatever reason it can't seem to find the monitoring_v3 library as when whenever I run the code I am getting the following error...
from google.cloud import monitoring_v3
ImportError: cannot import name monitoring_v3
I have tried to update to google-cloud-monitoring==0.29.0 and that just churns out another non-related error.
I solved it by upgrading monitoring 0.29.0 to 1.1.0
pip install --upgrade google-cloud-monitoring
Latest version now available is 2.0.0
https://pypi.org/project/google-cloud-monitoring/
Could be a conflict (dependency or otherwise) between google-api-python-client and google-cloud*.
The former is a REST client which includes the monitoring API, the latter is gRPC based client.
Are you using 'google-api-python-client' for anything else? If no, try removing that?
Make sure your requirements.txt file is proper, and located in the source directory where you have the main.py.
If everything fails , copy a requirements.txt file from a working function , and replace the requirements modules in it (strangely this worked for me :P)

Categories