Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm import does not recognize other files in the same module

Tags:

import

pycharm

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?

like image 308
y.selivonchyk Avatar asked Aug 14 '16 12:08

y.selivonchyk


People also ask

Why import is not working in PyCharm?

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.

How can I import modules if file is not in same directory?

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.

How import all packages in PyCharm?

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.


1 Answers

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.

like image 157
Jean Guzman Avatar answered Sep 21 '22 03:09

Jean Guzman