21

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?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247
  • 1
    Is PyCharm running your script using the same `python` as your terminal? That would be the first look. Make sure the run configuration is using the correct interpreter. Run > Edit Configurations. Select your project, then under the Project Interpreter, make sure you have your anaconda python selected. – Josh J Nov 19 '15 at 20:10
  • Yes -- I have Anaconda Python selected for my PyCharm project, and also the interpreter in my terminal... – Karnivaurus Nov 19 '15 at 20:19
  • I don't know then. http://stackoverflow.com/questions/28390961/using-anaconda-within-pycharm and http://stackoverflow.com/questions/33646541/tensorflow-and-anaconda-on-ubuntu could help you with further troubleshooting. – Josh J Nov 19 '15 at 20:46

6 Answers6

36

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

ktx
  • 476
  • 4
  • 3
  • 2
    I found that calling predefined environment variables within this dialog didn't work. I needed to use the full path when defining these environment variables. – norman_h Dec 01 '16 at 21:46
  • I found this solution worked best for me when installing TensorFlow in an Anaconda environment and using PyCharm as my IDE. Thank you. – Phil Glau Feb 17 '17 at 04:54
14

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
Laizer
  • 5,932
  • 7
  • 46
  • 73
  • Yes, I followed the official CUDA installation instructions. The directory at /usr/local/cuda-7.0/lib64` is actually a symbolic link to `/usr/local/cuda-7.0/targets/x86_64-linux/lib`. I'm not sure why that link was made, but I didn't do it manually. Typing in `env` gives me: `LD_LIBRARY_PATH=/home/karnivaurus/Libraries/CUDNN-7.0/lib64:/usr/local/cuda-7.0/targets/x86_64-linux/lib:/usr/local/cuda-7.0/lib64`. So the required directory is there (hence why my script works when running from the terminal), but it seems that PyCharm does not use this environment variable. – Karnivaurus Nov 19 '15 at 21:15
  • 2
    Just on a lark here, are you running pycharm from an icon on the desktop? Have you tried invoking it from a shell that has all of the environment variables set correctly? – Laizer Nov 19 '15 at 21:26
  • Yes, I was running PyCharm from an icon...if I try running it from a shell, then the script works! But I don't want to have to have to do that...! There must be a way for PyCharm to load these same environment variables... – Karnivaurus Nov 19 '15 at 21:32
  • 1
    Nice! So you can write a shell script that sets the environment and invokes pycharm, and then put a link to that shell script on your desktop, or alternatively you can set the environment for an icon-invoked program using a method like this: http://askubuntu.com/a/144971/471356 – Laizer Nov 19 '15 at 21:36
  • 1
    It did solve the issue -- thanks -- although I am still assuming there is a solution to set the library paths within PyCharm. Surely this is a standard requirement...! – Karnivaurus Nov 20 '15 at 12:45
11

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. enter image description here

John
  • 1,263
  • 2
  • 15
  • 26
  • This should be the accepted answer. This appears to have been similar to the solution I was searching for. It looks like the issue I was facing was that I was attempting to call the predefined Environment Variable $CUDA_HOME/lib from within PyCharms Configuration Setting dialog. When I changed to the full /usr/local/cuda/lib everything started to work. – norman_h Dec 01 '16 at 21:47
  • Agreed, that this seems like the actual answer. Also setting CUDA_HOME to /usr/local/cuda is also a good idea in the environments section of pycharm. – The Puternerd Apr 24 '17 at 22:52
3

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

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
风间月
  • 141
  • 1
  • 5
2

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.

Community
  • 1
  • 1
Seb
  • 21
  • 2
0

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"
David Soroko
  • 8,521
  • 2
  • 39
  • 51