VSCode terminal is using the wrong python version - python-3.x

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.

Related

How to get vscode to show my python venv listed in the interpreters list?

I use vscode for python dev on mac. in one of my projects I don't see the venv python listed in the "Select Interpreter" to select. When I use the "find" option to go to the venv/bin/python and select it it'd revert back and doesn't seem to have any effect.
How do I get vscode to show venv?
I'm using macOS Catalina
vscode 1.54.3
I found the fix for this:
after reading this:
https://github.com/microsoft/vscode-python/wiki/AB-Experiments
I added "python.experiments.enabled": false to
~/Library/Application\ Support/Code/User/settings.json
and it fixed the issue.
hopefully it'll help other folks who run into this.

How to resolve 'Import "django.contrib" could not be resolved from source' in VS Code?

This error suddenly came up when I created a new Django project. I used pip to install all the packages in the virtual environment.
I had exactly the same problem and there are several solutions out there, but only #Neha's answer has brought me to the point. The problem here is really simple. VS Code uses your "main" Python interpreter, whereas you should use the one in your virtual environment for Django.
In other words, I have my main Python installation here:
c:\users\yourName\appdata\local\programs\python\python39.
And my VS Code has pointed to exactly this interpreter. But my Django was installed in this path:
c:\users\yourName\source\vscode-repos\basic-app\backend-service\venvs\lib\site-packages.
In order to solve it click on the
VS Code bar with Python Interpreter, you will then see a pop-up window. Choose + Enter interpreter path... then Find.... It will open File Explorer for you. Go to your virtual environment folder, where you have your Django installed (in my case it the vscode-repos\basic-app\backend-service\venvs folder), inside of it go to the Scripts folder and pick python.exe, like this. And you are good to go!
Actually all of the answers were right , but what worked for me is:
Type pip show Django in vsc terminal
Go to the path of intallation mentioned there
It will be inside "lib" by default..go back to scripts
Inside the scripts , there will be python .exe app
Choose this as your interpreter (as per above answers)
Select the Python interpreter in which you have created the virtual environment. I had the same error and this solution worked.
Below is a screenshot that you can refer to.
If you are using VScode, click the python version on the bottom left corner and select the venv where you have installed the packages.
For anyone who can't see the virtual environment then click on the python version below right corner of vscode,click on Enter Interpretor Path,click find,chose the folder that you are working,click on virtual environment name ,then click bin and select python version and the warning will be gone.
In my case (on MacOS) the problem was caused by creating venv using default arguments.
I don't have Scripts directory, but I've got bin inside. The python3 -m venv my_venv command was creating symlinks to python and python3 executables which was confusing for the VSCode. After removing and creating again the venv with below command everything started to work
python3 -m venv --upgrade-deps --copies my_venv
Go to the Python(Extentions) below your screen Like this then select interpreter tab will pop up, then select C:\Python39\python.exe Like this.Hope it helps
What worked for me was to install django within my virtualenv. All other settings were correct in Visual Studio Code.

How to reconfigure VSCode for python

I had configured VSCode for python, it was working fine, linter , autopep all the things were working fine. After some days I installed Anaconda, from here problems started, after this I got error that Select Python Interpreter at the bottom of VSCode, I added python path to the settings,then it works fine but "Select Python Interpreter" error was still coming. Some days before I installed Javascript extension, after this linter,autopep nothing is working,only using python command line python program is getting run.
I removed javascript extension,again installed VSCode, now the things are worst,nothing is working now.Please anyone help me to how to reconfigure VSCode to work properly.Even I removed Anaconda installation too, still problems not solved. I searched online but did not get any proper inforamtion.
I had a similar issue, I think you have both Python 3.7 and Anaconda installed separately, Use any one them , remove the other. When both of them is together it was throwing lot of issues. I would prefer to remove Anaconda(pure personal choice).If you don't have much customized settings in VScode, delete its user configuration files and set it up again. it will work

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

Anaconda -- conda create results in "Failed to create process"

I recently installed Anaconda onto my laptop and I am trying to install a conda environment called pydecal that uses python 3.5. I have tried this in CMD as well as in Anaconda Prompt. I did not enter Python when Below is my code:
conda create --name pydecal python=3.5
I am getting a "Failed to create process." message every time I try to run the command. Anaconda installed fine in 64-bit. I am on a clean install of Windows 10. I have no other instances of Python on my computer whatsoever. I have tried running CMD and Anaconda Prompt as an administrator. I have restarted my laptop several times. Regardless, I run into the same problem. Any ideas on what is going wrong?
Did you install it on the primary drive (C:)?
My conda did not work either. I have just uninstalled Anaconda3 which was formerly installed in a secondary drive (E: rather than C:).
After installing Anaconda3 on the primary drive (C:), conda seems to work fine.
I solved it by making this way:
1: Install Anaconda in a directory structure not containing spaces.
2: Modify system environments to not include %PATH% in the user section.
I found that 'echo %PATH%' in a prompt resulted in the system defined paths, were shown twice.
3: Moved all paths containing %SYSTEMROOT% to the top search order. This was to assure the need to search obscure named paths was not searched by the 'activate' command.
Let me know if this solves your problem.
Br Michael
I solved it this way:
Opening the "Anaconda Prompt" Console Link that Anaconda creates in the start menu. As conda appeared to work fine there I copied the predefined envinronment variables for that prompt :
If you check the link, it executes this:
%windir%\System32\cmd.exe "/K" C:\tools\Anaconda3\Scripts\activate.bat C:\tools\Anaconda3
and that file refers to C:\tools\Anaconda3\condabin\conda.bat
So finally the path is specified there as :
C:\tools\Anaconda3;C:\tools\Anaconda3\Library\mingw-w64\bin;C:\tools\Anaconda3\Library\usr\bin;C:\tools\Anaconda3\Library\bin;C:\tools\Anaconda3\Scripts;C:\tools\Anaconda3\bin;C:\tools\Anaconda3\condabin;
I added that to the Windows System Environment variables and it now works properly on git bash, and other shells.
pip install pathlib
that is what i did now everything works

Resources