Module import Error and NameError in Flask using Heroku - python

I do have this issue when deploying a small app to heroku, the requirements.txt is very minimal, the app crash with Cannot import module 'NewsApiClient' when using
from newsapi import NewsApiClient
and the app actually start with a 500 Internal Error when using from newsapi import *
NameError: name 'NewsApiClient' is not defined
newsapi = NewsApiClient(api_key='xxxxxxxxxx')
What I've done so far is create an empty init file on the root folder

It appears you are importing the incorrect package in your requirements.txt file. You have imported the newsapi package which is different then the python-newsapi package.
Changing the newsapi==0.1.1 to newsapi-python==0.2.3 should fix your problem with the importing error as long as you reinstall the package dependencies.

Related

Unable to import module when developing a python package

My project structure is:
APP(dir)
app(dir)
model(dir)
metric_data.py
__init__.py
app.py
setup.py
When I'm running app.py with import statement of
from model.metric_data import MetricData
MetricData is a class, I'm able to successfully run the application retrieving data from metric_data.py file. But when I build it as a package and then try importing package app
from model.metric_data import MetricData
this statement is failing. Can anyone help me with the issue
here, I looked on the relative import part and tried but it didn't work.
Try to create a __init__.py file in the model directory.

No module named 'openpyxl' when trying to import it from flask app

No module named 'openpyxl' when trying to import it from flask app , pointing that the module is installed in "/home/ubuntu/.local/lib/python3.8/site-packages/" and it works fine in other scripts
here what i am using
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/upload_file', methods=['GET','POST'])
def upload_file():
try :
import sys
sys.path.append('/home/ubuntu/.local/lib/python3.8/site-packages/')
import openpyxl
except Exception as e:
return render_template('index.html',msg=str(e))
return render_template('index.html',msg=request.method)
i've seen a lot of questions about how to import a module in flask since i've found it as a common error , so i've tried to create a module that does nothing but import openpyxl but no luck either i had the same error
I am using ubuntu 20.04 python3.8
Thanks in advance
I found two solutions :
1 .
Install the module manually in /usr/local/lib/python3.8/dist-packages using python3 -m pip3 install openpyxl in that directory
2 .
change the directory where the modules are installed using pip by creating the config file ~/.config/pip/pip.conf , the content must be your desired install location like this :
[global]
target = /usr/local/lib/python3.8/dist-packages
I hope this help someone with the same need

python no module named 'A' __init__ vs package

I have looked up several answers on google but none of them made my problem correct.
I made my python file in directory A
in my __init__.py I have app = Flask(__name__)
When I want to get app from other file I use:
from A import app
but error says no module named 'A' so I changed to
from __init__ import app
now it works
Can anyone tell me why from A import app does not work?

GAE: ImportError while using google-auth

I am using google-auth to allow firebase authentication in my GAE project.
Everything works fine when I run the code locally using dev_appserver.py or when I deploy it to google app engine.
But I get this ImportError exceptions when I try to use Django's manage.py script to create/run migrations.
ImportError: Could not import 'firebase.authentication.FirebaseAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: No module named auth.transport.requests.
The google-auth module is installed under lib directory and has this structure:
- lib
- google
- auth
- oauth2
These import cause the ImportErrors:
import google.auth.transport.requests
from google.oauth2 import id_token
My guess is that there might be naming conflicts as other imports work fine.
Please help!
If you want to use 3rd party libraries that are not included in this list, then you'll have to add them manually since you have done that by adding lib folder and including all the package folder follow these steps.
Create your_app_directory/appengine_config.py file.
Add these following lines inside that file
from google.appengine.ext import vendor
vendor.add('lib')
This should solve the import error problem.

GAE import errors; sys.path shows wrong path for appengine libraries

I'm developing a web app for Google App Engine in Python on Windows 10. Everything was working fine when my main.py was just serving templates.
import os
import urllib
from google.appengine.api import users
import jinja2
import webapp2
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
But then I tried to add cloud storage, and got import errors no matter what library I tried. So I removed those references from main.py, and now I get an error with jinja2, which had been working fine!
ImportError: No module named jinja2
I don't remember everything I tried, but here's what I do know:
jinja2 is installed; attempts to pip install/upgrade says it's already installed and up-to-date, and I see it in c:\python27\lib\site-packages.
PYTHONPATH=C:\python27;c:\python27\lib;C:\Python27\DLLS for system and user.
At one point I had installed GoogleAppEngineCloudStorageClient with PIP into my app's lib directory per this. It didn't work (the module failed to import), so I also tried adding sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) but it didn't help. I think this is when I started getting the jinja2 import error. So I removed the statement from main.py. Still getting the jinja2 import error. I tried pip uninstall GoogleAppEngineCloudStorageClient but it said it wasn't installed, so I tried just deleting the lib directory. Still getting the jinja2 import error.
Have tried restarting the service and rebooting my machine.
EDIT:
I stripped main.py all the way to the new project template,
import webapp2
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write('Hello world!')
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
and now I get an import error for webapp2:
ImportError: No module named webapp2
EDIT 2:
By inserting this at the top of my main.py,
import sys
print sys.path
I can see the wrong path for all the google packages:
'C:\\Program Files (x86)\\Google\\lib\\webapp2-2.5.2',
'C:\\Program Files (x86)\\Google\\lib\\pycrypto-2.6',
'C:\\Program Files (x86)\\Google\\lib\\jinja2-2.6',
'C:\\Program Files (x86)\\Google\\lib\\markupsafe-0.15',
'C:\\Program Files (x86)\\Google\\lib\\setuptools-0.6c11',
'C:\\Program Files (x86)\\Google\\lib\\protorpc-1.0',
'C:\\Program Files (x86)\\Google\\lib\\webob-1.1.1',
'C:\\Program Files (x86)\\Google\\lib\\yaml-3.10'
They are actually in C:\Program Files (x86)\Google\google_appengine\lib
I don't know why I didn't have this problem before I tried to install that one package, but this may be related to a reported google issue.
Update: the issue was fixed in SDK version 1.9.40.
There is a GAE issue causing exactly this behaviour introduced in SDK version 1.9.37, see "ImportError: No module named webapp2" after Linux SDK upgrade (1.9.35 -> 1.9.38).
If your SDK version is 1.9.37 or 1.9.38 downgrade to 1.9.36, which you can find here. At least until the fix gets released.
Summary:
The webapp2 and jinja2 import errors are caused by sys.path corruption, a result of a GAE defect present in versions 1.9.37 or 1.9.38. It only impacts development; deployed versions should work. It can occur immediately upon upgrading or after attempting to install other items.
Solutions:
Downgrading the AppEngineSDK to 1.9.36 worked for me.
The GAE defect will be fixed at some point after 1.9.38. Check for Issue 12963 for status.
Per Google issue 13084, another workaround is to manually patch sys.path in appengine_config.py. Docs here.

Categories