Visual Studio code is not loading my python interpreters - python

I've been using a python interpreter that I set in my venv the whole time for this project. Recently I changed my python interpreter which I've set as a default interpreter in my user settings like the vs docs vs code python environments describes it and also set in my JSON settings file refuses to load instead. I'm getting the default conda python interpreter with no other options.
here is my Json file:
settings.json
{
"python.defaultInterpreterPath": "C:/Users/houst/Envs/Mach/Scripts/python.exe"
}

I think you should use the \ instead of the / in the path. For safety just copy paste the path from the windows explorer.
Another Solution : Writing the path in Json should definitely work. But I prefer using the functionality vscode provides. Now come to my solution.
press ctrl+shift+p to open the search bar. now type >Python:Select Interpreter. This should allow you to select the existing interpreter or to add the interpreter path in the vscode.
I'm attaching a screen shot for your convenient.
You should keep in mind that, if you add the Python interpreted path in environment path of your system, vscode should automatically find the interpreter.

Related

Unable to change Python 2.7 to Python 3.8 on Sublime and VS code

I have been trying to change my python interpreter on sublime text and VS code and no matter what I do, both of them refuse to change. It always picks the default Python 2.7 version.
On sublime, I tried to create a new build tool, added the path to Python3 but nothing is working.
And on, VS code, even if I select Python3 from the drop-down menu, it doesn't change anything.
I changed the system's default python interpreter from 2.7 to 3.9 and set the path on bash profile. It fixed the issue for me.
I don't know about Sublime, but for VS Code, there looks to be quite a few things to verify:
Check your User Settings on whether you've set up a default interpreter for your applications
Are you using Window 10 WSL? Then you might want to check out issue 3227 for vscode-remote-release where the comment was:
So I was having this same exact issue until I read this article on a wsl tutorial. In there it says we should have "Remote - WSL" extension installed (already did). In the bottom left corner "Remote - WSL" installs a status button that has the "greater than" and "less than" symbols displayed. If you click that button and tell VSCode to open up in the remote WSL distro environment it fixed my issue where vscode was unable to find an interpreter for python.
For Sublime, you need to hit ⌘B with your script pane focused in order to use your new python3.sublime-build build system.
It looks like you are running your script within Terminus, which is fine, but you need to use the python3 command when running it from the command line, even if that command line is within Sublime.
Please check out my answer here on how to make a Python build system that accepts user input, as the Sublime "console" doesn't allow you to interact with your program. Since you already have Terminus installed, I'd recommend going down to the bottom of the answer and using the Terminus build system. That way, you won't have to deal with Terminal windows opening every time you run a build.
The terminal window uses your PATH, not the IDE settings, so manually typing out python --version isn't testing anything except the PATH variable
In order for the terminal to use the "correct" python binary you've specified, it would need to give the absolute path to it, which I believe using Command+B (using the build options) in Sublime would do. Similarly, you can use the Run/Debug Configurations in VSCode.
Or you can modify your ~/.zshrc to fix your PATH, for example installing pyenv to change the entire system Python version, or the one for the current folder

Importing package from a virtual environment in Python 3.6

I'm trying to run TensorFlow on a virtual environment that I've created and which runs Python 3.6.8 using VS Code (I know anaconda is the preferred way to go but I have to use VS Code). However, when I try to import the package into my .py file, it says that the TensorFlow module cannot be found.
My settings.json file has the following data:
{
"python.pythonPath": "C:\\Users\\user\\Desktop\\AI\\venv36\\Scripts\\python"
}
The above was implemented in accordance with this video: https://www.youtube.com/watch?v=Wuuiga0wKdQ. I know that TensorFlow has been installed since when I go to the virtual environment directory and try to import it within the CLI, it imports the module without any issues.
The TensorFlow module is located in the path: C:\Users\user\Desktop\AI\venv36\Lib\site-packages
But when I put the above path in the settings.json file, it still didn't help.
Based on the information you provide, please refer to the following:
To use python in VS Code, please make sure that the 'python' extension has been installed.
If you are using a 'Windows' system, you need to use "python.exe" when setting "python.pythonPath".
Please check whether the python environment displayed in the lower left corner of VS Code is consistent with the python environment used by the VS Code terminal.
( If they use different pythons, please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will automatically enter the python environment selected in the lower left corner of VS Code. )
More reference: Python environments in VS Code.

