Description
This is my file structure:
proj_root
/rest_api/main.py
$ cd rest_api
$ uvicorn rest_api:APP --reload
results in one of the files
ModuleNotFoundError: No module named xxxx
But when Right-Click rest_api.py in Pycharm it runs ok:
INFO: Started server process [14928]
INFO: Waiting for application startup.
INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL-C to quit)
Additional context
Add any other context or screenshots about the feature request here.
Without a better description it's impossible to help
Le lun. 23 sept. 2019 Ã 7:14 PM, Sebastian Cheung CQF <
[email protected]> a écrit :
Description
This is my file structure:
proj_root
/rest_api/main.pycd rest_api
uvicorn rest_api:APP --reload
results in one of the files
ModuleNotFoundError: No module named xxxx
But when Right-Click rest_api.py in Pycharm it runs ok?
Additional context
Add any other context or screenshots about the feature request here.—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/560?email_source=notifications&email_token=AAINSPU74LBQNCYGVZUMY3TQLD2QVA5CNFSM4IZNQDK2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HNDA53Q,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAINSPXJUW3G4KRRAVKEVSLQLD2QVANCNFSM4IZNQDKQ
.
PyCharm has some capabilities to automatically activate the appropriate virtual environment, and adding appropriate paths to the PYTHONPATH. Try checking the value of sys.path (and sys.executable) when run in pycharm, and when run outside pycharm. They probably differ, and that would explain why the module is found in one case and not in the other.
Given the number of similar related questions this superb tutorial https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html# should be added to the FAQ once there is one,!
Hi @dmontagu, in a way they are both ran within the PyCharm context
Right Click on main.py within PyCharm runs ok.
But in venv terminal also from PyCharm:
cd rest_api
main.py:
from mymodel import GlobaldB
app = FastAPI()
$uvicorn main:app --reload
ModuleNotFoundError: No module named mymodel
I think you'll get better support on import issues on stackoverflow, this is in no way related to FastAPI but it's a general python issue. the link provided above should help too.
Yes, pycharm will configure the path when you run, but not in the terminal. You need to set up your import path, or change how you are organizing/referencing your package-internal files. The links @euri10 posted should be helpful for learning about how this stuff works.
The only variable I find within RUN config of PyCharm is:
PYTHONUNBUFFERED:1
Add content roots to PYTHONPATH: checked
Add source roots to PYTHONPATH: checked
If you do:
cd rest_api
You would have to do:
uvicorn main:app
See more in the docs here: https://fastapi.tiangolo.com/tutorial/first-steps/
And here: https://fastapi.tiangolo.com/tutorial/bigger-applications/
Hi @scheung38.
This may help you.
cd rest_api
export PYTHONPATH=$PWD
uvicorn main:app --reload
The only variable I find within RUN config of PyCharm is:
PYTHONUNBUFFERED:1
Add content roots to PYTHONPATH: checked
Add source roots to PYTHONPATH: checked
@scheung38 were you able to solve your problem? If so, you can close the issue.
Description
This is my file structure:
proj_root
/rest_api/main.py$ cd rest_api
$ uvicorn rest_api:APP --reload
results in one of the files
ModuleNotFoundError: No module named xxxx
But when Right-Click rest_api.py in Pycharm it runs ok:
INFO: Started server process [14928]
INFO: Waiting for application startup.
INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL-C to quit)Additional context
Add any other context or screenshots about the feature request here.
I'm having a similar problem ... did you solve it?
@danielahdez that's probably related to the Python environment you are using and where your files are being installed.
This post has some useful info on handling environments: https://snarky.ca/a-quick-and-dirty-guide-on-how-to-install-packages-for-python/
If after that you still have problems, please create a new issue so we can help you with your specific problem.
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.