main.py throws "ImportError: No module named app" - python

I have a file in the root directory of my app called main.py. This file contains the code which starts up the app:
app = webapp2.WSGIApplication(routes = routes, debug = True, config = config)
def main():
app.run()
if __name__ == '__main__':
main()
I have, amongst others, this in app.yaml:
- url: /.*
script: main.app
After upgrading to the 1.6.6 SDK, I get the following error:
ERROR 2012-06-05 13:39:01,856 wsgi.py:189]
Traceback (most recent call last):
File "/Users/jdoe/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 187, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Users/jdoe/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 236, in _LoadHandler
__import__(cumulative_path)
ImportError: No module named app
If I rename main.py to anything else than main.py and update app.yaml accordingly, this error disappears, and I can run the app.
Why can I no longer have my main routines in a file called main.py? On the App Engine SDK download page they say they're now including an experimental version of Python 2.7 in the SDK, could this have something to do with the issue I'm experiencing?

My comment as an answer:
Solved this myself. In the same dir where main.py resides I had created a package also called main. In app.yaml, I'd specified to look in main.app. Seems that if a package called main exists in the same dir as main.py, Python goes to look inside the package for a module called app. But if no package named main exists, it happily looks in main.py.

Related

ModuleNotFoundError: No module named 'mysite.polls' in django tutorial while testing

I am coding along tutorial from django documentation site: https://docs.djangoproject.com/en/3.1/intro/tutorial05/
I got a following problem:
While trying to run test on my app (with: py manage.py test polls in Pycharm commndline)
i get following error:
> ImportError: Failed to import test module: mysite.polls Traceback
> (most recent call last): File
> "C:\Users\micha\Anaconda3\envs\DjangoTutorial\lib\unittest\loader.py",
> line 470, in _find_test_path
> package = self._get_module_from_name(name) File "C:\Users\micha\Anaconda3\envs\DjangoTutorial\lib\unittest\loader.py",
> line 377, in _get_module_from_name
> __import__(name) ModuleNotFoundError: No module named 'mysite.polls'
The file structre of my project is as follwoing: link to the directory tree img
It looks like testing unit automaticcaly adds 'mysite' in front of 'polls'
What i tried:
I have tried to lookup for an answer in django test documentation.
Only solution that i was able to find was to change command to like like this:
py manage.py test polls.tests
Then it runs my test properely.
As far as I understand django documentation, django should automatically find every 'test*' file in polls yet in case of mine it cannot find 'polls' module.
Can you tell me why it happens?
Have you included your polls app in your settings file?
INSTALLED_APPS?
From django documentation : "Test discovery is based on the unittest module’s built-in test discovery. By default, this will discover tests in any file named “test*.py” under the current working directory."
It says current working directory ie. where the manage.py is, your test are located in the polls folder that is why polls.test is required.
You can also provide a path to a directory to discover tests below that directory: ./manage.py test polls/

Won't import local file as module

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?
What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

How can I permanently add a module to my flask app?

yes I know my question sounds like a duplicate but I have tried everything I have found.
I am trying to add a module to my python flask app. This module is located at ../../clients/api relative to my flask app. I haven't had any problems adding a module before, but for some reason this just doesn't want to work.
So far, I have tried:
sys.path.append("mypath") (worked temporarily but as soon as the shell was closed it disappeared)
adding a .pth file in lib/site-packages
adding it directly to my environment variables
adding in in my IDE (Wing 101)
Aside from 1., none of them had any effect whatsoever.
$ flask run
* Serving Flask app "main.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "main", an ImportError was raised:
Traceback (most recent call last):
File "c:\python37-32\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "my_flask_app_path", line 20, in <module>
from clients.api import Client as client
ModuleNotFoundError: No module named 'api'
Running this on python v-3.7.1, Windows 10, both git bash and cmd. Please try not to be blunt :)

Python ImportError: How to resolve "No module named main" in Google appengine project?

I have setup a google appengine project in my ubuntu system for the first time and made everything works till now by reading all the documentation available online. But here I have stuck, it is running properly on localhost using command:dev_appserver.py app.yaml but when I try to access my app on my browser, it throws the error: ImportError: No module named main
Project Structure
-GAE_Demo
-google_appengine
-src
-handlers.py
-main.py
-app.yaml
-config.json
app.yaml
url: /.*
script: main.app
secure: always
src/main.py
app = webapp2.WSGIApplication(
routes=([('/main', MainHandler)]),
debug=True)
Error
INFO 2017-10-17 01:32:58,464 module.py:821] default: "GET / HTTP/1.1" 500 -
ERROR 2017-10-17 01:56:21,976 wsgi.py:263]
Traceback (most recent call last):
File "/home/path/to/project/GAE_Demo/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/path/to/project/GAE_Demo/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/path/to/project/GAE_Demo/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
ImportError: No module named main
Thanks in Advance !!
First, going off-script on your first App Engine project (by putting .py files in src/, which none of the tutorials I know of do), is setting you up for frustrations that are better avoided until you have a successful project under your belt.
The easy way forward is to move the files in src/ up a level. But if you really, really want to stick with src/, then try adding an __init__.py to src/, and change your app.yaml to
url: /.*
script: src.main.app
secure: always

django_settings_module is undefined on GAE server

So, I know you guys have been asked this question a million times, believe me I think I have read them all. I am getting the DJANGO_SETTINGS_MODULE is undefined error when trying to convert the Django polls app (the one made in the django tutorial) to run on the google app engine using the google cloud SQL.
Here is the actual stack trace given to me by the google app engine logs:
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 223, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/wsgi.py", line 219, in __call__
self.load_middleware()
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/utils/functional.py", line 184, in inner
self._setup()
File "/base/python27_runtime/python27_lib/versions/third_party/django-1.4/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I was getting the same error when trying to run it on dev_appserver.py provided with the google app engine but managed to remedy that by adding
env_variables:
DJANGO_SETTINGS_MODULE: 'DjangoTut.settings'
to the app.yaml file.
my project directory looks like this:
DjangoTut
DjangoTut
init.py
settings.py
urls.py
wsgi.py
polls
init.py
admin.py
models.py
tests.py
urls.py
views.py
app.yaml
manage.py
and my app.yaml file looks like this
application: hochotime
version: 10
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.4"
builtins:
- django_wsgi: on
env_variables:
DJANGO_SETTINGS_MODULE: 'settings'
every other file is identical to those suggested here https://docs.djangoproject.com/en/1.4/intro/tutorial01/
I have tried adding
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'DjangoTut.settings'
to the start of just about every single python file but to no avail. I have tried moving the settings file into the project directory rather than the app directory and changing all the os.environ['DJANGO_SETTINGS_MODULE'] to point to just 'settings'. I have also tried configuring the settings directly as shown here https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-without-setting-django-settings-module but still, I get the same error whenever I try to run it on the google servers.
Any help would be greatly appreciated.
Are you using Django-nonrel? If not, take a look at this appengine ariticle.
I would try moving settings.py out to the same directory as app.yaml. The remove the env_variables from app.yaml, you shouldn't need those for it to work. However you will need handlers defined if you don't already (not sure if you listed your whole app.yaml or not):
handlers:
- url: /static
static_dir: static
- url: /.*
script: main.py
The environment setting should be in the wsgi handler you setup (main.py in this example) as shown in the google article above.
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
Hope this gets you going down the right path.

Categories