Say, my project structure is as following:
project_folder:
__init__.py
another_folder:
__init__.py
main_file.py
other_file.py
And main_file.py
contains next code:
import other_file as whatever
PyCharm (PEP8) highlights this line as an error 'no module named other_file
' and there is no autocomplete on any calls like whatever.this_should_be_autoc_suggested
. Meanwhile interpreter works perfectly fine.
What can be the cause of this annoying behaviour?
Troubleshooting: Try installing/importing a package from the system terminal (outside of PyCharm) using the same interpreter/environment. In case you are using a virtualenv/conda environment as your Project Interpreter in PyCharm, it is enough to activate that environment in the system terminal and then do the test.
We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.
Type the name of the package and hit Alt-Enter , then choose Install and Import package . PyCharm will do both: you'll see a notification during the installation, then the import will be generated in the right way, according to your project styles.
You need to mark your project directory as "Sources Root", just right click upon the directory in pycharm, Mark Directory as, Sources Root. that should make the trick for you.
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