Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
177 views
in Technique[技术] by (71.8m points)

python - Why manage.py can't see the __init__ file?

I'm just making a blog app with this structure:

blog/
    __init__.py
    main.py
    manage.py
    config.py
    tests.py
    __pycache__
    ...

When dev server is running I run also manage.py:

 import unittest, sys
 from flask.cli import FlaskGroup
 from blog import create_app

what I don't understand is why I'm getting error on the third line no module named blog. Almost as if it's not picking up on __init__.py being present.

Edit:

 import unittest, sys
 from flask.cli import FlaskGroup
 from . import create_app

structure:

├── blog
│?? ├── client
│?? ├── config.py
│?? ├── __init__.py
│?? ├── main.py
│?? ├── manage.py
│?? ├── __pycache__
│?? ├── templates
│?? ├── tests
│?? └── venv

output from manage.py:

(venv) mark@python:~/blog$ python manage.py test
Traceback (most recent call last):
  File "manage.py", line 3, in <module>
    from . import create_app
ImportError: attempted relative import with no known parent package

Another edit:

I went for another structure:

── my_blog
│?? ├── project
│?? │?? ├── __init__.py
│?? │?? └── main.py
│?? ├── tests
│?? │?? ├── __init__.py
│?? │?? └── main_test.py
│?? └── venv
│??     ├── bin
│??     ├── lib
│??     └── pyvenv.cfg


mark@python:~/my_blog/tests$ python3 main_test.py
Traceback (most recent call last):
  File "main_test.py", line 1, in <module>
    from project.main import app
ModuleNotFoundError: No module named 'project'
question from:https://stackoverflow.com/questions/65852495/why-manage-py-cant-see-the-init-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Adding parent directory to PYTHONPATH solved the problem. Thank you all for pointers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...