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

Importing from PWD using python -m fails in development environment #2817

Closed
jwg4 opened this issue Jun 6, 2018 · 5 comments
Closed

Importing from PWD using python -m fails in development environment #2817

jwg4 opened this issue Jun 6, 2018 · 5 comments

Comments

@jwg4
Copy link

jwg4 commented Jun 6, 2018

Expected Behavior

We have a Flask app defined in a file which sits inside a folder, say app/run.py:

from flask import Flask

from foo import bar

app = Flask(__name__)

app.run()

The file imports from the top-level directory, in this case foo.py

bar = "SOMETHING TO IMPORT"

The empty file __init__.py exists in the directory app.

Using the default environment (production), we can run the Flask app by invoking the module from the top-level directory, using -m

python -m app.run

The foo module is imported correctly and the app runs.

 * Serving Flask app "run" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Actual Behavior

If we try to use the development environment, setting the FLASK_ENV variable, it fails.

FLASK_ENV=development python -m app.run
 * Serving Flask app "run" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
Traceback (most recent call last):
  File "/home/jack/z/flask_dev/app/run.py", line 3, in <module>
    from foo import bar
ImportError: No module named foo

Environment

  • Python version:
    Python 2.7.12 (default, Nov 20 2017, 18:23:56)
    [GCC 5.4.0 20160609] on linux2

  • package versions:
    click==6.7
    Flask==1.0.2
    itsdangerous==0.24
    Jinja2==2.10
    MarkupSafe==1.0
    Werkzeug==0.14.1

@lepture
Copy link
Contributor

lepture commented Jun 7, 2018

Close it since it is not a bug of Flask.

It seems a misunderstanding of Python PATH. I don't know where you put your foo.py according to your description. It is better to ask your question on StackOverflow with a minimal example.

@lepture lepture closed this as completed Jun 7, 2018
@jwg4
Copy link
Author

jwg4 commented Jun 7, 2018

With all due respect, it seems like you haven't understood the issue.

I do understand how PATH works, and I did say where foo.py is located.

I also said that starting the Flask server this way works for the production environment, but not for the development environment. So clearly, this is related to Flask rather than be a general issue of Python imports. But to be absolutely clear, if you don't have the call to app.run() in the file run.py, running a script in this way works perfectly well, with the module being imported correctly.

I would be grateful if you could look at this issue again and try to fully understand what is reported about the behavior of the Flask development server.

@lepture
Copy link
Contributor

lepture commented Jun 7, 2018

The file imports from the top-level directory, in this case foo.py

I'm sorry, but I don't fully understand your top-level. Is it /home/jack/z/flask_dev/app/foo.py or /home/jack/z/flask_dev/foo.py? I can only guess.

@lepture lepture reopened this Jun 7, 2018
@lepture
Copy link
Contributor

lepture commented Jun 7, 2018

It is a werkzeug reloader issue.

Maybe related to pallets/werkzeug#461

@jwg4
Copy link
Author

jwg4 commented Jun 7, 2018

Thanks! Trying to understand exactly what that issue and #1246 imply for this.

@lepture lepture closed this as completed Jun 7, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants