Reputation: 37
This is somethings that has been bothering me since I started using PyCharm to program in Python. I have a two .py files, in the same directory, main.py
and external.py
. Inside main.py
, I have import external
at the top. PyCharm marks this as an error, but it runs fine both in the new zsh MacOS terminal and PyCharm itself, and I can use all the things declared in external.py
as expected.
I've played around with it a bit, and (to my very limited knowledge) it seems that PyCharm detects imports like Python2. Thats a guess, though, as I am unfamiliar with that version.
Why does PyCharm do this, or am I the one to blame? If it's not my fault, how can I fix it?
My file structure is as follows:
Project-|
|-external.py
|-main.py
I want to use things from external.py
in main.py
, and I can, but PyCharm gives it a red underline.
Upvotes: 1
Views: 124
Reputation: 7639
Please try to mark directory containing your python files as Sources Root
, see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
Upvotes: 0
Reputation: 56
From given description, it correctly imported your external module.
- Did you create a folder inside your project folder?
When using subfolders for your main.py/external.py files, Pycharm might not by default correctly detect your import statement.
- Pycharm should give you an error message for said import statement.
Maybe the error is not connected to the import statement but to your pycharm setup e.g. correctly setting up your python interpreter.
If you provide more information regarding your folder structure or the error message, that might help.
Upvotes: 1