Pycharm tensorflow ImportError but works fine with Terminal - linux

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?

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.

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.

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.

Related

VSCode terminal is using the wrong python version

I have a new mac with python 3 installed from homebrew and visual studio code. I am not a python person by default I tend to use java but wanted to try a project for fun.
Just my terminal in VSCode has a system install of python 3.8 version that I am not using I want to use my brew install version as shown in screen shots. if I do echo $path it shows correct, my mac terminal shows correct and my VSCode python plugin has the correct version showing. I have rebooted several times in case it was cached.
I want to use the brew version 3.9.7. My project is failing to load imports such as "import requests" I assume because of this. The import fail error is.
Import "requests" could not be resolved from sourcePylancereportMissingModuleSource
I have installed and uninstalled requests many times always through VSCode command line.
Perhaps the values in $PATH of VS Code is in a different order than that of Terminal.app.
See https://github.com/microsoft/vscode/issues/70248 for more informations.
You can simply set "terminal.integrated.inheritEnv": false. This works well for me.
Try putting the following in settings.json:
"terminal.integrated.env.osx": {
"PATH": ""
}
Save the file, close any terminal window and restart VS Code.
You can read more here.
Go into VS Code settings: Code->Preferences->Settings. Type the following in the search box:
terminal.integrated.inheritEnv": false
Uncheck the option box beside the setting "whether new shells should inherit their environment from VS Code..." to set it to False.
Restart VS code and try again, you should have the same version of python as on your standard terminal.

How can I change the default python packages accesible to git in vscode?

I am working on a commit-msg git hook. Everything works except importing of specific modules. The bad behaviour is specific to the use of the vs-code GUI git bar (little check mark at the top of the source control panel) with pip installed modules. If I use git commit -m "some message" through the terminal, everything works perfectly.
But, when I use the vs-code GUI, I get an import error.
Thinking it may be an environment issue, I ensured that the module needed was available globally by pip installing the needed module to my machine outside of any virtual environment. It didn't help.
To see what's going on, I wiped my hook and replaced it with nothing more than help("modules") to see if my modules were indeed there. They were not. So I ran
import os
dir_path = os.path.dirname(os.path.abspath(__file__))
print(dir_path)
This printed my path to the file being ran and was the path expected.
I opened a new terminal outside of vs-code with no virtual environment enabled and ran python3 path/to/file to see if my module would show up there. It did.
Additionally, it is there if I run it in the vs-code terminal as well. The only place it seems to be missing is wherever the vs-code git GUI pulls it's package list from.
Where does vs-code git get it python package list and how can I ensure that either; 1) it points to my normal python package list or 2) I can add the needed modules into it's path?
Thanks
EDIT: Thanks to the intelligent suggestion of John Szakmeister left in the comments, I have configured my hook to call env. I then initiated the hook through the terminal and through the GUI to compare.
Using the GUI adds a few variables, mostly for logging purposes and error handling. But there are a couple that I don't quite know what they do. APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=true and
ELECTRON_RUN_AS_NODE=1
However, being that it works in terminal but not GUI, I have to assume that the added variables are not the culprit. So, instead I look at what is different from one to the other.
Outside of the additional VS-Code specific variables, there really isn't very much that is coming up different.
Shell level is 2 through terminal but only 1 through the GUI (interesting...)
Terminal has extra variables TERM=xterm-256color and TERM_PROGRAM_VERSION=1.46.0 that aren't present in GUI env.
Additionally, there are some colors that are set in terminal but not the GUI.
Lastly, there is one variable that is in both that is different. GUI gives _=/usr/share/code/code while terminal yields _=/usr/bin/git. I assume that these refer to what program initiated the terminal but I haven't been able to verify that.
That's about it. PATH is the same for both, as is SHELL, as are just about all of the GIT_anything variables (outside of the fact that the GUI sets a pager that the terminal does not)

Mujoco_py missing path to LD_LIBRARY_PATH, Ubuntu 16.04

I am trying to get mujoco_py running. When I do
import mujoco_py
I get this error:
Exception:
Missing path to your environment variable.
Current values LD_LIBRARY_PATH=
Please add following line to .bashrc:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/jonah/.mujoco/mjpro150/bin
I have added the above line to both /etc/skel/.bashrc and ~/.bashrc. If I run
echo $LD_LIBRARY_PATH
I get
/home/jonah/.mujoco/mjpro150/bin/
My .mujoco folder includes mjkey.txt and the mjpro150 folder. I can run ./simulate successfully, so I have a feeling that this is some kind of mujoco_py specific bug.
Which program do you use to import mujoco?
I had a similar issue using mujoco_py with PyCharm Community 2018.1. A workaround was to launch PyCharm from the terminal instead of using the launcher icon. Maybe it could help with your issue too.
Otherwise you could try adding the LD_LIBRARY_PATH to ~/.profile instead of ~/.bashrc, as proposed in this answer here: https://askubuntu.com/questions/1022836/python-not-recognizing-ld-library-path/1022913#1022913
You can try to reinstall Pycharm for the newest version.
After you save the .bashrc file your want execute this code.
source ~/.bashrc
Now link is updated.
Please check the user which you run the code with. The mismatch user will cause this problem. There is the checklist may help you:
Don’t use ‘sudo’ to run the code;
Don’t use ‘sudo’ or virtual environment (e.g., anaconda) to run Pycharm (If you run the code in Pycharm).

Spyder external system terminal does not work (Python3.6)

I am not sure if this is a right place to ask this type of questions. I am a python beginner or programmer overall at this point. I am using Spyder to use python 3.6 (via Anaconda). I wrote a code that works fine when I run it in the current Ipython console. But I really need to run it in an external system terminal. In order to do so, I chose the following path: Run-> configuration per file -> execute in an external system terminal. That has been working fine. But now it refuses to work!
I validated that there is nothing wrong with my code by running something simple and saw that running via external system terminal does not work.
So far I deleted Anaconda and re-installed it. Could someone suggest what I should be looking for to diagnose the problem and fix it?
Thanks!
I had the same problem and noticed that unless the Working directory settings is set to "The directory of the file being executed", it won't work.
You can change it in "Run" > "Run configuration per file".

How to set a windows env variable from cygwin .bashrc?

Background: I am trying to run a windows-installed version of python from within cygwin, rather than the cygwin version (some packages I am using prefer the non-cygwin version). The following post got me up and running: Using Windows Python from Cygwin. However, I am now having an issue.
Problem: The PYTHONPATH variable does not seem to be working for me. I try "import module1" when I am using python in an arbitrary directory. module1 is located here: c:\cygwin\home\mcsefl\testFolder\module1.py. And my PYTHONPATH variable is set in my .bashrc file in the following way: "export PYTHONPATH=$PYTHONPATH:/home/mcsefl/testFolder". However I still get a "No module named..." error.
Alternate tries: Since I am running the Windows Python, I thought perhaps python did not care about the cygwin environment variables (not sure if this is true..?) So, I also tried to set the windows PYTHONPATH env variable in the control panel of windows with no luck as well.
Thanks in advance
I deleted the line in my .bashrc file that tried to set the PYTHONPATH variable and just let it be set by Windows. This corrects the problem.
Side note: The title for this Question is a misnomer now that I re-read it. My issue was resolved without addressing that overall question

Resources