VScode extensions: CODE RUNNER and PYTHON PREVIEWER do not use the interpreter I select

I'm using some VSCODE extensions that I find really interesting, those are: Python Preview (https://github.com/dongli0x00/python-preview), and Code Runner (https://github.com/formulahendry/vscode-code-runner.git).
I normally let VScode select the environment for python development and it is so good, it always chooses the right one:
So the Problem here is, that, as you can see, in the code I'm using MatPlotLib and NumPy modules. And when I use either Python Preview or Code Runner, they do not detect the right interpreter, I don't know if they follow the interpreter that VScode selects automatically or if some configuration is needed in order for them to do it.
These are the captures of both extensions selecting the wrong interpreter:
PYTHON PREVIEW:
CODE RUNNER:
Well I'd just like to know if this happens due to the extensions configuration and if I can solve it somehow :)
Thanks.
Code Runner allows you to point to an interpreter as follows:
Search Settings for “executor map” and click “edit in settings.json”
Paste in this line:
"python": "$pythonPath -u $fullFileName",
So that the entire file looks like this:
{
...
"code-runner.executorMap": {
"python": "$pythonPath -u $fullFileName",
}
...
}
This way Code Runner uses the interpreter you have set in “Python Path”. (Search that in your settings.)
The Code Runner README contains other variables you may use in code-runner.executorMap, in case your Python executable is located someplace else.
PS You might also want to add interpreters for other languages, if you use any.
This is not a fix, but a workaround.
I had the same issue as you have.
I tried many methods mentioned in variour blogs as well as stackoverflow, but it seems it wont fix the issue.
Then I removed Python2 completely from my system, as I don't use Py2 for anything, to make it working.
I recommend you to remove Python2 if you dont use that.
This will fix your issue
Tried and Tested
I created a virtual environment and when i clicked the option Python: Run Current File in Python Interactive Window, the environment was activated automatically like the following screenshot shows:
However, pressing Ctrl+Shift+P to open the panel and select the option: Terminal: Create New Integrated Terminal is used more often to activate the virtual environment.
So check if you can get the above output which means you activate the virtual env successfully by pressing Ctrl+Shift+`, which is the shortcut key of creating new integrated terminal. If the environment is still not activated, press Ctrl+, to open settings.json and add
"python.terminal.activateEnvironment": true
Reference: python environment. Try this and see if the problem goes away.

Django manage.py [duplicate]

Today when I tried to run simple code on Sublime Text 3, the following message appeared:
Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640
And when I type Python in CMD, it opens the Windows Store for me to download Python 3.7. This problem started today for no good reason. I didn't change or download anything about Python and already tried reinstalling Python, and the Path environment variable is correct.
Use the Windows search bar to find "Manage app execution aliases". There should be two aliases for Python. Unselect them, and this will allow the usual Python aliases "python" and "python3". See the image below.
I think we have this problem when installing Python because in a new Windows installation the aliases are in the ON position as in image below. When turned on, Windows puts an empty or fake file named python.exe and python3.exe in the directory named %USERPROFILE%\AppData\Local\Microsoft\WindowsApps. This is the alias.
Then Microsoft put that directory at the top of the list in the "Path" environment variables.
When you enter "python" in cmd, it searches the directories listed in your "Path" environment variables page from top to bottom. So if you installed Python after a new Windows 10 install then get redirected to the Windows Store, it's because there are two python.exe's: The alias in the App Execution Alias page, and the real one wherever you installed Python. But cmd finds the App execution, alias python.exe, first because that directory is at the top of the Path.
I think the easiest solution is to just check the python.exe and python3.exe to OFF as I suggested before, which deletes the fake EXE file files. Based on this Microsoft Devblog, they stated they created this system partially for new Python users, specifically kids learning Python in school that had trouble installing it.
Creating this alias was to help kids just starting Python to install it and focus on learning to code. I think Windows probably deletes those aliases if you install Python from the Windows App Store. We are noticing that they do not get deleted if you manually install from another source.
(Also, the empty/fake python.exe is not really empty. It says 0 KB in the screenshot, but entering "start ms-windows-store:" in cmd opens the Windows App Store, so it probably just has a line with that and a way to direct it to the Python page.)
Finally, as Chipjust suggested, you can create a new alias for Python using something like DOSKEY as explained in this article for example:
How to set aliases for the command prompt in Windows
The main problem here is that the order in the path calls the windows from top to bottom, and that there is python.exe in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps which is called first if there are no other python.exes in the PATH above that line.
To ensure that the correct python.exe is called, add the Python interpreter installation folder (containing python.exe) to the PATH, above %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
Here is an example:
To get to this location, click "Start" → start typing "Env" → Select "Edit the system environment variables" → "Environment variables" button → Select the entry for "Path" in the upper list → Click "Edit".
Python components should be at the top, as in step 5. If not, move them up by pressing the button in step 6.
If the Python interpreter is already installed, then go to Apps & features from settings, select Python, and then select modify.
Again select modify and select Next:
Then this window will appear:
Select "add Python to environment variable" and click on the install button. Then again go to apps & features, click modify and click Repair.
Now go to CMD and type Python.
Problem solved.
This is a PowerShell script that does the magic.
Remove-Item $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps\python*.exe
I had problems with this as well, where Windows didn't recognize Python or Anaconda in a double click or cmd (command) prompt.
Problem: unable to import libraries in "python" cmd in Windows. Instead the Windows "python" cmd took users somewhere they don't want to go.
Problem Cause: In Windows "Environmental Variables", Windows adds a python.exe and python3.exe (I don't know where these link to) in the "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps" directory.
Solution: I tried deleting the python*.exe files in the WindowsApp directory, but Windows wouldn't allow it, so I opened a command prompt in the "%USERPROFILE%\AppData\Local\Microsoft\WindowsApps" directory and typed:
del python.exe
del python3.exe
Then I created an environment variable linking to the installed Python interpreter link. In my case, it was C:\Anaconda3; C:\Anaconda3\Scripts, and some others for good measure.
Because this is a common issue and this appears to be the canonical question, I want to try to give a complete overview of The Python 3.7 Windows Store Fiasco (TM).
Why is it possible for this to happen?
A convergence of two things: the previous introduction of the Python Launcher for Windows in 2011 (hereafter py), and a Windows 10 update in May 2019 that was apparently intended to make installing Python easier for Windows users.
Oops. Turns out that installation path isn't great; it bypasses the "lengthy" setup wizard... which contains some options that some users find very useful. It caused other issues, too. Not to mention that it just works in slightly non-standard ways, has limitations on file system access because it's a Store app, initially couldn't itself be launched by py....
Okay, but why do those factors result in the problem?
Since the introduction of py, by default, Windows Python installers do not add the new Python install to the PATH. Why? Because the entire point of py is that it uses its own logic to find a Python installation, based on some combination of command-line switches and possibly the source file's own shebang line. Now your source files can be associated with py instead of any particular python.exe, and you can get Linux-like behaviour when double-clicking a file. Meanwhile, by running py at the command line, you have easy access to whatever you need, and you don't have to think about which version of Python was installed most recently. So there's seemingly no good reason to put any of those Python installations on the PATH. It only risks confusing you when, for example, the most recently installed version isn't the most up-to-date one. Right?
In the update, Windows 10 put a "python.exe" into a Windows Apps-related folder, which is a wrapper app to open a Microsoft Store link. The idea is that it's on the PATH, but way near the end; so if you have an installed Python, it gets used, and otherwise the wrapper is invoked and helpfully prompts you to install Python - so that you can actually run that random, totally trusted .py file your friend sent you on Discord.
And it would work perfectly, if your installed Python were on the PATH.
Oops.
(But, you know, py had been introduced around 8 years prior. You'd think someone at Microsoft would have been aware of the potential issue. Maybe instead of a special shortcut link, they could have made an actual script that checks for the presence of C:\Windows\py.exe or something.)
So what are my options?
You can check the option to add new Python versions to the PATH when you install them, and deal with the fact that python at the command line means a specific one of them. If you need to change that, you can manually tweak your PATH variable.
You can just manually tweak the PATH variable after the fact. (or "Modify" a Python installation to fix it.) This is covered in several other answers.
Independently of that, you can disable the wrappers, as shown in the top answer. You should probably do this anyway; seeing python fail at the command line is less aggravating than dealing with a random GUI window popping up and offering to install something for you, especially when you know you have it already.
If you want to keep the PATH empty, consider using virtual environments for your projects. Whenever a virtual environment is active, the PATH is temporarily modified such that python means the Python installation of that environment. It's quite convenient, really.
You might be able to tell your IDEs to use py instead of a specific Python installation, and it might even be helpful to do so. I don't know. I don't use one.
As a person who does Python development in Sublime Text, I know you said the Python interpreter path was correct, but when you install the Python interpreter make sure to tick the option to add Python to PATH.
I had the same issue back in the day till I did this.
You can manually add Python to the Windows path by doing this:
Start the Run box and enter sysdm.cpl
Go to the Advanced tab and click the Environment Variables button
Now you’ll need to locate the relevant Python paths
Here is how a Python application path looks like:
And this is how a Python Scripts path looks like:
Select the Path variable, press edit and add both paths (Python application and Python Scripts)
If it doesn't show, press new instead of edit and fill the New User Variable box
This is how my Variable value looks like:
C:\Users\Ron\AppData\Local\Programs\Python\Python37-32;C:\Users\Ron\AppData\Local\Programs\Python\Python37-32\Scripts
That’s it! You just added Python to the Windows Path.
Source: Data To Fish
So, I had the same problem.
My answer was to add python to PATH not only for User variables, it was there, but also for System variables. And now everything works.
So, I've got the same problem in VENV.
I solved it by typing >> python**.exe**
But to add every time .exe a little bit nervous.
Also try to type python.exe in cmd.
This is an additional note for anyone using pyenv-win: after turning off the application execution aliases, run pyenv rehash. You may also need to close/reopen your CMD or PowerShell window.
I have put together a powershell snippet, which reorders WindowsApps and Python folder so that Python is first. Run this as Admin:
$appsFld="$env:USERPROFILE\AppData\Local\Microsoft\WindowsApps";
$pyPath=(Resolve-Path "$env:USERPROFILE\AppData\Local\Programs\Python\Python*\")
$Env:Path = (($Env:Path.Split(';') | Where-Object { $_ -ne "$appsFld" }) -join ';');
$Env:Path = (($Env:Path.Split(';') | Where-Object { $_ -ne "$pyPath" }) -join ';');
$Env:Path += ";$pyPath";
$Env:Path +=";$appsFld";
[Environment]::SetEnvironmentVariable("PATH", "$Env:Path", "Machine")
Then I can run python just fine:
> python
Python 3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
Adding a bit to the question. Even when I typed pip freeze, it wasn't showing anything.
Here's what I did:
There were multiple instances for the Python application in /AppData/Local/Microsoft/WindowApps.
I deleted those and then it worked.

VSCode / Virtual Studio Code: Unable to Load Conda Environment in VSCode Terminal

I'm attempting to switch to VScode, to use the built-in terminal to interact with Anaconda, and know VScode supports Anaconda because it ships with Anaconda.
From the documentation, and various tutorials/videos, I see that the conda/virtual environments are recognized (seemingly natively). And yet I cannot seem to figure it out.
With the python script I want to run open in VScode, I ensure that I am using the correct interpreter/environment as follows:
ctrl+shift+p to load Command Pallette
Python:Select Interpreter
C:...\Anaconda3\envs\p3audio\python.exe
At the bottom of the window, the environment also appears to be pointing to the environment as I loaded the p3audio environment to use python 3.7 and this is what is showing ('Anaconda Python 3.7.0') (The top Python in top level anaconda is shipped with 3.6 I believe)
When I try to run the python file by 'Run Active File' from the Terminal Menu, it automatically loads powershell and executes this into the terminal:
PS C:...\Anaconda3\envs> c:...\Anaconda3\envs\Projects\p3audo_projects\my_script.py
(the second path is in yellow)
If I try to activate the conda environment manually from the powershell terminal, it doesn't recognize it.
I can run the script fine from the anaconda prompt terminal.
I think it may be a path issue, but all of the documentation/resources I used suggest I shouldn't have the issue if I didn't select (tick the box) for add path on install. I checked my environment variable path and don't have anaconda on it.
I am wondering if it has to do with the 'you can change the default terminal shell by selecting the customize button' that loads in the terminal window. This leads me to believe that I have to manually add the conda terminal path somewhere. But this surprises me because, vscode shipped with anaconda, and I see no mention of this in the documentation.
I am obviously a newbie at this, and have suffered through environment variable/path issues many times. But I can't figure this out.
Any help would be greatly appreciated.
Thanks
For anyone struggling with this, I'll tell you what worked for me. This was after four hours of trying to get Python to run in PyCharm and VS Code.
Basically dldatacowboy has the answer, but the argument for terminal.integrated.shellArgs.windows needs to be reformatted (maybe only if you're on a Windows machine?). There should be no space before the "K", and the backspaces need to be double backspaces. So my settings in my settings.json file look like so:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K","C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat C:\\ProgramData\\Anaconda3"]
Make sure to change the path to whatever comes up when you follow dldatacowboy, but then change the backslashes to double backslashes.
For people working in Mac OS.
In order to setup the integrated terminal in VS Code to use the active Anaconda Environment's Python by default, you will need to modify your User or Workspace Setting's 'settings.json' with the following line:
"terminal.integrated.shellArgs.osx": [],
For reference: this closed issue addressed the topic.
Ok I figured it out. VSCode is awesome.
Go to Settings (ctrl+,)
Decide if you want this custom terminal to be applied at the User Settings or Workspace Settings, and select that (located just below the settings search bar)
Open settings.json (don't know shortcut key yet, it is the three-dot menu located to the far right on the same row as the User Settings and Workspace Settings tabs.
Locate "terminal.integrated.shell.windows" in the Default User Settings (left panel), again you can enter it into the search bar to locate it.
Copy it over to the User Settings/Workspace Settings (depending on what you selected or now decide to select) (in the right panel)
e.g. copy:
"terminal.integrated.shell.windows": "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe",into json array on the right
Edit the path to equal your cmd executable path
e.g. "terminal.integrated.shell.windows":
"C:\Windows\System32\cmd.exe",
You need to create the argument, which is what I was confused about:
Go to start menu in windows, locate the Anaconda Prompt Open File Location with right click, inspect properties and see Target Location Path. Don't worry about the first string, copy from after the cmd.exe to the end. Use this for the "terminal.integrated.shellArgs.windows"
e.g. "terminal.integrated.shellArgs.windows": [
"/K", "C:\Users\User-1\Anaconda3\Scripts\activate.bat C:\Users\User-1\Anaconda3"
Make sure you separate with a comma and properly contain strings with all four quotations. Save Close vscode. load it up and you are good to go.
Hope this helps someone! = )
With the recent conda release (4.6.1) Conda can now be initialized in Powershell and can now be integrated in VSCode cleanly. I made a stackoverflow post here
Please use the following settings in vs code and it ought to work, provided your conda is installed appropriately. Also, please make modifications according to your environment.
settings for powershell is mentioned below
{
"workbench.startupEditor": "newUntitledFile",
"editor.wordWrap": "on",
"editor.wordWrapColumn": 100,
"terminal.integrated.shell.windows": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
"-ExecutionPolicy", "ByPass", "-NoExit", "-Command", "&",
"'C:\\Users\\kpache\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\kpache\\Anaconda3'"
// "/K", "C:\\Users\\kpache\\Anaconda3\\Scripts\\activate.bat", "C:\\Users\\kpache\\Anaconda3"
],
"terminal.external.windowsExec": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"workbench.colorTheme": "Default Light+"
}
It looks like you've been mislead by the title of this article.
If you read the article, it says Anaconda comes with Visual Studio Code included. So it would be easier to setup if you download and install the Anaconda setup.
If you look at the article above, it shows in one of the installation steps, the option to install VS Code.
The following is the download link:
Sharing what worked for me on MacOS.
For latest versions of Anaconda (v 2019.10 at the time of this writing) add this line to your .bash_profile:
export PATH="/opt/anaconda3/bin:$PATH"
And for older versions it may look like:
export PATH="/anaconda3/bin:$PATH"
Hope that helps someone.

Categories

Resources