Getting ModuleNotFoundError: No module named 'azure' - python

I am using Azure Pipelines and getting the following error:
ImportError: Failed to import test module: QueryUrls
Traceback (most recent call last):
File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "/home/vsts/work/1/s/QueryUrls/__init__.py", line 2, in <module>
import azure.functions as func
ModuleNotFoundError: No module named 'azure'
The error occurs when I run unit tests for an Azure Function written in Python. The code I am runnning in the pipeline (to run the unit tests) is the following:
- script: |
python -m unittest QueryUrls/test_queryurls.py
displayName: 'Test with unittest'
The pipeline was running correctly before I added the above lines. Here is the script that is being called:
# tests/test_httptrigger.py
import unittest
from pathlib import Path
import azure.functions as func
from . import main
#from QueryUrls import my_function
class TestFunction(unittest.TestCase):
def test_my_function(self):
# Construct a mock HTTP request.
req = func.HttpRequest(
method='GET',
body=None,
url='/api/QueryUrls',
params={'name': 'World'})
# Call the function.
resp = my_function(req)
# Check the output.
self.assertEqual(
resp.get_body(),
b'Hello World',
)
if __name__ == '__main__':
unittest.main()

Proper way to do this is to have a requirements txt which has all the dependencies
pip install -r requirements.txt -r requirements-test.txt
add the azure-functions to the requirements-text.txt and run the script at the beginning

Are you sure that azure.function is isntalled on the machine?
Please install it before you run your tests:
script: pip install azure-functions
For pip list on agent I got this:
2020-07-17T12:50:55.7295699Z Generating script.
2020-07-17T12:50:55.7324332Z Script contents:
2020-07-17T12:50:55.7324840Z pip list
2020-07-17T12:50:55.7325214Z ========================== Starting Command Output ===========================
2020-07-17T12:50:55.7366079Z [command]/bin/bash --noprofile --norc /home/vsts/work/_temp/64166a13-122f-49af-b5af-153c399305d9.sh
2020-07-17T12:50:57.7202987Z ansible (2.9.10)
2020-07-17T12:50:57.7249252Z chardet (2.3.0)
2020-07-17T12:50:57.7250502Z crcmod (1.7)
2020-07-17T12:50:57.7251632Z cryptography (1.2.3)
2020-07-17T12:50:57.7252618Z ecdsa (0.13)
2020-07-17T12:50:57.7253446Z enum34 (1.1.2)
2020-07-17T12:50:57.7254214Z httplib2 (0.9.1)
2020-07-17T12:50:57.7255377Z idna (2.0)
2020-07-17T12:50:57.7256319Z ipaddress (1.0.16)
2020-07-17T12:50:57.7257152Z Jinja2 (2.8)
2020-07-17T12:50:57.7257929Z MarkupSafe (0.23)
2020-07-17T12:50:57.7258731Z mercurial (4.4.1)
2020-07-17T12:50:57.7259442Z paramiko (1.16.0)
2020-07-17T12:50:57.7260158Z pip (8.1.1)
2020-07-17T12:50:57.7260864Z pyasn1 (0.1.9)
2020-07-17T12:50:57.7261625Z pycrypto (2.6.1)
2020-07-17T12:50:57.7262364Z Pygments (2.1)
2020-07-17T12:50:57.7263653Z PyYAML (3.11)
2020-07-17T12:50:57.7264117Z setuptools (20.7.0)
2020-07-17T12:50:57.7264553Z six (1.10.0)
So as you see there is no azure.functions.

Related

Why I can't import my own module in python?

