Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load submodule #18

Closed
YTed opened this issue Sep 1, 2017 · 7 comments
Closed

Cannot load submodule #18

YTed opened this issue Sep 1, 2017 · 7 comments

Comments

@YTed
Copy link

YTed commented Sep 1, 2017

I have a project with structure below:

- /
  |- src
    |- main.py
    |- __init__.py
    |- submodule
      |- __init__.py
      |- ... 
    |- web
      |- web.py
      |- __init__.py
  |- Dockerfile

main.py calls entry function defined in web.py, and web.py calls functions defined in submodule. It works fine with command line python main.py. Then I plan to deploy it under docker, with Dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.6
COPY ./src/* /app/

Build and run, I got error:

Traceback (most recent call last):
  File "./main.py", line 1, in <module>
    from web import run
  File "./web.py", line 5, in <module>
    import submodule
ModuleNotFoundError: No module named 'submodule'

Why did uwsgi cannot find submodule? Did I miss something?

@tiangolo
Copy link
Owner

tiangolo commented Sep 1, 2017

It probably depends on how you are importing that submodule in main.

When you run python main.py, you are running it "as a script" that imports modules. But uWSGI doesn't run it as a script, it runs it "as a module". And as your code is in a Python package (you have a __init__.py in your top level) for uWSGI your main.py will be a "package module" (which is what you have in your code).

That changes the way you can import relative and absolute modules and how you structure them.

If you share your code with me or something with the same structure that replicates the bug, maybe I can help you debug it better.


I have a very similar project, and there are a couple things you can do to be able to run it directly with python main.py when developing and still have uWSGI be able to use your code.

In my case, I have the equivalent of src/app/app/main.py instead of src/app/app/main.py.

And in src/app/uwsgi.ini is a file with:

[uwsgi]
module = app.main
callable = app

@YTed
Copy link
Author

YTed commented Sep 2, 2017

I am new to python, module loading mechanism in python confused me a lot.

My project have more files and dependencies than I have descriped before, so I built another simpler project to make this issue reappear, located in my github repository.

Branch master is the project described above.
Branch uwsgi-config moved source files to src/app, added src/uwsgi.init, still not work, response error no python application found.

Thanks for your help.

@frankjdelgado
Copy link

@YTed Try with from submodule.hello import say_hello

@YTed
Copy link
Author

YTed commented Sep 2, 2017

@frankjdelgado Same error with your solution.

IMHO, this issue is caused by wrong module path configuration, changing import statement in invocation file helps nothing.

@frankjdelgado
Copy link

frankjdelgado commented Sep 2, 2017

@YTed I dowloaded your repo, made the following changes and it worked:

  • import submodule to from hello import say_hello

  • greeting = submodule.say_hello() to greeting = say_hello()

  • Deleted the __init__.py file at the top level

  • The uwsgi.ini file looks like this:

[uwsgi]
module = web
callable = app

I couldn't find a way to make it work with the main.py because i'm a python noob but i hope this helps!

@tiangolo
Copy link
Owner

tiangolo commented Sep 2, 2017

Thanks @frankjdelgado for the help!

So, @YTed , your problem inspired me to add more specific documentation for those cases.

I just created a new release with an example project using a Python package structure (like you). You can download the example project here.

I also updated the README docs on how to use the flask run commands, that would be the equivalent of python main.py but will work even if you have a package structure.

...and as an extra, I'm creating a pull request to your project, so you can see exactly what I changed to make it work. 🎉

Let me know if you still have problems after that, or otherwise, you can close this issue.

@YTed
Copy link
Author

YTed commented Sep 4, 2017

Thanks @tiangolo and @frankjdelgado, it finally works.

@YTed YTed closed this as completed Sep 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants