ImportError running Google's python appengine on Ubuntu - python

I'm trying to teach myself python using Google's AppEngine, and I can't get the dev server running. I get this error:
Traceback (most recent call last):
File "/opt/google_appengine/google_appengine_1.2.7/dev_appserver.py",
line 60, in
run_file(file, globals()) File
"/opt/google_appengine/google_appengine_1.2.7/dev_appserver.py",
line 57, in run_file
execfile(script_path, globals_) File
"/opt/google_appengine/google_appengine_1.2.7/google/appengine/tools/dev_appserver_main.py",
line 65, in
from google.appengine.tools import os_compat ImportError: cannot import
name os_compat
Ubuntu 9.10 comes with python2.6 (didn't work), and I installed python2.5 (didn't work), and have tried running it with python dev_appserver.py helloWorld (didn't work) as well as running dev_appserver.py after editing the first line to be:
#!/usr/bin/env python2.5
I can't seem to find anything online with this error. The only problem I've found is about using python 2.5, and I think I've solved that.
Kyle suggested I need to set my PYTHONPATH variable. After running
export PYTHONPATH=/opt/google_appengine/google_appengine_1.2.7
I still get the same error trying to run dev_appserver.py. Am I setting PYTHONPATH wrong? Alternatively, how do I uninstall the protocol buffers python project? I have no use for Ubuntu One and had already uninstalled it.

The problem appears to be the fact that Karmic Koala 9.10 (the latest version of Ubuntu) ships with Ubuntu One, a python app that depends on Google's protocol buffers library. The python-protobuf package provides the google.protobuf package in /usr/lib/pymodules/python2.6.
Unfortunately, the AppEngine SDK includes another package called google.appengine. So somewhere in your code, the google package is being imported, and the package that contains protobuf is being found on PYTHONPATH first. Python caches the first package it finds in sys.modules, so the second google package in the SDK will never be imported.
You could move the google AppEngine SDK up to the front of your PYTHONPATH. That should ensure that Python finds the google.appengine package instead of the package provided by python-protobuf.
PYTHONPATH=/opt/google_appengine/google_appengine_1.2.7 \
python dev_appserver.py helloWorld
This is a bug that should be reported to the AppEngine SDK project.
Update: I've submitted a bug against the AppEngine API.

It was a file permission problem. os_compat.py wasn't readable by user, just by root. I'm not sure if I screwed this up, or if the permissions by default don't have read-all, but that was the fix.
I hate to accept my own answer after Kyle gave such a good response, but I don't need the $PYTHONPATH fix to make it work now that I did sudo chown -R +r /opt/google_appengine/google_appengine_1.2.7

With that error, Python is saying that it can't find or read the name that it's trying to import. Since the import of os_compat is the very first executable line of AppEngine's dev_appserver.py, I suspect that there's a problem with the way that your paths are configured.

The latest version of Ubuntu (10.10) has also removed Python 2.5 - making it a pain to install the App Engine development environment.
I (finally) got my environment working (including using App Engine Helper for unit testing). I built this bash script which might be useful to others. It installs:
sqlite
libsqlite
pep8
mock
OpenSSL
Python 2.5.2
Python SSL Library
Django 1.1 (latest version in production)
App Engine
App Engine Helper
http://pageforest.googlecode.com/hg/tools/pfsetup

Ubuntu 11.04 comes with python 2.6 as the default version. It is suggested to use Google app engine with version 2.5. I am using it though for many years with python 2.6 without any issues.
What you need to do in order to execute it smoothly with python 2.6 is to edit google/appengine/tools/dev_appserver.py and add these three lines
'_counter',
'_fastmath',
'strxor',
after 'XOR', and before '_Crypto_Cipher__AES', around line ~1350.

If you are now using Google Cloud SDK, put this into ~/.profile.
export CLOUDSDK_ROOT_DIR="/path/to/google/cloud/sdk/"
export APPENGINE_HOME="${CLOUDSDK_ROOT_DIR}/platform/appengine-java-sdk"
export GAE_SDK_ROOT="${CLOUDSDK_ROOT_DIR}/platform/google_appengine"
# The next line enables Java libraries for Google Cloud SDK
export CLASSPATH="${APPENGINE_HOME}/lib":${CLASSPATH}
# The next line enables Python libraries for Google Cloud SDK
export PYTHONPATH=${GAE_SDK_ROOT}:${PYTHONPATH}
# * OPTIONAL STEP *
# If you wish to import all Python modules, you may iterate in the directory
# tree and import each module.
#
# * WARNING *
# Some modules have two or more versions available (Ex. django), so the loop
# will import always its latest version.
for module in ${GAE_SDK_ROOT}/lib/*; do
if [ -r ${module} ]; then
PYTHONPATH=${module}:${PYTHONPATH}
fi
done
unset module
Do not put inside ~/.bashrc because, every time you open a bash session, all those modules will be added again and again into your PYTHONPATH environment variable.

Related

Flask App with Azure AD Example on Windows 10

I try the example on: https://github.com/Azure-Samples/ms-identity-python-webapp with Windows 10, but I get an error with ModuleNotFoundError: No module named 'flask_caching.backends.filesystem' (Flask-Caching is already installed with pip).
Version:
Python 3.9.9,
Flask 1.1.4 and
Werkzeug 1.0.1.
I only changed the code with Client_ID, CLient_Secret and domain name in app_config.py.
Has anybody an idea?
The error ModuleNotFoundError means python interpreter cannot find the libraries which you are referring to in the code although the module is already installed.
Common causes of this error:
Using modules meant for different python versions but Installing python 2.x modules in python 3.x and vice a versa.
When not properly setting PATH variable.
(Or)
If you are using a python virtual environment. It need to be installed after creating a virtual environment as commented by #grumpyp . The libraries will reside inside the folder created for the virtual environment.
And can installed according to requirements.txt file
pip install virtualenv
It requires activation and dedicated installation of modules inside the virtual environment.
Refer this blog for more details to do
pip install -r requirements.txt
Other reference :Set Up a Virtual Python Environment (Windows)
(or )
This may not be your query but Just to make it a bit easy You can try this way when trying out your sample project to compare with manually configured one.
The quick start: "Add sign-in with Microsoft to a Python web app" that you are using ,can be directly configured in portal quickstart like below where every thing is configured including client id ,tenant id etc directly.
Just register the app with name and account type and follow the steps below for direct configuration .
Go to quickstart page of app
Select Python as platform for web application
Just follow the steps to configure azure ad inside app directly
There after following the steps , I checked the versions with pip freeze and
versions i have: Python 3.9.7, Flask 1.1.4 and Werkzeug 1.0.1.
quickstart-v2-python-webapp | microsoftdocs

Google App Engine ImportError: No module named

I am creating a Python Web App in Google App engine.
When I
sudo pip install
a third party library and then try to import it, I get the error 'ImportError: No module named x'. Where x is the name of that library. In my case as an example: Boto, Boto3, Fask etc.
If I go into shell in GAE and type python >> import X the library can be used inside the python environment. When deploying the app though or running the app in the virtaul server in Google App Engine I get the module import error.
I even tried methods like: python >> import sys >> sys.path.insert(0, "path_here")
export PYTHONPATH and selected where those libraries are located
I even followed several Q&A here in Stackoverflow without any success, can somebody please give me a proper way to fix the import error in Google App Engine?
FYI
I am not using any local environment in my pc, I am working directly through the GAE bash console, the launch code editor in GAE and I am running the command dev_appserver.py $PWD
When I do
pip freeze
I can see that the modules are currently installed and deployed on the GAE virtual environment. Is there a problem with my path? What's the best approach to make GAE load my already installed third party libraries.
UPDATE:
Importing the library directly on the python shell from Google App Engine works just fine. Importing the library on my python app index.py file results in the error.
Python import directly from Shell
Python import to the index.py file
Though this is an old thread, adding this answer now:
Run command : gcloud components list
This will show the different components installed and not in your environment.
Install app-engine-python components if not installed:
gcloud components install app-engine-python
gcloud components install app-engine-python-extras
If it doesn't work:
In Windows, uninstall and download and install Google-sdk (check the python version you need). Delete all the files the installer ask you to delete in the last step and run the gcloud component commands again.

python "ImportError: No module named xyz" on shared hosting

Similar questions have been asked on SO, I tried to follow their solution but so far no success.
I have downloaded gspread from here to use it in my project.
Locally in mac + eclipse + pydev everything works fine.
When I copy the code to my shared hosting environment (centos, bluehost) I get the error no module named gspread:
Traceback (most recent call last):
File "ReadSpreadsheet.py", line 8, in
import gspread
ImportError: No module named gspread
Here is my directory structure:
src/
gspread/
__init__.py
SpreadsheetReader/
__init__.py
ReadSpreadsheet.py (calls import gspread)
Python version is 2.7 both locally and on shared hosting env. On shared I installed python 2.7 myself in ~/python.
I am not sure when it works locally in pydev why it doesn't work in the shared hosting environment.
Since I only develop locally and just use git pull on the sever to copy the code my preferred solution is not to have a custom code for the server. Setting env variables, etc is not perfect but acceptable.
Sorry in advance if the question looks similar. I was struggling for the past day with no luck. I think this should be a fairly common importing situation.
Thanks to the comments, installing pip and then installing gspread using pip solved the problem.
pip install gspread
The command above is mentioned in the gspread documentation.

Pycharm issue - ImportError: No module named appengine.datastore.datastore_query

I have been trying to use the excellent project located at http://docs.gae-init.appspot.com/, in order to learn python, GAE and perhaps start my own hobby project.
My IDE of choice is Pycharm 3.4 Professional Edition. Operating system is OSX Mavericks.
I have been trying to follow the tutorial, therefore I have git cloned the project at a local directory and subsequently opened in into Pycharm. I believe I have managed to resolve the majority of requirements (PyCharm helps a lot).
Nevertheless, when trying to Run the project I get the following error:
File "/Users/nilminus/Developer/phonebook/main/main.py", line 9, in <module>
import util
File "/Users/nilminus/Developer/phonebook/main/util.py", line 10, in <module>
from google.appengine.datastore.datastore_query import Cursor
ImportError: No module named appengine.datastore.datastore_query
I have configured GAE directory as /usr/local/google_appengine which is correct.
Pycharm does not complain about not finding this module before actually executing it.
There is a warning that module google is not listed in requirements.txt, but it does not makes sense to me that this could be the issue.
I have included PYTHONPATH in .profile and ~/.bash_profile:
PYTHONPATH=/usr/local/google_appengine/:/usr/local/google_appengine/lib/:/usr/local/google_appengine/lib/yaml/
The funny thing is that if I upload this exact project in GAE, it works perfectly.
Also, if I run this project with ./run.py -s (as exhibited by its author), it works again.
Even ./dev_appserver.py main/ works.
This issue has truely dazzled me that last week.
Any help is highly appreciated.
When creating Google Engine App project, specify App Engine SDK path in App Engine SDK directory field:
Note: PyCharm automatically detects the GAE installation path, and displays it in this field. However, Auto-detection works for Windows and OS X. For Linux, you have to specify the App Engine SDK yourself.
Also specify SDK path in Settings

ImportError: No module named Foundation

I am trying to follow the instructions for the accepted answer to "PyObjC development with Xcode 3.2". I will repost them here since I don't have enough rep to comment on the actual question:
Here's what I have done to get PyObjC working in Snow Leopard:
Using the Finder, I went to Go > Connect to Server... and connected to http://svn.red-bean.com/pyobjc/trunk/pyobjc/pyobjc-xcode/ as a guest.
I then made a folder called Xcode on my local system at ~Library/Application Support/Developer/Shared/Xcode/. (You may already have this folder, but I hadn't customized anything for myself yet).
I copied the File Templates folder from the red-bean server into my new Xcode folder.
Copied the Project Templates folder to some other place, for example, the Desktop.
Using the Terminal, navigated to the temporary Project Templates folder on my Desktop and ran this command to "build" the template.:
$ cd ~/Desktop/Project\ Templates/
$ ./project-tool.py -k -v --template ~/Desktop/Project\ Templates/Cocoa-Python\ Application/CocoaApp.xcodeproj/TemplateInfo.plist Cocoa-Python\ Application ~/Library/Application\ Support/Developer/Shared/Xcode/Project\ Templates/Cocoa-Python\ Application
When I try to run the line that starts with ./project-tool.py, I get the following error in Terminal:
Traceback (most recent call last):
File "./project-tool.py", line 22, in <module>
from Foundation import NSDictionary
ImportError: No module named Foundation
I am running Snow Leopard and have installed Xcode 3.2.1 and have read that this module should already be installed and working. I've read that you can test if the PyObjC modules are working by running >>> import objc in the Python command-line. When I run this, I get:
>>> import objc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named objc
Could anyone help me dispel this error? It seems like I should be able to do all of this automatically with my Snow Leopard installation, but I can't.
I had the same problem. Mine was caused I think by using homebrew to install my own Python to tinker with.
Because I was worried about mixing python versions, rather than creating the link as described above, I installed a new pyobjc using:
$ pip install pyobjc
For interest, from (http://pythonhosted.org/pyobjc/)
The PyObjC project aims to provide a bridge between the Python and Objective-C programming languages.
Okay, it turned out that, amending mjv's answer, I was able to get it working by typing
export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/PyObjC/"
before executing the ./project-tool.py line. I still find it ridiculous that I had to do this and if anyone can see why, I would be delighted to know.
Doing this also got the
>>> import objc
line working.
It's because PyObjC is there :
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC
Edit :
I found how to make "import objc" work, just :
export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/"
It will add all the directories to the python path (sys.path)
for python 2.7
export PYTHONPATH="/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/"
One of two things:
Either the Fundation module doesn't exists
Or Python interpretor doesn't know when to find this file
Python looks for modules in the PythonPath
See this SO question for more details on how Python Path is created etc.
Run python -v to trace import statements, this work for interactive mode too.
I could access a Python installation with Foundation on my OSX by running /usr/bin/python file-to-run.py
remove your python or remove site-packages/Foundation | site-packages/foundation
pip3 install pyobjc
the name Foundation is in conflict with https://pypi.org/project/foundation/
I found the foundation folder in /usr/local/lib/python3.9/site-packages/ next to the AppKit folder. After renaming it to Foundation (with uppercase F), the import worked. The Filesystem is not case-sensitive but it seems some part of the import implementation is.
Saw it mentioned in another comment and I too ran into this problem due to installing Python via homebrew. My pyobjc installation wound up going to the Python homebrew installation, yet my pythonpath was linked to the Python that comes bundled with macOS, so there was this big disconnect and I had no luck getting pythonpath re-routed in .zshrc or .zprofile.
In the end, these steps resolved the issue:
brew uninstall python
pip3 install -U pyobjc

Categories