I had trying a app using Flask.
I'm used my package UsefulDeveloperTools.
Its __init__.pylike this: (__init__.py)
"""
Useful Tools.
"""
import threads
import IDgenerator
And its directory is like this: (directories
.
\ __init__.py
\ threads.py
\ IDgenerator.py
And I pushed it in TestPyPI and installed it in my virtual environment used python3 -m pip install --upgrade --index-url https://test.pypi.org/simple/ --no-deps UsefulDeveloperTools.
And I have activated my environment,
and run following code use python3 main.py: (main.py)
import flask
from threading import Thread
import time
import random
import UsefulDeveloperTools # Error this
import logging
app=flask.Flask(__name__)
# unimportance
But python raised an error: (terminal)
(Blog) phao#phao-virtual-machine:~/桌面/pypoj/Blog$ python3 main.py
Traceback (most recent call last):
File "/home/phao/桌面/pypoj/Blog/main.py", line 5, in <module>
import UsefulDeveloperTools
File "/home/phao/桌面/pypoj/Blog/lib/python3.10/site-packages/UsefulDeveloperTools/__init__.py", line 5, in <module>
import threads
ModuleNotFoundError: No module named 'threads'
Why? What wronged? How can I finish it?
P.S. This is my packages in my environment: (terminal)
(Blog) phao#phao-virtual-machine:~/桌面/pypoj/Blog$ pip list
Package Version
-------------------- -------
click 8.1.3
Flask 2.2.2
itsdangerous 2.1.2
Jinja2 3.1.2
Markdown 3.4.1
MarkupSafe 2.1.1
pip 22.3.1
setuptools 59.6.0
UsefulDeveloperTools 0.2.2
Werkzeug 2.2.2
I looked on your repo and you are not following the packaging guide at all. You should reorganize your code in a proper file structure and set up eg. a pyproject.toml.
Python has an easy tutorial regarding packaging you can find here:
https://packaging.python.org/en/latest/tutorials/packaging-projects.html
Additionally your __init__.py is wrong. There are several options how you can put imports in your init file. The closes to what you attemptet would be to place a dot before the module names. But I don't know if that helps, before fixing the general package.
"""
Useful Tools.
"""
import .threads
import .IDgenerator
Check out this article for different options for init styles:
https://towardsdatascience.com/whats-init-for-me-d70a312da583
if you have made the module then keep it in the same directory as your file and run it.

ModuleNotFoundError: No module named 'google' - Python & Google TTS

I have a batch file that generates wav files using Python and Google TTS. This shell script runs fine on Windows 10. I need to run the script on a Linux web server (as a shell script) but I get the error
ModuleNotFoundError: No module named 'google'
Running help('modules') in a Python shell gives me the following google modules that are currently installed:
google_auth_httplib2
googleapiclient
googlesearch
Running pip install --upgrade google gives me:
Collecting google
Using cached https://files.pythonhosted.org/packages/81/51/36af1d18648574d13d8f43e863e95a97fe6f43d54a13fbcf272c638c10e9/google-2.0.3-py2.py3-none-any.whl
Collecting beautifulsoup4 (from google)
Using cached https://files.pythonhosted.org/packages/cb/a1/c698cf319e9cfed6b17376281bd0efc6bfc8465698f54170ef60a485ab5d/beautifulsoup4-4.8.2-py3-none-any.whl
Collecting soupsieve>=1.2 (from beautifulsoup4->google)
Using cached https://files.pythonhosted.org/packages/81/94/03c0f04471fc245d08d0a99f7946ac228ca98da4fa75796c507f61e688c2/soupsieve-1.9.5-py2.py3-none-any.whl
Installing collected packages: soupsieve, beautifulsoup4, google
Successfully installed beautifulsoup4-4.8.2 google-2.0.3 soupsieve-1.9.5
But I still only have the same three modules mentioned above. . When I run the script sudo -u www-data sh testfile.sh I get the following error:
Traceback (most recent call last):
File "TTSA.py", line 37, in <module>
f'<speak>{speech}</speak>'
File "TTSA.py", line 9, in synthesize_ssml
from google.cloud import texttospeech
ModuleNotFoundError: No module named 'google'
Here is the Python script:
import sys
filename = str(sys.argv[1])
speech = str(sys.argv[2])
def synthesize_ssml(ssml):
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'creds.json'
from google.cloud import texttospeech
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.types.SynthesisInput(ssml=ssml)
# Note: the voice can also be specified by name.
# Names of voices can be retrieved with client.list_voices().
voice = texttospeech.types.VoiceSelectionParams(
language_code='en-GB',
name='en-GB-Wavenet-A'
#ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE
)
audio_config = texttospeech.types.AudioConfig(
audio_encoding=texttospeech.enums.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
speaking_rate=1.0
#pitch=2
)
response = client.synthesize_speech(input_text, voice, audio_config)
# The response's audio_content is binary.
with open(f'{filename}.wav', 'wb') as out:
out.write(response.audio_content)
print(f'Audio content written to file {filename}.wav')
synthesize_ssml(
f'<speak>{speech}</speak>'
)
Versions running:
$ pip -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.7)
$ pip2 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.7)
$ pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
python3.6 google modules:
google_auth_httplib2
google_oauth2_tool
googleapiclient
googlesearch
python3.7 google modules:
google_auth_httplib2
googleapiclient
googlesearch

No module named 'requests' when pip installing ssh-import-id

