V
V
Vladislav2020-06-02 19:37:53
Flask
Vladislav, 2020-06-02 19:37:53

Why doesn't python see the module even though __init__.py is there?

Project structure (Flask):

blog:
    client:
         *files
    server:
      blog_app:
             __init__.py
             *files
      test:
            test1.py

It is necessary from under test1.py to refer to the app variable set in __init__.py, according to the documentation
In test1.py it is written: __init__.py should define blog_app as a module, but this does not happen. __init__.py contains:
from blog_app import app
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

from .config import Config

app = Flask(__name__)
app.config.from_object(Config)
app.debug = True
db = SQLAlchemy(app)
migrate = Migrate(app, db)

from blog_app import main, models

The result is an error:
ModuleNotFoundError: No module named 'blog_app'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-06-02
@nitron_5

Python just doesn't know that the server directory (and for test1.py is "..") can be searched for modules and packages.
Do this:
sys.path.append('..') in test1.py and it will be possible to import your package in it if you run it directly (test1.py)
When importing, Python looks for modules in the paths listed in sys.path .
One solution for you is to make a normal setup.py and then install it in editable mode (pip install -e .).
So the folder where the package is located will be added to sys.path and it will be imported from anywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question