Pycharm doesnt recognize project packages - python

Pycharm doesn't recognize the packages of my project's modules.
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

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"
Hope this helps.

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.
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.

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.

In the latest release of Pycharm, setting the project folder as "content root" fixes your problem.
Go to Preferences
Go to Project -> Project Structure
Add content root with your project directory path

Related

Problems executing files through venv python local imports cause errors [duplicate]

I have a directory structure
├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...
And I can access the network module with sys.path.insert().
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?
Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.
This way, you don't have to hard code things into your interpreter's settings:
Add src as a source content root:
                           
Then make sure to add add sources to your PYTHONPATH under:
Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console
Now imports will be resolved:
                     
This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:
                                 
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
check for __init__.py file in src folder
add the src folder as a source root
Then make sure to add sources to your PYTHONPATH (see above)
in PyCharm menu select: File --> Invalidate Caches --> Restart
If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.
ie. Right click on your root folder --> 'Mark Directory As' --> Source Root
After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:
close the project and quick PyCharm
delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Find the folder that relative imports require in the project explorer
right click and mark it as "Source Root"
Editor not marking init.py as Python but as Text
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files
or
Find Text and delete __init__.py from the list of text files
The project I cloned had a directory called modules and was successfully using files from there in the code with import this as that, but Pycharm was unable to jump to those code fragments because it did not recognise the imports.
Marking the module folder in the following settings section as source solved the issue.
Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.
Although all the answers are really helpful, there's one tiny piece of information that should be explained explicitly:
Essentially, a project with multiple hierarchical directories work as a package with some attributes.
To import custom local created Classes, we need to navigate to the directory containing .py file and create an __init__.py (empty) file there.
Why this helps is because this file is required to make Python treat the directory as containing packages. Cheers!
Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the 'Install Package' button.
After following the accepted answer, doing the following solved it for me:
File → Settings → Project <your directory/project> → Project Dependencies
Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.
My folder hierarcy is slightly different from the one in the question. Mine is like this
├── MyDirectory
│     └── simulate.py
├── src
│     ├── networkAlgorithm.py
│     ├── ...
Telling Pycharm that src depends on MyDirectory solved the issue for me!
This worked for me: Top Menu -> File -> Invalidate Caches/Restart
--> Right-click on the directory where your files are located in PyCharm
Go to the --> Mark Directory as
Select the --> Source Root
your problem will be solved
Many a times what happens is that the plugin is not installed. e.g.
If you are developing a django project and you do not have django plugin installed in pyCharm, it says error 'unresolved reference'.
Refer:
https://www.jetbrains.com/pycharm/help/resolving-references.html
I was also using a virtual environment like Dan above, however I was able to add an interpreter in the existing environment, therefore not needing to inherit global site packages and therefore undo what a virtual environment is trying to achieve.
Please check if you are using the right interpreter that you are supposed to. I was getting error "unresolved reference 'django' " to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings:
Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).
In my case the problem was I was using Virtual environment which didn't have access to global site-packages. Thus, the interpreter was not aware of the newly installed packages.
To resolve the issue, just edit or recreate your virtual interpreter and tick the Inherit global site-packages option.
Done in PyCharm 2019.3.1
Right-click on your src folder -> "Mark Directory as" -> Click-on "Excluded" and your src folder should be blue.
I tried everything here twice and even more. I finally solved it doing something I hadn't seen anywhere online. If you go to Settings>Editor>File Types there is an 'Ignore Files and folders' line at the bottom. In my case, I was ignoring 'venv', which is what I always name my virtual environments. So I removed venv; from the list of directories to ignore and VOILA!! I was FINALLY able to fix this problem. Literally all of my import problems were fixed for the project.
BTW, I had installed each and every package using PyCharm, and not through a terminal. (Meaning, by going to Settings>Interpreter...). I had invalidated cache, changed 'Source Root', restarted PyCharm, refreshed my interpreters paths, changed interpreters, deleted my venv... I tried everything. This finally worked. Obviously there are multiple problems going on here with different people, so this may not work for you, but it's definitely worth a shot if nothing else has worked, and easy to reverse if it doesn't.
For my case :
Directory0
├── Directory1
│ └── file1.py
├── Directory2
│ ├── file2.py
Into file1, I have :
from Directory2 import file2
which trows an "unresolved reference Directory2".
I resolved it by:
marking the parent directory Directory0 as "Source Root" like said above
AND
putting my cursor on another line on the file where I had the error so that it takes my modification into account
It is silly but if I don't do the second action, the error still appears and can make you think that you didn't resolve the issue by marking the parent directory as Source Root.
For me, adding virtualenv (venv)'s site-packages path to the paths of the interpreter works.
Finally!
I had the same problem and also try so many suggestions but none of them worked, until I find this post (https://stackoverflow.com/a/62632870/16863617). Regardless his solution didn't work for me, it helped me to came up with the idea to add _init.py_ into the --> Settings | Editor | File Types | Python | Registered patterns
ScreenShot
And the unresolved reference error is now solved.
just note if u have a problem with python interpreter not installing
packages, just change the permission for folder PycharmProjects
C:\Users'username'\PycharmProjects
to every one
This problem also appears if you use a dash within the Python filename, which therefore is strongly discouraged.
I encountered an import problem when installing aiogram. At the same time, the bot worked, but pyCharm highlighted the import in red and did not give hints. I've tried all of the above many times.As a result, the following helped me: I found the aiogram folder at the following path
c\Users...\AppData\Roaming\Python\Python310\site-packages
and copied it to the folder
C:\Program Files\Python310\Lib\site-packages
After that, I reset pyCharm and that's it!
In my case, with Pycharm 2019.3, the problem was that I forgot to add the extension '.py' to the file I wanted to import. Once added, the error went away without needing to invalides caches or any other step.
Pycharm uses venv. In the venv's console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.
The easiest way to fix it is by doing the following in your pyCharm software:
Click on: File > Settings > (Project: your project name) > Project Interpreter >
then click on the "+" icon on the right side to search for the package you want and install it.
Enjoy coding !!!
In newer versions of pycharm u can do simply by right clicking on the directory or python package from which you want to import a file, then click on 'Mark Directory As' -> 'Sources Root'

Set PYTHONPATH with PyCharm using remote interpreter [duplicate]

I have a directory structure
├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...
And I can access the network module with sys.path.insert().
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?
Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.
This way, you don't have to hard code things into your interpreter's settings:
Add src as a source content root:
                           
Then make sure to add add sources to your PYTHONPATH under:
Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console
Now imports will be resolved:
                     
This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:
                                 
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
check for __init__.py file in src folder
add the src folder as a source root
Then make sure to add sources to your PYTHONPATH (see above)
in PyCharm menu select: File --> Invalidate Caches --> Restart
If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.
ie. Right click on your root folder --> 'Mark Directory As' --> Source Root
After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:
close the project and quick PyCharm
delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Find the folder that relative imports require in the project explorer
right click and mark it as "Source Root"
Editor not marking init.py as Python but as Text
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files
or
Find Text and delete __init__.py from the list of text files
The project I cloned had a directory called modules and was successfully using files from there in the code with import this as that, but Pycharm was unable to jump to those code fragments because it did not recognise the imports.
Marking the module folder in the following settings section as source solved the issue.
Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.
Although all the answers are really helpful, there's one tiny piece of information that should be explained explicitly:
Essentially, a project with multiple hierarchical directories work as a package with some attributes.
To import custom local created Classes, we need to navigate to the directory containing .py file and create an __init__.py (empty) file there.
Why this helps is because this file is required to make Python treat the directory as containing packages. Cheers!
Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the 'Install Package' button.
After following the accepted answer, doing the following solved it for me:
File → Settings → Project <your directory/project> → Project Dependencies
Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.
My folder hierarcy is slightly different from the one in the question. Mine is like this
├── MyDirectory
│     └── simulate.py
├── src
│     ├── networkAlgorithm.py
│     ├── ...
Telling Pycharm that src depends on MyDirectory solved the issue for me!
This worked for me: Top Menu -> File -> Invalidate Caches/Restart
--> Right-click on the directory where your files are located in PyCharm
Go to the --> Mark Directory as
Select the --> Source Root
your problem will be solved
Many a times what happens is that the plugin is not installed. e.g.
If you are developing a django project and you do not have django plugin installed in pyCharm, it says error 'unresolved reference'.
Refer:
https://www.jetbrains.com/pycharm/help/resolving-references.html
I was also using a virtual environment like Dan above, however I was able to add an interpreter in the existing environment, therefore not needing to inherit global site packages and therefore undo what a virtual environment is trying to achieve.
Please check if you are using the right interpreter that you are supposed to. I was getting error "unresolved reference 'django' " to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings:
Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).
In my case the problem was I was using Virtual environment which didn't have access to global site-packages. Thus, the interpreter was not aware of the newly installed packages.
To resolve the issue, just edit or recreate your virtual interpreter and tick the Inherit global site-packages option.
Done in PyCharm 2019.3.1
Right-click on your src folder -> "Mark Directory as" -> Click-on "Excluded" and your src folder should be blue.
I tried everything here twice and even more. I finally solved it doing something I hadn't seen anywhere online. If you go to Settings>Editor>File Types there is an 'Ignore Files and folders' line at the bottom. In my case, I was ignoring 'venv', which is what I always name my virtual environments. So I removed venv; from the list of directories to ignore and VOILA!! I was FINALLY able to fix this problem. Literally all of my import problems were fixed for the project.
BTW, I had installed each and every package using PyCharm, and not through a terminal. (Meaning, by going to Settings>Interpreter...). I had invalidated cache, changed 'Source Root', restarted PyCharm, refreshed my interpreters paths, changed interpreters, deleted my venv... I tried everything. This finally worked. Obviously there are multiple problems going on here with different people, so this may not work for you, but it's definitely worth a shot if nothing else has worked, and easy to reverse if it doesn't.
For my case :
Directory0
├── Directory1
│ └── file1.py
├── Directory2
│ ├── file2.py
Into file1, I have :
from Directory2 import file2
which trows an "unresolved reference Directory2".
I resolved it by:
marking the parent directory Directory0 as "Source Root" like said above
AND
putting my cursor on another line on the file where I had the error so that it takes my modification into account
It is silly but if I don't do the second action, the error still appears and can make you think that you didn't resolve the issue by marking the parent directory as Source Root.
For me, adding virtualenv (venv)'s site-packages path to the paths of the interpreter works.
Finally!
I had the same problem and also try so many suggestions but none of them worked, until I find this post (https://stackoverflow.com/a/62632870/16863617). Regardless his solution didn't work for me, it helped me to came up with the idea to add _init.py_ into the --> Settings | Editor | File Types | Python | Registered patterns
ScreenShot
And the unresolved reference error is now solved.
just note if u have a problem with python interpreter not installing
packages, just change the permission for folder PycharmProjects
C:\Users'username'\PycharmProjects
to every one
This problem also appears if you use a dash within the Python filename, which therefore is strongly discouraged.
I encountered an import problem when installing aiogram. At the same time, the bot worked, but pyCharm highlighted the import in red and did not give hints. I've tried all of the above many times.As a result, the following helped me: I found the aiogram folder at the following path
c\Users...\AppData\Roaming\Python\Python310\site-packages
and copied it to the folder
C:\Program Files\Python310\Lib\site-packages
After that, I reset pyCharm and that's it!
In my case, with Pycharm 2019.3, the problem was that I forgot to add the extension '.py' to the file I wanted to import. Once added, the error went away without needing to invalides caches or any other step.
Pycharm uses venv. In the venv's console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.
The easiest way to fix it is by doing the following in your pyCharm software:
Click on: File > Settings > (Project: your project name) > Project Interpreter >
then click on the "+" icon on the right side to search for the package you want and install it.
Enjoy coding !!!
In newer versions of pycharm u can do simply by right clicking on the directory or python package from which you want to import a file, then click on 'Mark Directory As' -> 'Sources Root'

How to make python import be recognised with path [duplicate]

I have a directory structure
├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...
And I can access the network module with sys.path.insert().
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?
Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.
This way, you don't have to hard code things into your interpreter's settings:
Add src as a source content root:
                           
Then make sure to add add sources to your PYTHONPATH under:
Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console
Now imports will be resolved:
                     
This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:
                                 
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
check for __init__.py file in src folder
add the src folder as a source root
Then make sure to add sources to your PYTHONPATH (see above)
in PyCharm menu select: File --> Invalidate Caches --> Restart
If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.
ie. Right click on your root folder --> 'Mark Directory As' --> Source Root
After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:
close the project and quick PyCharm
delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Find the folder that relative imports require in the project explorer
right click and mark it as "Source Root"
Editor not marking init.py as Python but as Text
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files
or
Find Text and delete __init__.py from the list of text files
The project I cloned had a directory called modules and was successfully using files from there in the code with import this as that, but Pycharm was unable to jump to those code fragments because it did not recognise the imports.
Marking the module folder in the following settings section as source solved the issue.
Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.
Although all the answers are really helpful, there's one tiny piece of information that should be explained explicitly:
Essentially, a project with multiple hierarchical directories work as a package with some attributes.
To import custom local created Classes, we need to navigate to the directory containing .py file and create an __init__.py (empty) file there.
Why this helps is because this file is required to make Python treat the directory as containing packages. Cheers!
Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the 'Install Package' button.
After following the accepted answer, doing the following solved it for me:
File → Settings → Project <your directory/project> → Project Dependencies
Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.
My folder hierarcy is slightly different from the one in the question. Mine is like this
├── MyDirectory
│     └── simulate.py
├── src
│     ├── networkAlgorithm.py
│     ├── ...
Telling Pycharm that src depends on MyDirectory solved the issue for me!
This worked for me: Top Menu -> File -> Invalidate Caches/Restart
--> Right-click on the directory where your files are located in PyCharm
Go to the --> Mark Directory as
Select the --> Source Root
your problem will be solved
Many a times what happens is that the plugin is not installed. e.g.
If you are developing a django project and you do not have django plugin installed in pyCharm, it says error 'unresolved reference'.
Refer:
https://www.jetbrains.com/pycharm/help/resolving-references.html
I was also using a virtual environment like Dan above, however I was able to add an interpreter in the existing environment, therefore not needing to inherit global site packages and therefore undo what a virtual environment is trying to achieve.
Please check if you are using the right interpreter that you are supposed to. I was getting error "unresolved reference 'django' " to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings:
Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).
In my case the problem was I was using Virtual environment which didn't have access to global site-packages. Thus, the interpreter was not aware of the newly installed packages.
To resolve the issue, just edit or recreate your virtual interpreter and tick the Inherit global site-packages option.
Done in PyCharm 2019.3.1
Right-click on your src folder -> "Mark Directory as" -> Click-on "Excluded" and your src folder should be blue.
I tried everything here twice and even more. I finally solved it doing something I hadn't seen anywhere online. If you go to Settings>Editor>File Types there is an 'Ignore Files and folders' line at the bottom. In my case, I was ignoring 'venv', which is what I always name my virtual environments. So I removed venv; from the list of directories to ignore and VOILA!! I was FINALLY able to fix this problem. Literally all of my import problems were fixed for the project.
BTW, I had installed each and every package using PyCharm, and not through a terminal. (Meaning, by going to Settings>Interpreter...). I had invalidated cache, changed 'Source Root', restarted PyCharm, refreshed my interpreters paths, changed interpreters, deleted my venv... I tried everything. This finally worked. Obviously there are multiple problems going on here with different people, so this may not work for you, but it's definitely worth a shot if nothing else has worked, and easy to reverse if it doesn't.
For my case :
Directory0
├── Directory1
│ └── file1.py
├── Directory2
│ ├── file2.py
Into file1, I have :
from Directory2 import file2
which trows an "unresolved reference Directory2".
I resolved it by:
marking the parent directory Directory0 as "Source Root" like said above
AND
putting my cursor on another line on the file where I had the error so that it takes my modification into account
It is silly but if I don't do the second action, the error still appears and can make you think that you didn't resolve the issue by marking the parent directory as Source Root.
For me, adding virtualenv (venv)'s site-packages path to the paths of the interpreter works.
Finally!
I had the same problem and also try so many suggestions but none of them worked, until I find this post (https://stackoverflow.com/a/62632870/16863617). Regardless his solution didn't work for me, it helped me to came up with the idea to add _init.py_ into the --> Settings | Editor | File Types | Python | Registered patterns
ScreenShot
And the unresolved reference error is now solved.
just note if u have a problem with python interpreter not installing
packages, just change the permission for folder PycharmProjects
C:\Users'username'\PycharmProjects
to every one
This problem also appears if you use a dash within the Python filename, which therefore is strongly discouraged.
I encountered an import problem when installing aiogram. At the same time, the bot worked, but pyCharm highlighted the import in red and did not give hints. I've tried all of the above many times.As a result, the following helped me: I found the aiogram folder at the following path
c\Users...\AppData\Roaming\Python\Python310\site-packages
and copied it to the folder
C:\Program Files\Python310\Lib\site-packages
After that, I reset pyCharm and that's it!
In my case, with Pycharm 2019.3, the problem was that I forgot to add the extension '.py' to the file I wanted to import. Once added, the error went away without needing to invalides caches or any other step.
Pycharm uses venv. In the venv's console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.
The easiest way to fix it is by doing the following in your pyCharm software:
Click on: File > Settings > (Project: your project name) > Project Interpreter >
then click on the "+" icon on the right side to search for the package you want and install it.
Enjoy coding !!!
In newer versions of pycharm u can do simply by right clicking on the directory or python package from which you want to import a file, then click on 'Mark Directory As' -> 'Sources Root'

Why do I get unresolved reference while the included file is there? [duplicate]

I have a directory structure
├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...
And I can access the network module with sys.path.insert().
import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *
However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?
Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.
This way, you don't have to hard code things into your interpreter's settings:
Add src as a source content root:
                           
Then make sure to add add sources to your PYTHONPATH under:
Preferences ~ Build, Execution, Deployment ~ Console ~ Python Console
Now imports will be resolved:
                     
This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:
                                 
After all this don't forget to restart. In PyCharm menu select: File --> Invalidate Caches / Restart
check for __init__.py file in src folder
add the src folder as a source root
Then make sure to add sources to your PYTHONPATH (see above)
in PyCharm menu select: File --> Invalidate Caches --> Restart
If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.
ie. Right click on your root folder --> 'Mark Directory As' --> Source Root
After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.
Normally, $PYTHONPATH is used to teach python interpreter to find necessary modules. PyCharm needs to add the path in Preference.
There are several reasons why this could be happening. Below are several steps that fixes the majority of those cases:
.idea caching issue
Some .idea issue causing the IDE to show error while the code still runs correctly. Solution:
close the project and quick PyCharm
delete the .idea folder where the project is. note that it is a hidden folder and you might not be aware of its existence in your project directory.
start PyCharm and recreate the project
imports relative not to project folder
Relative imports while code root folder is not the same as the project folder. Solution:
Find the folder that relative imports require in the project explorer
right click and mark it as "Source Root"
Editor not marking init.py as Python but as Text
Which is the most illusive of all the cases. Here, for some reason, PyCharm considers all __init__.py files not to be python files, and thus ignores them during code analysis. To fix this:
Open PyCharm settings
Navigate to Editor -> File Types
Find Python and add __init__.py to the list of python files
or
Find Text and delete __init__.py from the list of text files
The project I cloned had a directory called modules and was successfully using files from there in the code with import this as that, but Pycharm was unable to jump to those code fragments because it did not recognise the imports.
Marking the module folder in the following settings section as source solved the issue.
Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.
Although all the answers are really helpful, there's one tiny piece of information that should be explained explicitly:
Essentially, a project with multiple hierarchical directories work as a package with some attributes.
To import custom local created Classes, we need to navigate to the directory containing .py file and create an __init__.py (empty) file there.
Why this helps is because this file is required to make Python treat the directory as containing packages. Cheers!
Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the 'Install Package' button.
After following the accepted answer, doing the following solved it for me:
File → Settings → Project <your directory/project> → Project Dependencies
Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.
My folder hierarcy is slightly different from the one in the question. Mine is like this
├── MyDirectory
│     └── simulate.py
├── src
│     ├── networkAlgorithm.py
│     ├── ...
Telling Pycharm that src depends on MyDirectory solved the issue for me!
This worked for me: Top Menu -> File -> Invalidate Caches/Restart
--> Right-click on the directory where your files are located in PyCharm
Go to the --> Mark Directory as
Select the --> Source Root
your problem will be solved
Many a times what happens is that the plugin is not installed. e.g.
If you are developing a django project and you do not have django plugin installed in pyCharm, it says error 'unresolved reference'.
Refer:
https://www.jetbrains.com/pycharm/help/resolving-references.html
I was also using a virtual environment like Dan above, however I was able to add an interpreter in the existing environment, therefore not needing to inherit global site packages and therefore undo what a virtual environment is trying to achieve.
Please check if you are using the right interpreter that you are supposed to. I was getting error "unresolved reference 'django' " to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings:
Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).
In my case the problem was I was using Virtual environment which didn't have access to global site-packages. Thus, the interpreter was not aware of the newly installed packages.
To resolve the issue, just edit or recreate your virtual interpreter and tick the Inherit global site-packages option.
Done in PyCharm 2019.3.1
Right-click on your src folder -> "Mark Directory as" -> Click-on "Excluded" and your src folder should be blue.
I tried everything here twice and even more. I finally solved it doing something I hadn't seen anywhere online. If you go to Settings>Editor>File Types there is an 'Ignore Files and folders' line at the bottom. In my case, I was ignoring 'venv', which is what I always name my virtual environments. So I removed venv; from the list of directories to ignore and VOILA!! I was FINALLY able to fix this problem. Literally all of my import problems were fixed for the project.
BTW, I had installed each and every package using PyCharm, and not through a terminal. (Meaning, by going to Settings>Interpreter...). I had invalidated cache, changed 'Source Root', restarted PyCharm, refreshed my interpreters paths, changed interpreters, deleted my venv... I tried everything. This finally worked. Obviously there are multiple problems going on here with different people, so this may not work for you, but it's definitely worth a shot if nothing else has worked, and easy to reverse if it doesn't.
For my case :
Directory0
├── Directory1
│ └── file1.py
├── Directory2
│ ├── file2.py
Into file1, I have :
from Directory2 import file2
which trows an "unresolved reference Directory2".
I resolved it by:
marking the parent directory Directory0 as "Source Root" like said above
AND
putting my cursor on another line on the file where I had the error so that it takes my modification into account
It is silly but if I don't do the second action, the error still appears and can make you think that you didn't resolve the issue by marking the parent directory as Source Root.
For me, adding virtualenv (venv)'s site-packages path to the paths of the interpreter works.
Finally!
I had the same problem and also try so many suggestions but none of them worked, until I find this post (https://stackoverflow.com/a/62632870/16863617). Regardless his solution didn't work for me, it helped me to came up with the idea to add _init.py_ into the --> Settings | Editor | File Types | Python | Registered patterns
ScreenShot
And the unresolved reference error is now solved.
just note if u have a problem with python interpreter not installing
packages, just change the permission for folder PycharmProjects
C:\Users'username'\PycharmProjects
to every one
This problem also appears if you use a dash within the Python filename, which therefore is strongly discouraged.
I encountered an import problem when installing aiogram. At the same time, the bot worked, but pyCharm highlighted the import in red and did not give hints. I've tried all of the above many times.As a result, the following helped me: I found the aiogram folder at the following path
c\Users...\AppData\Roaming\Python\Python310\site-packages
and copied it to the folder
C:\Program Files\Python310\Lib\site-packages
After that, I reset pyCharm and that's it!
In my case, with Pycharm 2019.3, the problem was that I forgot to add the extension '.py' to the file I wanted to import. Once added, the error went away without needing to invalides caches or any other step.
Pycharm uses venv. In the venv's console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.
The easiest way to fix it is by doing the following in your pyCharm software:
Click on: File > Settings > (Project: your project name) > Project Interpreter >
then click on the "+" icon on the right side to search for the package you want and install it.
Enjoy coding !!!
In newer versions of pycharm u can do simply by right clicking on the directory or python package from which you want to import a file, then click on 'Mark Directory As' -> 'Sources Root'

"Cannot start process, the working directory ... does not exist" error in PyCharm

When I run my Main.py script, PyCharm keeps telling me Cannot start process, the working directory /home/myname/PyCharmProjects/MyProjectName/mypackage does not exist.
This error occurred after creating a package (mypackage) for test purposes, moving files to it (including Main.py), and moving the files back to the root folder afterwards.
The package mypackage was empty after that, but PyCharm still thought that Main.py is located in that package. I could still run the program until I deleted the empty package, but path errors occurred. After deleting the package, I was unable to start it.
I can still run the other files that used to be in mypackage and are now in my root directory again.
I can still create and run new files in my root directory.
It happens because when you create a file it automatically assigns the working directory to it's configuration, which of course is the one where you created it.
You can change that by going into Run -> Edit Configurations. Click on the folder icon in Script path: and correct the path to the file. Click OK to save and you should be able to Run the file again.
After testing for a bit, I've found a solution (but not an answer to why this error occurs in PyCharm):
Delete the file and create it again. (Or rename or move it and create a new file with its old name, both should work.)
Set the working directory correctly
1. File-> Settings
2. Build, Execution, Deployment -> Console -> Python Console
3. Working directory: [The path to the directory where the file you're currently working on resides.]
I had the same problem, mine is probably related to the explaination gave by the others, it comes from the dir .idea, files *.xml contain the variable $DIR_PROJECT$.
Therefore, as the attribution of a new path didn't work, I just deleted my .idea, that is automatically loaded each time I open my project's directory.
It automatically regenerated the .idea, asked for the script path... And it worked perfectly
CAREFUL => You will automatically lose your project settings, you are deleting the "settings file"
I was getting this same error, and the path in "edit configurations" was correct.
However, this is what eventually got my code working again.
1) I commented out all of the code in my file ("ctrl" + "a" + "ctrl" + "/")
2) I commented something I knew would compile in the file. (my list of imports)
3) I ran the python file.
This time, it actually completely compiled and after that I was able to uncomment the rest of my code and everything worked again.
The issue kept popping up over and over in PyCharm. So I created a new project and loaded the needed script. Then I provided the directory to path and assigned the default Python version that I wanted to use... and it worked. Then I was able to finally use "execute line in console" once again.
I had this problem because I renamed my project, it was "xx" I renamed it to "yy", what I did was I went through the directory of .idea in the "yy", in any of those files (all XML files) if there were the name "xx", I replaced it with "yy"
I experienced this problem after moving my project to a different root directory. None of the above solutions worked for me.
I solved it by opening my entire project folder, instead of just the python file I was trying to run. And then running the file I wanted, while the entire project was loaded into PyCharm.
open the qtdesigner work dictionary setting and choose your project path then click OK, don't not use the mysterious work dictionary path by default
In my case Run -> Edit Configuration didn't help.
I've solved it changing the value of "WORKING_DIRECTORY" attribute in .idea -> workspace.xml
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/your/correct/path/here" />
I am a beginner so appreciated this 6 year and 6 month discussion. I couldn't add a comment. I setup PyCharm 2022.1.1 and a Python 3.10.4 virtual environment with pyside2 and pyside6 on Ubuntu 22.04. I tried all the suggestions above and could get none of them to work for me. Following these instructions: https://pythonpyqt.com/how-to-install-pyqt5-in-pycharm/ both pyside2-designer (Qt5Designer) and pyside6-designer from my virtual environment were added as external tools. pyside2-designer failed with the working directory error and pyside6-designer worked without any error. Looking closer at pyside2-designer selecting the working directory with the browse button, I still could not create file - exit code was 127 - but the Qt5 Designer was available for existing ui files. I used pyside2 because PyQt5-tools could not be pip installed in my python3.10.4 virtual env.
This worked for me on PyCharm 2021.3.2:

Categories

Resources