Or Smith
Or Smith

Reputation: 3606

Python: ImportError while __init__.py exists

I have the file: run.py in C:/mycp/applicationl directory, which contains the following code:

from che import wserver
from applicationl import app

The directory: C:/mycp/applicationl also contains the __init__.py file.

When I run python run.py, It gives me the following error:

  File "run.py", line 8, in <module>
    from application1 import app
ImportError: No module named 'application1'

But __init__.py exists, and app exists in this file:

app = Flask(__name__, static_url_path='')

What did I do wrong?

Upvotes: 0

Views: 1069

Answers (4)

jersey bean
jersey bean

Reputation: 3639

Try adding 'C:/mycp/application1' to the environment variable PYTHONPATH

Upvotes: 0

Oliver
Oliver

Reputation: 29493

Try from . import app since app.py is in same folder as run.py

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121914

You should not run file as a script and expect the directory to be treated as a package.

For applicationl to be treated as a package, the parent directory has to be added to the Python sys.path module lookup path. But when you run a script, the directory itself is added instead.

It is not a good idea to manually add the C:/mycp directory to sys.path. Just import other Python code as top-level modules here instead, or create a subdirectory of C:/mycp/applicationl and use that as a package instead.

Without creating additional packages, your run.py code should change to:

from che import wserver
from app import app

and rename __init__.py to app.py.

Alternatively, create a new subdirectory; it can even be named applicationl again, and move __init__ into that subdirectory.

Upvotes: 3

Zulu
Zulu

Reputation: 9275

If you are inside C:/mycp/applicationlyou can't import from application1 if it isn't in your sys.path.

Upvotes: 0

Related Questions