ModuleNotFoundError - No module named 'main' when attempting to start service

后端 未结 4 550
梦毁少年i
梦毁少年i 2020-12-11 15:21

Context:

  • I created a Django application using Python 3.7.
  • I am (attempting) to use the 2nd generation Google App Engine standard environment.
相关标签:
4条回答
  • 2020-12-11 15:32

    As Farzad has said, just rename 'app.py' or 'application.py' or whatever name you have saved the application file to 'main.py'. If you can't change then create another project with the same code but this time with application file names as 'main.py'

    0 讨论(0)
  • 2020-12-11 15:47

    Simply rename your main python app (for me it was app.py) to main.py. Google Cloud requires main.py to begin the process.

    0 讨论(0)
  • 2020-12-11 15:53

    By default, App Engine looks for an app variable in a file called main.py. You have two options: put your WSGI app where App Engine expects it to be, or define a custom entrypoint:

    Put your WSGI app where App Engine expects it to be:

    You can create a file called main.py that has an app variable which is simply imported and aliased from the correct location:

    from demosite.wsgi import main as app
    

    Adding a custom entrypoint:

    From https://cloud.google.com/appengine/docs/standard/python3/config/appref:

    entrypoint: Optional. The command that is executed when your app starts. For your app to receive HTTP requests, entrypoint should contain a command which starts a web server that listens on the port specified by the PORT environment variable. If you do not specify an entrypoint, App Engine will configure and start the Gunicorn webserver.

    By default it's this:

    entrypoint: gunicorn -b :$PORT main:app
    

    You would need something like:

    entrypoint: gunicorn -b :$PORT demosite.wsgi:main
    

    See here for more details about application startup: https://cloud.google.com/appengine/docs/standard/python3/runtime#application_startup

    0 讨论(0)
  • 2020-12-11 15:55

    Adding:

    The main.py have to be in root of your application, where app.yaml is.

    And the content, can to be, as well:

       from mysite.wsgi import application
    
       # App Engine by default looks for a main.py file at the root of the app
       # directory with a WSGI-compatible object called app.
       # This file imports the WSGI-compatible object of your Django app,
       # application from mysite/wsgi.py and renames it app so it is discoverable by
       # App Engine without additional configuration.
       # Alternatively, you can add a custom entrypoint field in your app.yaml:
       # entrypoint: gunicorn -b :$PORT mysite.wsgi
       app = application
    
    0 讨论(0)
提交回复
热议问题