I am using PyCharm to work on a project. The project is opened and configured with an interpreter, and can run successfully. The remote interpreter paths are mapped properly. This seems to be the correct configuration, but PyCharm is highlighting my valid code with "unresolved reference" errors, even for built-in Python functions. Why don't these seem to be detected, even though the code runs? Is there any way to get PyCharm to recognize these correctly?
This specific instance of the problem is with a remote interpreter, but the problem appears on local interpreters as well.
File | Invalidate Caches... and restarting PyCharm helps.
Dmitry's response didn't work for me.
I got mine working by going to Project Interpreters, Selecting the "Paths" tab, and hitting the refresh button in that submenu. It auto-populated with something called "python-skeletons".
edit: screenshot using PyCharm 3.4.1 (it's quite well hidden)
There are many solutions to this, some more convenient than others, and they don't always work.
Here's all you can try, going from 'quick' to 'annoying':
Do File -> Invalidate Caches / Restart and restart PyCharm.
You could also do this after any of the below methods, just to be sure.
First, check which interpreter you're running: Run -> Edit Configurations -> Configuration -> Python Interpreter.
Refresh the paths of your interpreter:
File -> Settings
Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More...
Click the 'Show paths' button (bottom one)
Click the 'Refresh' button (bottom one)
Remove the interpreter and add it again:
File -> Settings
Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More...
Click the 'Remove' button
Click the 'Add' button and re-add your interpeter
Delete your project preferences
Delete your project's .idea folder
Close and re-open PyCharm
Open your project from scratch
Delete your PyCharm user preferences (but back them up first).
~/.PyCharm50 on Mac
%homepath%/.PyCharm50 on Windows
Switch to another interpreter, then back again to the one you want.
Create a new virtual environment, and switch to that environments' interpreter.
Create a new virtual environment in a new location -- outside of your project folder -- and switch to that environment's interpreter.
Switch to another interpreter altogether; don't switch back.
If you are using Docker, take note:
Make sure you are using pip3 not pip, especially with remote docker and docker-compose interpreters.
Avoid influencing PYTHONPATH. More info here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python .
If the above did not work for you, but you did find another trick, then please leave a comment.
In my case it was the directories structure.
My project looks like this:
+---dir_A
+---dir_B
+app
|
\-run.py
So right click on dir_b > "mark directory as" > "project root"
You have to mark your root directory as:
SOURCE ROOT (red),
and your applications:
EXCLUDED ROOT (blue).
Then the unresolved reference will disappear. If you use PyChram pro it do this for you automatically.
I find myself removing and re-adding the remote interpreter to fix this problem when Invalidating Caches or Refreshing Paths does not work.
I use vagrant and every once and awhile if I add a new VM to my multi-vm setup, the forwarded port changes and this seems to confuse PyCharm when it tries to use the wrong port for SSH. Changing the port doesn't seem to help the broken references.
If none of the other solutions work for you, try (backing up) and deleting your ~/.PyCharm40 folder, then reopening PyCharm. This will kill all your preferences as well.
On Mac you want to delete ~/Library/Caches/Pycharm40 and ~/Library/Preferences/PyCharm40.
And on Windows: C:\Users\$USER.PyCharm40.
Tested with PyCharm 4.0.6 (OSX 10.10.3)
following this steps:
Click PyCharm menu.
Select Project Interpreter.
Select Gear icon.
Select More button.
Select Project Interpreter you are in.
Select Directory Tree button.
Select Reload list of paths.
Problem solved!
Sorry to bump this question, however I have an important update to make.
You may also want to revert your project interpreter to to Python 2.7.6 if you're using any other version than that This worked for me on my Ubuntu installation of PyCharm 4.04 professional after none of the other recommendations solved my problem.
Much simpler action:
File > Settings > Project > Project Interpreter
Select "No interpreter" in the "Project interpreter" list
Apply > Set your python interpreter again > Click Apply
Profit - Pycharm is updating skeletons and everything is fine.
If you want to ignore only some "unresolved reference" errors, you can also tell it PyCharm explicitly by placing this in front of your class/method/function:
# noinspection PyUnresolvedReferences
You might try closing Pycharm, deleting the .idea folder from your project, then starting Pycharm again and recreating the project. This worked for me whereas invalidating cache did not.
I finally got this working after none of the proposed solutions worked for me. I was playing with a django rest framework project and was using a virtualenv I had setup with it. I was able to get Pycharm fixed by marking the root folder as the sources root, but then django's server would throw resolve exceptions. So one would work when the other wouldn't and vice versa.
Ultimately I just had to mark the subfolder as the sources root in pycharm. So my structure was like this
-playground
-env
-playground
That second playground folder is the one I had to mark as the sources root for everything to work as expected. That didn't present any issues for my scenario so it was a workable solution.
Just thought I'd share in case someone else can use it.
It could also be a python version issue. I had to pick the right one to make it work.
None of the answers solved my problem.
What did it for me was switching environments and going back to the same environment. File->Settings->Project interpreter
I am using conda environments.
Mine got resolved by checking inherit global site-packages in PyCharm
File -> Settings -> Project Interpreter -> Add Local Interpreter -> Inherit global site-packages
I closed all the other projects and run my required project in isolation in Pycharm. I created a separate virtualenv from pycharm and added all the required modules in it by using pip. I added this virtual environment in project's interpreter. This solved my problem.
Geeze what a nightmare, my amalgamation of different StackOVerflow answers:
Switch to local interpreter /usr/bin/pythonX.X and apply
View paths like above answer
Find skeletons path. Mine was (/home/tim/Desktop/pycharm-community-2016.2.3/helpers/python-skeletons)
Switch back to virt interpreter and add the skeletons path manually if it didn't automatically show up.
None of the above solutions worked for me!
If you are using virtual environment for your project make sure to apply the python.exe file that is inside your virtual environment directory as interpreter for the project (Alt + Ctrl + Shift + S)
this solved the issue for me.
In my case the inspection error shows up due to a very specific case of python code.
A min function that contains two numpy functions and two list accesses makes my code inspection give this kind of errors.
Removing the 'd=0' line in the following example gives an unresolved reference error as expected, but readding doesn't make the error go away for the code inspector. I can still execute the code without problems afterwards.
import numpy as np
def strange(S, T, U, V):
d = 0
print min(np.abs(S[d]), np.abs(T[d]), U[d], V[d])
Clearing caches and reloading list of paths doesn't work. Only altering the code with one of the following example patches does work:
Another ordering of the 'min' parameters: schematically S U T V but not S T U V or T S U V
Using a method instead of the function: S[d].abs() instead of np.abs(S[d])
Using the built-in abs() function
Adding a number to a parameter of choice: U[d] + 0.
My problem is that Flask-WTF is not resolved by PyCharm. I have tried to re-install and then install or Invalidate Cache and Restart PyCharm, but it's still not working.
Then I came up with this solution and it works perfectly for me.
Open Project Interpreter by Ctrl+Alt+S (Windows) and then click Install (+) a new packgage.
Type the package which is not resolved by PyCharm and then click Install Package. Then click OK.
Now, you'll see your library has been resolved.
In PyCharm 2020.1.4 (Community Edition) on Windows 10 10.0. Under Settings in PyCharm: File > Settings > Project Structure
I made two changes in Project Structure:
main folder marked as source and
odoo folder with all applications I excluded
Screenshot shows what I did.
After that I restarted PyCharm: File > Invalidate Caches / Restart...
Unresolved references error was removed
Invalidating the cache as suggested by other answers did not work for me. What I found to be the problem in my case was that PyCharm was marking init.py files of Python packages as text and thus not including them in the analysis which means python resolving was not working correctly.
The solution for me was to:
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
To add yet another one: None of the solutions involving the Python Interpreter tab helped, however, I noticed I had to set Project Dependencies: In the project that had unresolved reference errors, none of the dependencies were checked. Once I checked them, the relevant errors disappeared. I don't know why some are checked to begin with and others are not.
If you are using vagrant the error can be caused by wrong python interpreter.
In our vagrant we are using pyenv so I had to change Python Interpreter path path from /usr/bin/python to /home/vagrant/.pyenv/versions/vagrant/bin/python
I have a project where one file in src/ imports another file in the same directory. To get PyCharm to recognize I had to to go to File > Settings > Project > Project Structure > select src folder and click "Mark as: Sources"
From https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
Source roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports
I had to go to File->Invalidate Caches/Restart, reboot Ubuntu 18.04 LTS, then open Pycharm and File-> Invalidate Caches/Restart again before it cleared up.
For me it helped:
update your main directory "mark Directory as" -> "source root"
#kelorek works for me, but before, in interpereter paths I had to add some path.
lets say
from geometry_msgs.msg import Twist
is underline as error, then in remote machine in python run:
help("geometry_msgs")
at the end there will be path lets say :
/opt/ros/foxy/lib/python3.8/site-packages/geometry_msgs/__init__.py
so to Your intepreter pycharm path add
/opt/ros/foxy/lib/python3.8/site-packages
Hope it will help You and it helps me :)
I had the same symptoms. In my instance the problem source was that I had set
idea.max.intellisense.filesize=50 in the custom properties.
I could resolve it by setting it to 100.
Help->Edit Custom Properties
Related
I am using PyCharm to work on a project. The project is opened and configured with an interpreter, and can run successfully. The remote interpreter paths are mapped properly. This seems to be the correct configuration, but PyCharm is highlighting my valid code with "unresolved reference" errors, even for built-in Python functions. Why don't these seem to be detected, even though the code runs? Is there any way to get PyCharm to recognize these correctly?
This specific instance of the problem is with a remote interpreter, but the problem appears on local interpreters as well.
File | Invalidate Caches... and restarting PyCharm helps.
Dmitry's response didn't work for me.
I got mine working by going to Project Interpreters, Selecting the "Paths" tab, and hitting the refresh button in that submenu. It auto-populated with something called "python-skeletons".
edit: screenshot using PyCharm 3.4.1 (it's quite well hidden)
There are many solutions to this, some more convenient than others, and they don't always work.
Here's all you can try, going from 'quick' to 'annoying':
Do File -> Invalidate Caches / Restart and restart PyCharm.
You could also do this after any of the below methods, just to be sure.
First, check which interpreter you're running: Run -> Edit Configurations -> Configuration -> Python Interpreter.
Refresh the paths of your interpreter:
File -> Settings
Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More...
Click the 'Show paths' button (bottom one)
Click the 'Refresh' button (bottom one)
Remove the interpreter and add it again:
File -> Settings
Project: [name] -> Project Interpreter -> 'Project Interpreter': Gear icon -> More...
Click the 'Remove' button
Click the 'Add' button and re-add your interpeter
Delete your project preferences
Delete your project's .idea folder
Close and re-open PyCharm
Open your project from scratch
Delete your PyCharm user preferences (but back them up first).
~/.PyCharm50 on Mac
%homepath%/.PyCharm50 on Windows
Switch to another interpreter, then back again to the one you want.
Create a new virtual environment, and switch to that environments' interpreter.
Create a new virtual environment in a new location -- outside of your project folder -- and switch to that environment's interpreter.
Switch to another interpreter altogether; don't switch back.
If you are using Docker, take note:
Make sure you are using pip3 not pip, especially with remote docker and docker-compose interpreters.
Avoid influencing PYTHONPATH. More info here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000058690-Module-not-found-in-PyCharm-but-externally-in-Python .
If the above did not work for you, but you did find another trick, then please leave a comment.
In my case it was the directories structure.
My project looks like this:
+---dir_A
+---dir_B
+app
|
\-run.py
So right click on dir_b > "mark directory as" > "project root"
You have to mark your root directory as:
SOURCE ROOT (red),
and your applications:
EXCLUDED ROOT (blue).
Then the unresolved reference will disappear. If you use PyChram pro it do this for you automatically.
I find myself removing and re-adding the remote interpreter to fix this problem when Invalidating Caches or Refreshing Paths does not work.
I use vagrant and every once and awhile if I add a new VM to my multi-vm setup, the forwarded port changes and this seems to confuse PyCharm when it tries to use the wrong port for SSH. Changing the port doesn't seem to help the broken references.
If none of the other solutions work for you, try (backing up) and deleting your ~/.PyCharm40 folder, then reopening PyCharm. This will kill all your preferences as well.
On Mac you want to delete ~/Library/Caches/Pycharm40 and ~/Library/Preferences/PyCharm40.
And on Windows: C:\Users\$USER.PyCharm40.
Tested with PyCharm 4.0.6 (OSX 10.10.3)
following this steps:
Click PyCharm menu.
Select Project Interpreter.
Select Gear icon.
Select More button.
Select Project Interpreter you are in.
Select Directory Tree button.
Select Reload list of paths.
Problem solved!
Sorry to bump this question, however I have an important update to make.
You may also want to revert your project interpreter to to Python 2.7.6 if you're using any other version than that This worked for me on my Ubuntu installation of PyCharm 4.04 professional after none of the other recommendations solved my problem.
Much simpler action:
File > Settings > Project > Project Interpreter
Select "No interpreter" in the "Project interpreter" list
Apply > Set your python interpreter again > Click Apply
Profit - Pycharm is updating skeletons and everything is fine.
If you want to ignore only some "unresolved reference" errors, you can also tell it PyCharm explicitly by placing this in front of your class/method/function:
# noinspection PyUnresolvedReferences
You might try closing Pycharm, deleting the .idea folder from your project, then starting Pycharm again and recreating the project. This worked for me whereas invalidating cache did not.
I finally got this working after none of the proposed solutions worked for me. I was playing with a django rest framework project and was using a virtualenv I had setup with it. I was able to get Pycharm fixed by marking the root folder as the sources root, but then django's server would throw resolve exceptions. So one would work when the other wouldn't and vice versa.
Ultimately I just had to mark the subfolder as the sources root in pycharm. So my structure was like this
-playground
-env
-playground
That second playground folder is the one I had to mark as the sources root for everything to work as expected. That didn't present any issues for my scenario so it was a workable solution.
Just thought I'd share in case someone else can use it.
It could also be a python version issue. I had to pick the right one to make it work.
None of the answers solved my problem.
What did it for me was switching environments and going back to the same environment. File->Settings->Project interpreter
I am using conda environments.
Mine got resolved by checking inherit global site-packages in PyCharm
File -> Settings -> Project Interpreter -> Add Local Interpreter -> Inherit global site-packages
I closed all the other projects and run my required project in isolation in Pycharm. I created a separate virtualenv from pycharm and added all the required modules in it by using pip. I added this virtual environment in project's interpreter. This solved my problem.
Geeze what a nightmare, my amalgamation of different StackOVerflow answers:
Switch to local interpreter /usr/bin/pythonX.X and apply
View paths like above answer
Find skeletons path. Mine was (/home/tim/Desktop/pycharm-community-2016.2.3/helpers/python-skeletons)
Switch back to virt interpreter and add the skeletons path manually if it didn't automatically show up.
None of the above solutions worked for me!
If you are using virtual environment for your project make sure to apply the python.exe file that is inside your virtual environment directory as interpreter for the project (Alt + Ctrl + Shift + S)
this solved the issue for me.
In my case the inspection error shows up due to a very specific case of python code.
A min function that contains two numpy functions and two list accesses makes my code inspection give this kind of errors.
Removing the 'd=0' line in the following example gives an unresolved reference error as expected, but readding doesn't make the error go away for the code inspector. I can still execute the code without problems afterwards.
import numpy as np
def strange(S, T, U, V):
d = 0
print min(np.abs(S[d]), np.abs(T[d]), U[d], V[d])
Clearing caches and reloading list of paths doesn't work. Only altering the code with one of the following example patches does work:
Another ordering of the 'min' parameters: schematically S U T V but not S T U V or T S U V
Using a method instead of the function: S[d].abs() instead of np.abs(S[d])
Using the built-in abs() function
Adding a number to a parameter of choice: U[d] + 0.
My problem is that Flask-WTF is not resolved by PyCharm. I have tried to re-install and then install or Invalidate Cache and Restart PyCharm, but it's still not working.
Then I came up with this solution and it works perfectly for me.
Open Project Interpreter by Ctrl+Alt+S (Windows) and then click Install (+) a new packgage.
Type the package which is not resolved by PyCharm and then click Install Package. Then click OK.
Now, you'll see your library has been resolved.
In PyCharm 2020.1.4 (Community Edition) on Windows 10 10.0. Under Settings in PyCharm: File > Settings > Project Structure
I made two changes in Project Structure:
main folder marked as source and
odoo folder with all applications I excluded
Screenshot shows what I did.
After that I restarted PyCharm: File > Invalidate Caches / Restart...
Unresolved references error was removed
Invalidating the cache as suggested by other answers did not work for me. What I found to be the problem in my case was that PyCharm was marking init.py files of Python packages as text and thus not including them in the analysis which means python resolving was not working correctly.
The solution for me was to:
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
To add yet another one: None of the solutions involving the Python Interpreter tab helped, however, I noticed I had to set Project Dependencies: In the project that had unresolved reference errors, none of the dependencies were checked. Once I checked them, the relevant errors disappeared. I don't know why some are checked to begin with and others are not.
If you are using vagrant the error can be caused by wrong python interpreter.
In our vagrant we are using pyenv so I had to change Python Interpreter path path from /usr/bin/python to /home/vagrant/.pyenv/versions/vagrant/bin/python
I have a project where one file in src/ imports another file in the same directory. To get PyCharm to recognize I had to to go to File > Settings > Project > Project Structure > select src folder and click "Mark as: Sources"
From https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
Source roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports
I had to go to File->Invalidate Caches/Restart, reboot Ubuntu 18.04 LTS, then open Pycharm and File-> Invalidate Caches/Restart again before it cleared up.
For me it helped:
update your main directory "mark Directory as" -> "source root"
#kelorek works for me, but before, in interpereter paths I had to add some path.
lets say
from geometry_msgs.msg import Twist
is underline as error, then in remote machine in python run:
help("geometry_msgs")
at the end there will be path lets say :
/opt/ros/foxy/lib/python3.8/site-packages/geometry_msgs/__init__.py
so to Your intepreter pycharm path add
/opt/ros/foxy/lib/python3.8/site-packages
Hope it will help You and it helps me :)
I had the same symptoms. In my instance the problem source was that I had set
idea.max.intellisense.filesize=50 in the custom properties.
I could resolve it by setting it to 100.
Help->Edit Custom Properties
I'm using macOS 10.15.4 and PyCharm 2019.3.4
I currently have a folder inside the PyCharmProjects folder (that is automatically created when PyCharm first runs) where I keep projects for a class. I want to move this folder, which contains multiple other folders which each contain PyCharm projects, somewhere else on my computer (like the Desktop). The problem is if I move the folder (or even just a single PyCharm project) the next time I open the project in PyCharm, it says "Invalid python interpreter selected for the project." Now I can manually go into PyCharm preferences and point it to the new location I moved the folder so it can use the correct python interpreter. But this would be tedious to do for every single project I have. (And yes, every project I have uses its own interpreter and virtual environment.)
Is then a way I can move a folder containing multiple PyCharm projects without loosing the references to each of their respective python interpreters?
(Feel free to reword this question or the title.)
PyCharm uses configurations from your home directory. The docs say
macOS
Configuration
~/Library/Preferences/<PRODUCT><VERSION>
Caches
~/Library/Caches/<PRODUCT><VERSION>
Plugins
~/Library/Application Support/<PRODUCT><VERSION>
Logs
~/Library/Logs/<PRODUCT><VERSION>
where <PRODUCT> is PyCharm.
They are xml files. Among them also a list of configured interpreters. They are separate because they are available for all your projects to be chosen as the default interpreter. You'll have to edit the xml files.
For some reason PyCharm does not help with a moved virtualenv directory directly. (see comment from engineer: https://youtrack.jetbrains.com/issue/PY-32435#focus=Comments-27-3139072.0-0)
On the upside, everything else seems to work, except the path to the venv python. That one can be changed through preferences/project preferences/python interpreter. Just click on "add", "existing", and select the python compiler in your project folder.
Try not to select the ".virtualenvs" python compiler, as if you fumble like I did, it seems it's a bit of a pain to get rid of it and go back to the right one. If you do that, renaming the project folder once more gets you back to square one and you get to try again.
Note that I'm using MacOS. YMMV.
I have a PyCharm project on my Windows, where I am able to run most .py files by pressing Ctrl + Shift + F10 (or running the debugger). In one of the files however I get the error
Error running 'test':
Cannot run program "\opt\anaconda\bin\python" (in directory "..."): CreateProcess error=2, the system cannot find the file specified.`
The test.py file right now only contains print('hello')
I can do this for the other files, and using 'Execute selection in console' also works fine. Given I am on a Windows machine the "\opt\anaconda\bin\python" part looks suspicious, but I don't know how to fix it.
Any help?
Copying the content to another file (e.g. test_2.py) 'fixes' the problem, but since this is a collaborative project this isn't viable.
I think your case is cause by some project environment has changed.
I suggest your open the workspace.xml which located in .idea\, check the parameters in it.
or you can delete the directory ".idea" and re-create the project locate in the original path.
Hope it work
I had the same problem in PyCharm IDE and Windows after adding new libraries and some changes.
I recreated Run/Debug Configurations with these steps (Instead of recreating the whole of project!):
Select Edit Configurations... from top panel in PyCharm IDE
Select these files and press delete for deleting them
Recreate these files likes this images:
Click green arrow or press Ctrl + Shift + F10
This is what I had to do:
Check the .idea/workspace.xml for any old venv references (there are several tags like "SDK_HOME" which store the path to the venv) and update as necessary
Check the .idea/RunConfigurations for any run configs and update them (or delete and recreate as you like)
when I installed the pycharm I had the same issue. for this, you really need to understand the concept of the virtual environment. this error comes because you run the file in another directory in which you do not create any virtual environment.
let's say you create a virtual environment in any folder located at the desktop now you run the files in any other folder located in /user/AppData/any_folder then it will show the error that the system can't find the file specified.
So be sure you run in a file in the same folder in which you created a virtual environment.
I had the same problem after downloading a project from Github. It ended up being a configuration problem.
Creating a new project on Pycharm, pasting the code in it, and using your own configuration should solve the problem.
I am using PyCharm 5 to run a Python 2.7 (Anaconda) script in Ubuntu. My script imports a module with import tensorflow, but this causes the error ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory. So, it seems that the library libcudart.so.7.0 is needed by this module, but it cannot be found.
Now, I have seen that this library is on my machine in /usr/local/cuda-7.0/targets/x86_64-linux/lib. So, in PyCharm, I went to Settings->Project Interpreters->Interpreter Paths. This had a list of paths, such as /home/karnivaurus/Libraries/Anaconda/python2.7. I then added to this list, the path mentioned above which contains the required library.
However, this did not fix the problem. I still get an error telling me that libcudart.so.7.0 cannot be found. If I run my script from the shell though (python myfile.py), then it runs fine.
How can I tell PyCharm where to find this library?
I have noticed that if I have print sys.path in my script, the paths it prints out are entirely different to those in Settings->Project Interpreters->Interpreter Paths... should they be the same?
I came across this problem just recently using a remote debugger, however I believe it's still the same solution. I just added the following to the Environment Variables section in the Run/Debug Configuration options found in Run > Edit Configurations... dialog: LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
The path to your cuda library seems strange to me. I would expect it to be /usr/local/cuda-7.0/lib64 or /usr/local/cuda-7.0/lib.
Did you follow all of the cuda installation procedure?
If you type env on the command line, do you see a path to cuda in your LD_LIBRARY_PATH?
Update from comments below:
The issue is that PyCharm was invoked from the desktop, and wasn't getting the right environment variables. Solution is to either:
invoke from the command line,
create a script to set environment and then invoke, and make a link to that script on the desktop,
or set environment variables on the desktop item
While some of these answers are correct, and could work, I haven't seen what the OP specifically asked for, and that is where to set environments for the python console. This can be accomplished inside pycharm at:
File > Settings > Build,Execution,Deployment > Console > Python Console
In the options there, you'll find a place to define Environment Variables. Set LD_LIBRARY_PATH there.
Edit your pycharm.desktop, specify the environment variable in exec, like below:
[Desktop Entry]
Version=1.0
Type=Application
Name=Pycharm
Exec=env LD_LIBRARY_PATH=:/usr/local/cuda/lib64:/usr/local/cuda/lib64 /home/cwh/software/pycharm-2016.1.4/bin/pycharm.sh
Icon=/home/cwh/software/pycharm-2016.1.4/bin/pycharm.png
Name[zh_CN]=Pycharm
so pycharm will find cuda
Have you selected the right python interpreter in your project's settings?
See here.
I had a similar issue and changing the interpreter solved it without having to create a new icon.
The following works for me on Community edition 2019.3
To set globally for a project:
Open File/Settings/Project/Project Interpreter
click on the cog icon next to the interpreter
choose show all
click on the little folder with tree icon bottom right
add the path to "Interpreter Paths"
I have written a module (a file my_mod.py file residing in the folder my_module).
Currently, I am working in the file cool_script.py that resides in the folder cur_proj. I have opened the folder in PyCharm using File -- open (and I assume, hence, it is a PyCharm project).
In ProjectView (CMD-7), I can see my project cur_proj (in red) and under "External Libraries" I do see my_module. In cool_script.py, I can write
from my_module import my_mod as mm
and PyCharm even makes suggestion for my_mod. So far so good.
However, when I try to run cool_script.py, PyCharm tells me
"No module named my_module"
This seems strange to me, because
A) in the terminal (OS 10.10.2), in python, I can import the module no problem -- there is a corresponding entry in the PYTHONPATH in .bashrc
B) in PyCharm -- Settings -- Project cur_proj -- Project Interpreter -- CogWheel next to python interpreter -- more -- show paths for selected interpreter icon, the paths from PYTHONPATH do appear (as I think they should)
Hence, why do I get the error when I try to run cool_script.py? -- What am I missing?
Notes:
I am not declaring a different / special python version at the top of cool_script.py
I made sure that the path to my_module is correct
I put __init__.py files (empty files) both in my_module and in cur_proj
I am not using virtualenv
Addendum 2015-Feb-25
When I go in PyCharm to Run -- Edit Configurations, for my current project, there are two options that are selected with a check mark: "Add content roots to PYTHONPATH" and "Add source roots to PYTHONPATH". When I have both unchecked, I can load my module.
So it works now -- but why?
Further questions emerged:
What are "content roots" and what are "source roots"? And why does adding something to the PYTHONPATH make it somehow break?
should I uncheck both of those options all the time (so also in the defaults, not only the project specific configurations (left panel of the Run/Debug Configurations dialog)?
If your own module is in the same path, you need mark the path as Sources Root. In the project explorer, right-click on the directory that you want import. Then select Mark Directory As and select Sources Root.
So if you go to
-> Setting -> Project:My_project -> Project Structure,
Just the directory in which the source code is available and mark it as "Sources" (You can see it on the same window). The directory with source code should turn blue. Now u can import in modules residing in same directory.
PyCharm Community/Professional 2018.2.1
I was having this problem just now and I was able to solve it in sort of a similar way that #Beatriz Fonseca and #Julie pointed out.
If you go to File -> Settings -> Project: YourProjectName -> Project Structure, you'll have a directory layout of the project you're currently working in. You'll have to go through your directories and label them as being either the Source directory for all your Source files, or as a Resource folder for files that are strictly for importing.
You'll also want to make sure that you place __init__.py files within your resource directories, or really anywhere that you want to import from, and it'll work perfectly fine.
What I tried is to source the location where my files are.
e.g. E:\git_projects\My_project\__init__.py is my location.
I went to File -> Setting -> Project:My_project -> Project Structure and added the content root to about mention place E:\git_projects\My_project
it worked for me.
Always mark as source root the directory ABOVE the import!
So if the structure is
parent_folder/src/module.py
you must put something like:
from src.module import function_inside_module
and have parent_folder marked as "source folder" in PyCharm
I was getting the error with "Add source roots to PYTHONPATH" as well. My problem was that I had two folders with the same name, like project/subproject1/thing/src and project/subproject2/thing/src and I had both of them marked as source root. When I renamed one of the "thing" folders to "thing1" (any unique name), it worked.
Maybe if PyCharm automatically adds selected source roots, it doesn't use the full path and hence mixes up folders with the same name.
my_module is a folder not a module and you can't import a folder, try moving my_mod.py to the same folder as the cool_script.py and then doimport my_mod as mm. This is because python only looks in the current directory and sys.path, and so wont find my_mod.py unless it's in the same directory
Or you can look here for an answer telling you how to import from other directories.
As to your other questions, I do not know as I do not use PyCharm.
The key confusing step that must be done is to recreate the run configuration for the source file that you're trying to execute, so that the IDE picks up the new paths.
The way that actually worked for me was to go to Run/Edit Configurations..., select the configuration for the file that you're trying to run on the left side, uncheck the "Add source roots to PYTHONPATH" box, save, and then go back and check the box and save. THEN it would work.
This can be caused when Python interpreter can't find your code. You have to mention explicitly to Python to find your code in this location.
To do so:
Go to your python console
Add sys.path.extend(['your module location']) to Python console.
In your case:
Go to your python console,
On the start, write the following code:
import sys
sys.path.extend([my module URI location])
Once you have written this statement you can run following command:
from mymodule import functions
The solution for this problem without having to Mark Directory as Source Root is to Edit Run Configurations and in Execution select the option "Redirect input from" and choose script you want to run. This works because it is then treated as if the script was run interactively in this directory. However Python will still mark the module name with an error "no module named x":
When the interpreter executes the import statement, it searches for x.py in a list of directories assembled from the following sources:
The directory from which the input script was run or the current directory if the interpreter is being run interactively
The list of directories contained in the PYTHONPATH environment variable, if it is set.
An installation-dependent list of directories configured at the time Python is installed, in my case usr/lib/python3.6 on Ubuntu.
Content roots are folders holding your project code while source roots are defined as same too. The only difference i came to understand was that the code in source roots is built before the code in the content root.
Unchecking them wouldn't affect the runtime till the point you're not making separate modules in your package which are manually connected to Django. That means if any of your files do not hold the 'from django import...' or any of the function isn't called via django, unchecking these 2 options will result in a malfunction.
Update - the problem only arises when using Virtual Environmanet, and only when controlling the project via the provided terminal. Cause the terminal still works via the default system pyhtonpath and not the virtual env. while the python django control panel works fine.
PyCharm 2021.2.4 -- March 4th, 2022
Solved it by marking the source directory as "Source". Accomplished it by
right clicking on the source directory in the Project structure on the left of the IDE. I had a code source named src/.
navigate to "Mark Directory as".
select "Source" as directory type.
In short,
src -> Mark Directory as -> Source
This can occur if you are running a python file with the same name as one of its parent directories and try to import another
e.g.
Say you have files
my_project/processing/method1/processing.py
my_project/processing/algorithms/predict.py
And in processing.py you do something like:
from my_project.processing.algorithms.predict import Predict
it will throw the error
ln -s . someProject
If you have someDirectory/someProjectDir and two files, file1.py and file2.py, and file1.py tries to import with this line
from someProjectDir import file2
It won't work, even if you have designated the someProjectDir as a source directory, and even if it shows in preferences, project, project structure menu as a content root. The only way it will work is by linking the project as show above (unix command, works in mac, not sure of use or syntax for Windows).
There seems some mechanism where Pycharm does this automatically either in checkout from version control or adding as context root, since the soft link was created by Pycharm in a dependent project. Hence, just copying the same, although the weird replication of directory is annoying and necessity is perplexing. Also in the dependency where auto created, it doesn't show as new directory under version control. Perhaps comparison of .idea files will reveal more.
try installing the missing modules using the "terminal" on pycharm
python -m pip install your-module
Pycharm 2017.1.1
Click on View->ToolBar & View->Tool Buttons
On the left pane Project would be visible, right click on it and
press Autoscroll to source
and then run your code.
This worked for me.
The answer that worked for me was indeed what OP mentions in his 2015 update: uncheck these two boxes in your Python run config:
"Add content roots to PYTHONPATH"
"Add source roots to PYTHONPATH"
I already had the run config set to use the proper venv, so PyCharm doing additional work to add things to the path was not necessary. Instead it was causing errors.