2

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.

9
  • only import statements are highlighted or python syntax too? Commented Apr 11, 2017 at 16:59
  • Python syntax too. So if I use a function from helper_functions that's highlighted in red as well Commented Apr 11, 2017 at 17:00
  • what interpreter is chosen in PyCharm settings? Commented Apr 11, 2017 at 17:14
  • 3.6.0 (~/anaconda/bin/python). This may be the reason. I just upgraded to python 3 over python 2 (I used to have 2.7.12 at the same location) Commented Apr 11, 2017 at 17:18
  • with old version everything works fine? Commented Apr 11, 2017 at 17:24

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.