0
votes

I am building a web application with Python and Flask, the Flask part does not have anything to do with my question.

What I am struggling with is Python import's. I have discovered the following: I can only import files as a module when it is in the same directory, when I am in a sub-directory I can import files from outside the directory, but not in reverse. I have a directory structure like this:

Project
   |-src
      |- templates
      |- utils
      |- blueprints
      |- app.py

The main problem I am facing is with the difference when I am running it from my IDE PyCharm or from commandline.

I am importing files the following way import src.utils.distanceConverter it works fine from my IDE, but as soon as I run it from command line I get the following import error:

ModuleNotFoundError: No module named 'src'

So I removed the 'src' part and it works fine, from both command line as from PyCharm. Still the weird thing is that PyCharm keeps complaining that module utils does not exists.

So besides from that this is not really an issue, it is the fact that the import system from PyCharm is not consistant with that from Python itself. So is there a way to tell Python that this is not an issue and what is causing this problem. I think it is somewhere in my IDE configurations, but I am new to PyCharm since I am used to coding Python from VScode.

1
You missed to add an __init__.py. And of cause Project has to be in your PYTHONPATH. - Klaus D.
That seems to be the issue - Harry Stylesheet

1 Answers

0
votes

You missed to add an __init__.py And of cause Project has to be in your PYTHONPATH

CREDITS TO: Klaus D