Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm tensorflow ImportError but works fine with Terminal

I made a virtualenv with tensorflow installed and I changed Python interpreter set to the location where the virtualenv is located at. When I ran the program, it gives the error:

ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory

I have the following lines written in my .bashrc file

export CUDA_HOME=/usr/local/cuda-8.0
export LD_LIBRARY_PATH=${CUDA_HOME}/lib64:$LD_LIBRARY_PATH
export PATH=${CUDA_HOME}/bin:${PATH}

Also, I found the libcudnn.so.5 file in the following path:

/usr/local/cuda-8.0/lib64 

I encountered this error before I opened PyCharm and I did manage to install the correct cudnn version. The problem is, I can use

$ source [virtualenv_path]

to active the virtualenv and run python in terminal and everything works fine, no errors shown. The program also runs without error in Pycharm's terminal but it just gives me the above error whenever I click the run button. Could there still be something wrong with the environment variable? How do I fix it?

like image 678
edhu Avatar asked Apr 29 '17 04:04

edhu


3 Answers

Unfortunately in pycharm you have to set CUDA_HOME and LD_LIBRARY_PATH inside the IDE. To do that go to the Run menu->edit configurations then choose your project. Then you click on the Environment Variables and add an entry for CUDA_HOME and LD_LIBRARY_PATH. When you have done that you can run directly from inside pycharm. I would also recommend setting the defaults to have these paths as well, so you don't have to do this for every project. Also it wouldn't hurt to make sure you have the interpreter from source [virtualenv_path] set while you are on this setting page. Let me know if you have trouble finding where to do this.

like image 87
The Puternerd Avatar answered Oct 19 '22 00:10

The Puternerd


I've read this other Stackoverflow post: Pycharm environment different than command line, and it seems that the problem is that openning PyCharm in the normal way won't preserve the bash environment. The solution is to open PyCharm from the command line and everything works now. All you need to do is to open PyCharm using charm and everything works fine now.

like image 36
edhu Avatar answered Oct 19 '22 01:10

edhu


I have searched a lot, but I didn't find the right solution for this question anywhere. I found the solution with my friends and I would like to share with you also.

If your Tensorflow works in terminal properly but doesn't work in Pycharm you have two solutions:

1- Open Pycharm from Terminal. For me:

pycharm-community

2- If you are using Unity, you need to edit .profile, because Pycharm doesn't preserve .bashrc: So, open a terminal and type:

gedit .profile

Then, add the following codes at the end of the opened file:

export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Save and exit. Now, you can work with Tensorflow in Pycharm without the necessity of opening the Pycharm form terminal.

like image 40
EhsanYaghoubi Avatar answered Oct 19 '22 02:10

EhsanYaghoubi