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

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

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.

Getting ModuleNotFoundError: No module named 'azure'

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.

Trouble shooting when tring to install and import `stats_exporter` from `opencensus.ext.stackdriver`

Im trying to install and use stats_exporter from opencensus.ext.stackdriver using the following guide: opencensus-ext-stackdriver
after installing it through pip:
pip install opencensus-ext-stackdriver
Im trying to import it and:
from opencensus.ext.stackdriver import stats_exporter as stackdriver
ImportError: cannot import name 'stats_exporter' from 'opencensus.ext.stackdriver'
When comparing the Git repo, and my local venv/lib/python3.7/site-packages/... it seems like the pip version isn't compatible with Github , so i tried to install it though cloning, and using setup.py
pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
which gives me the following error:
(venv) Yehoshaphats-MacBook-Pro:present-value yehoshaphatschellekens$ pip install ../opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
Processing /Users/yehoshaphatschellekens/opencensus-python/contrib/opencensus-ext-stackdriver/dist/opencensus-ext-stackdriver-0.2.dev0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/setup.py", line 17, in <module>
from version import __version__
ModuleNotFoundError: No module named 'version'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/s2/y6vcdc1105s8xlpb12slr9z00000gn/T/pip-req-build-7m1ibdpd/
Similar errors of this type indicated that i need to upgrade setuptools, tried that also :(
This post suggests that it might related to the fact that i'm using python3, which isn't completable with version though i really need to install this package on my python3 venv.
Any Help on this issue would be great!
Try this:
#!/usr/bin/env python
import os
from opencensus.common.transports.async_ import AsyncTransport
from opencensus.ext.stackdriver import trace_exporter as stackdriver_exporter
from opencensus.trace import tracer as tracer_module
from opencensus.stats import stats as stats_module
def main():
sde = stackdriver_exporter.StackdriverExporter(
project_id=os.environ.get("PROJECT_ID"),
transport=AsyncTransport)
tracer = tracer_module.Tracer(exporter=sde)
with tracer.span(name='doingWork') as span:
for i in range(10):
continue
if __name__ == "__main__":
main()
and
grpcio==1.19.0
opencensus==0.3.1
opencensus-ext-stackdriver==0.1.1
NB The OpenCensus libraries need gRPC too.
You will need:
a GCP Project and it's Project ID (${PROJECT_ID})
a service account with roles/cloudtrace.agent and its key.
Then:
virtualenv venv
source venv/bin/activate
export PROJECT_ID=[[YOUR-PROJECT-ID]]
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/key.json
pip3 install --requirement requirements.txt
python3 stackdriver.py

How do i fix the following Python error: no module named Playback?

I am trying to run a script that was given to me and when i try to run it, I am given the following error message:
Traceback (most recent call last):
File "C:\Users\XX\Documents\XX\Project\Experiments\Y-Maze\Data\M06\ymaze_working_script1.py", line 74, in <module>
from playback import Playback
ImportError: No module named playback
Code that this error is from:
#################################################################
### DON'T EDIT FROM HERE ###
from ymaze_track import MouseTracker
from ymaze_mark import Marker
from ymaze_track import FileHandler
import os
import csv
import numpy as np
import time
from tkFileDialog import askopenfilenames
import sys
import Tkinter as tk
from playback import Playback
Is playback a module that needs to be added to python or is inbuilt in? and if so - where can I find it?
playback is not a standard python module so you must add it to your own system python modules
You can do this with run pip install playback
If you got other errors same as the error you described you can find package installation command by
searching in https://pypi.org/
And if your python code has requirement text file which most of the times named
requirements.txt
You can run pip install -r requirements.txt
For installing all needed package easily
playback is not a standard python module, so you have to install it to use it. Run this command from the command line:
pip install playback
If that doesn't work, this should:
py -m pip install playback
Here is a tutorial on using pip to install python modules.

How do I make Pip respect requirements?

If I create a setup.py using requires, Pip doesn't install my dependencies.
Here's my setup.py:
from distutils.core import setup
setup(name='my_project',
description="Just a test project",
version="1.0",
py_modules=['sample'],
requires=['requests'])
I wrote a simple sample.py:
import requests
def get_example():
return requests.get("http://www.example.com")
I then try to install it:
$ pip install -e . [15:39:10]
Obtaining file:///tmp/example_pip
Running setup.py egg_info for package from file:///tmp/example_pip
Installing collected packages: my-project
Running setup.py develop for my-project
Creating /tmp/example_pip/my_venv/lib/python2.7/site-packages/my-project.egg-link (link to .)
Adding my-project 1.0 to easy-install.pth file
Installed /tmp/example_pip
Note that requests, my dependency isn't installed. If I now try to use my test project:
$ python [15:35:40]
>>> import sample
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/example_pip/sample.py", line 1, in <module>
import requests
ImportError: No module named requests
What am I doing wrong?
The correct spelling is install_requires, not requires; this does require that you use setuptools, not distutils:
from setuptools import setup
setup(name='my_project',
description="Just a test project",
version="1.0",
py_modules=['sample'],
install_requires=['requests'])
I can recommend the Python Packaging User Guide for the nitty gritty details.

Categories