I'm using Heroku as a development server. When I try to push my Django application to Heroku it first tries to install my packages from the requirements.txt file.
requests==2.18.3
ssh-import-id==5.5
The problem is I have a dependency on one of my packages with others. In the above packages, ssh-import-id needs requests package already installed. So when I push the app, pip fails to install and stops the deployment.
Collecting requests==2.18.3 (from -r re.txt (line 1))
Using cached https://files.pythonhosted.org/packages/ba/92/c35ed010e8f96781f08dfa6d9a6a19445a175a9304aceedece77cd48b68f/requests-2.18.3-py2.py3-none-any.whl
Collecting ssh-import-id==5.5 (from -r re.txt (line 2))
Using cached https://files.pythonhosted.org/packages/66/cc/0a8662a2d2a781db546944f3820b9a3a1a664a47c000577b7fb4db2dfbf8/ssh-import-id-5.5.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-go0a5mxf/ssh-import-id/setup.py", line 20, in <module>
from ssh_import_id import __version__
File "/tmp/pip-install-go0a5mxf/ssh-import-id/ssh_import_id/__init__.py", line 25, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-go0a5mxf/ssh-import-id/
I need to install all the listed packages using pip in single attempt. Because by default Heroku runs, pip install -r requirements.txt.
This is a bug.
The library's setup.py imports the library to get the version for inclusion in the setup() function call...
import os
from setuptools import setup
from ssh_import_id import __version__
... and the library tries to import requests which doesn't yet exist in the environment. This is ssh_import_id.__init__.py:
import argparse
import json
import logging
import os
import platform
import requests # <=== here
import stat
import subprocess
import sys
import tempfile
A fix has been added which works around needing to import the package to get the version...
import os
from setuptools import setup
import sys
def read_version():
# shove 'version' into the path so we can import it without going through
# ssh_import_id which has deps that wont be available at setup.py time.
# specifically, from 'ssh_import_id import version'
# will fail due to requests not available.
verdir = os.path.abspath(
os.path.join(os.path.dirname(__file__), "ssh_import_id"))
sys.path.insert(0, verdir)
import version
return version.VERSION
... but the fix isn't in the current pypi version 5.6.
You could install latest master branch from source instead of pypi by changing your requirements.txt to something like:
-e git+https://git.launchpad.net/ssh-import-id#egg=master

Python import google.oauth2.credentials ImportError: No module named google.oauth2.credentials

i'm trying to run this bot https://github.com/ItsCEED/Youtube-Comment-Bot
in mac os but i get this error
l:Youtube-Comment-Bot-master ROOTXX$ python yt.py
Traceback (most recent call last):
File "yt.py", line 6, in <module>
import google.oauth2.credentials
ImportError: No module named google.oauth2.credentials
l:Youtube-Comment-Bot-master ROOTXX$
Run in a root terminal:
pip install --upgrade google-api-python-client
From here, found with a google search.
EDIT: Even better, you linked the required libraries yourself. They are in the GitHub README.md at here. Please read your own links. Anyway, the commands to install follow:
# pip install --upgrade google-api-python-client
# pip install --upgrade requests
# pip install --upgrade beautifulsoup4

AttributeError: module 'urwid' has no attribute 'Text'

I want to program in urwid. But after pip install of package "urwid".If i want to check the package with simple program like this:
import urwid
txt = urwid.Text(u"Hello World")
fill = urwid.Filler(txt, 'top')
loop = urwid.MainLoop(fill)
loop.run()
and save it as some_name.py . Error is thrown, while running in terminal like:
$ python <some_name>.py
Traceback (most recent call last):
File "urwid.py", line 1, in <module>
import urwid
File "/home/<user's directory>/<some_name>.py", line 2, in <module>
txt = urwid.Text(u"Hello World")
AttributeError: module 'urwid' has no attribute 'Text'
if you want to see list of python package installed in my system is:
$ pip list
appdirs (1.4.0)
Brlapi (0.6.5)
chardet (2.3.0)
cupshelpers (1.0)
Cython (0.25.1)
isc (2.0)
louis (3.0.0)
packaging (16.8)
pip (8.1.2)
psutil (5.0.0)
pycups (1.9.73)
pycurl (7.43.0)
pygobject (3.22.0)
pyparsing (2.1.10)
pyxdg (0.25)
requests (2.11.1)
setuptools (28.8.0)
simplejson (3.10.0)
six (1.10.0)
team (1.0)
termcolor (1.1.0)
urllib3 (1.19.1)
urwid (1.3.1)
youtube-dl (2016.11.14.1)
version of pip i have installed is
$pip --version
pip 8.1.2 from /usr/lib/python3.5/site-packages (python 3.5)
please help me out!!Thanks in advance
Issues like this only happen in two cases:
There is a file in your current directory same name as one of the imported libraries.
You have named the file same as one of your imported libraries.
The library, in this case, is urwid.

Categories