11

Pycharm doesn't recognize the packages of my project's modules.

enter image description here

I noticed there are already quite some questions on this, but I tried all the given solutions and nothing works.

I tried:
- Mark directory as source root & checking Add sources root to pythonpath
- Opening a different project and then reopening the original one to make pycharm reindex the packages

Any suggestions?

I went over the following questions
PyCharm does not recognize modules installed in development mode
Pycharm not recognizing packages even when __init__.py exits

Kemeia
  • 826
  • 2
  • 9
  • 24

4 Answers4

11

Just in case people got here with a similar trouble as I am. I had all files __init__.py correctly but pycharm didn't recognize those package properly. Finally, this following way worked for me:

In pycharm, 

1. right click on a base folder of your code
2. Choose "make Directory as"
3. Choose "Sources Root"

enter image description here

Hope this helps.

sonvx
  • 411
  • 4
  • 7
  • 3
    I couldn't find the "make Directory as" option in 2021.1 version of PyCharm. Removing .idea folder worked though. After I removed the .idea folder I reopened PyCharm and voila! My project folder was there listed as it used to be. – Can Apr 14 '21 at 21:02
  • @Can that was a great solution, working in my case (also 2021.1). Strangely, it happened right after a PyCharm update. – ggrelet Apr 26 '21 at 13:21
10

Ok it is quite funny to answer my own question, but I understood that is the encouraged way to go if you actually found the answer.

So what was causing this problem was that __init__.py was listed as a text file in Pycharm settings.

If you go to Settings | File Types | Select Text you see a list of file names/extensions that Pycharm recognizes as text.

enter image description here

I think this happened because I created a file named __init__ without file extension, and later changed it to __init__.py

Removing it from there fixed the issue.

Kemeia
  • 826
  • 2
  • 9
  • 24
  • While this didn't work exactly the same for me, it did give me a hint. I originally created my __init__.py file by right clicking and creating a new text document, then renaming the empty file to __init__.py . I deleted the file, created a new __init__.py from within PyCharm. Problem solved! – jgrant May 04 '18 at 12:38
  • 1
    Thx. This solved my problem. File type association can so easily go wrong and Pycharm is just too lazy to give an obvious hint. – ShellayLee May 18 '18 at 16:43
  • Unfortunately, your solution doesn't work for me. My `__init__.py` can be recognized by the pycharm. – xxks-kkk Jul 03 '18 at 16:31
  • You saved my life! Worked for me! – Temak Oct 05 '18 at 22:46
  • Amazing! My `__init__` was accidentally set to `Automatically Recognize` in the settings you described. Thank you so much for sharing, that's a tough debug! – nikojpapa Feb 12 '21 at 19:35
5

I would suggest the following

  • Close pycharm
  • Delete the .idea folder
  • Open the project as new project
  • Make sure you have the __init__.py files where they belong (each package needs one)

It should not be necessary in this case to add anything to the pythonpath or source root manually. This is only necessary if you work on two projects with are disconnected from each other and you want them to be able to import from each other.

Nickpick
  • 6,163
  • 16
  • 65
  • 116
  • It didnt work :( Though I'm not entirely sure what you meant by 'open as new project'. I 1. closed pycharm 2. removed .idea folder 3. openend pycharm 4. chose my project from the list of option given 5. did again 'mark as directory as sources root' – Kemeia Oct 24 '17 at 09:08
  • no, the project should no longer be on the list. Try to remove it from there as well and then just FILE->OPEN the folder where your project is. The corresponding directory should automatically be makred as root. Do you have the __init__.py file? – Nickpick Oct 24 '17 at 09:10
  • I tried that, making sure the project doesn't appear in the list when opening Pycharm, and then opening it by File > Open, but still the same issue. Yes the __init__ files are there, note also that the example above is from a django import. – Kemeia Oct 24 '17 at 11:14
  • If it's django that it doesn't find, make sure that you have chosen a project sdk of a virtual environment of python that contains django – Nickpick Oct 26 '17 at 10:16
2

In the latest release of Pycharm, setting the project folder as "content root" fixes your problem.

  1. Go to Preferences

enter image description here

  1. Go to Project -> Project Structure
  2. Add content root with your project directory path

enter image description here