Johnny Metz
Johnny Metz

Reputation: 5965

pycharm: not recognizing local python files

I have the following files in a project folder (there are more; just listing what's relevant):

repo
|--> flask_app
    |--> app.py
    |--> helper_functions.py

If I try to import helper_functions.py inside of app.py the former is highlighted in red (however it does work when I run the script).

app.py:

import pandas  # works, no red highlight
import helper_functions  # works, red hightlight

Not sure what the problem is here. I realize it's not a huge deal because everything works fine; however, the red highlight is very distracting and it's hard to distinct true errors from this PyCharm glitch. I've tried this solution but no luck.

Upvotes: 2

Views: 2697

Answers (3)

zzfima
zzfima

Reputation: 1565

in VScode if is local file, but other folder opened, it's not recognize it

Upvotes: 0

Johnny Metz
Johnny Metz

Reputation: 5965

Figured this out. Because I'm working in a directory that isn't a direct child of the repository (e.g. it's a directory inside a child directory), I need to mark it (flask_app in this case) as a Sources Folder. Which tells Pycharm to look for imports when executing code from this folder.

To do this, simply go to Preferences > Project Structure > Click on the folder > Click on Mark as: SOURCES.

The red highlight disappears and all is well.

Upvotes: 3

Azat Ibrakov
Azat Ibrakov

Reputation: 10963

You should have __init__.py file (may be empty) inside of your package, this helps to both Python and PyCharm to understand that you are working inside of Python package, not regular directory, more

Also it helps to mark your package as Source Root, more info about it can be found here

Upvotes: 1

Related Questions