I'm using PyCharm and I'm importing some constants from another python file in the same directory. The import works at runtime, but I get this annoying red underline on the import statement and also every time I use a constant from the file.
Here's the file hierarchy
(Please ignore the red underlining on the folders, they are unrelated to this)
What causes this behaviour and how can I fix it?
Select your working folder and mark it as Sources. Then Pycharm recognize the working folder as a Source folder for the project and you will be able to simply add other files within that folder by using In Pycharm you can simply add . before the .py file which you are going to import it from the same folder. In your case it will be
Select Optimize imports. To optimize imports in a file, you can also press Ctrl+Alt+Shift+L, select Optimize imports, and click Run. If your project is under version control, you can instruct PyCharm to optimize imports in modified files before committing them to VCS. Press Ctrl+K or select Git | Commit from the main menu.
The issue is because of how PyCharm sets PYTHONPATH to the root of your project. While you can add directories as Sources, I would not recommended. That will make your project fragile. You can right click pca_mixtures and then mark the directory as Sources Root, this should solve your problem.
This doesn't cause issues when running, say, pca_mixtures/funcs.py directly, though. The issue is because of how PyCharm sets PYTHONPATH to the root of your project. While you can add directories as Sources, I would not recommended. That will make your project fragile.
It is better if you replace that with:
from pca_mixtures.constants import *
The issue is because of how PyCharm sets PYTHONPATH to the root of your project.
While you can add directories as Sources
, I would not recommended. That will make your project fragile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With