0

I get the following error when trying to import from src package:

client.py

from . import create_app
from . models import db
from . models import Monitoring


app = create_app() 

init.py

from flask import Flask
from . models import db
    
app = Flask(__name__)

def create_app():
    db.init_app(app)
    return app


from src import routes

Project tree

.
├── manage.py
├── sf.py
├── src
│   ├── client.py
│   ├── __init__.py
│   ├── models.py
│   └── routes.py
└── tests
    ├── __init__.py
    ├── test.py

Error message:

(sensor_flora) edx@edx-HP-250-G3-Notebook-PC:~/VisualStudio/SensorFlora$ python src/client.py 
Traceback (most recent call last):
  File "src/client.py", line 4, in <module>
    from . import create_app
ImportError: attempted relative import with no known parent package

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • How are you running your script? – Iron Fist Feb 03 '21 at 16:49
  • Also, there should not be any space between `.` and `models` in `from . models import db` and `from . models import Monitoring` – Iron Fist Feb 03 '21 at 16:51
  • I run code with python src/client.py (through pyenv virtual environment) I've edited my code with .models but doesn't make difference, I've got still error. If I move clients.py at top level and then from src import create_app it works. But when I try to import it inside src I've got the error. – Edoardo Zucchelli Feb 03 '21 at 16:57
  • My comment on `.models` instead of `. models` was not the cause of your issue, but to avoid others, as it's wrong syntax – Iron Fist Feb 03 '21 at 17:05
  • 1
    By doing `python src/client.py`, are you trying to test `client.py` only? – Iron Fist Feb 03 '21 at 17:06
  • Does this answer your question? [Relative imports in Python 3](https://stackoverflow.com/questions/16981921/relative-imports-in-python-3) – Jürgen Gmach Feb 03 '21 at 17:17
  • Fix `.models` and run `client.py` from inside `src` folder not outside it as `~$ python client.py` this should fix your issue if you are only testing `client.py` file – Iron Fist Feb 03 '21 at 17:32
  • 1
    For me your code works (when I comment out those imports which cannot work as you did not provide all code). But do not get the error you got. Maybe that is because I did a `FLASK_APP=src` before running `flask run`. I am not a big fan of relative imports as they make everything more complicated, see my link above. Also, when you are planning to restructure your project, I recommend the following links to read https://hynek.me/articles/testing-packaging/ https://web.archive.org/web/20170615032800/https://enotuniq.org/ https://blog.ganssle.io/articles/2019/08/test-as-installed.html – Jürgen Gmach Feb 03 '21 at 17:48
  • Please note, the mentioned "src" directory in the mentioned blog articles do not correspond to your "src" directory - you miss a package inbetween. – Jürgen Gmach Feb 03 '21 at 17:49

1 Answers1

0

Thank you guys for your help, I've appreciated it.

SOLUTION:

The problem was that the client.py script it's not executed by flask which has it's own path. In fact, what I did is simply add a proper python path, at the top level (project) like so:

export PYTHONPATH="/home/usr/VisualStudio/MyProject"

Then I've changed all imports to absolute imports.

Finally I ran with python src/clients.py and it works.

TODO: As client.py is a "standalone" script, nothing to do with the flask app, apart it uses the app like a package to import database and it's configurations, I though it was better to move the client.py in a separate package (because I need to run the script along the flask app).