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.
Related
[Mac OS] Up until yesterday, my python files were executing with whatever environment I had activated. After that, I explored python debugging and how it could be used with the Conda environment. It worked like a charm.
Today, I got a notification in VSC saying something along the lines of "python.PythonPath in settings.json ... default...". Because I was in a hurry, I didn't pay attention to it. Now, when I activate conda environment X, it shows up in the terminal that X is activated, when I use "conda list" it lists the correct modules installed in X, and in VSC's status bar, it shows that I am using X too.
Unfortunately, when I go running the python code, it simply uses "python - u ", and that does not happen to be X.
Could someone please tell me what's wrong? If there's some information that I haven't provided, please feel free to let me know about it and I'll update the question accordingly.
Do you mean the VSCode works like the first picture and you want it can run in the terminal like the second picture?
The first picture was running by Code Runner, the second picture was running by Python Extension. You can choose it like this:
If I am misunderstanding your problem, please point it out and provide the related info.
You can modify settings.json to use the specific conda environment python interpreter. Or you can set the interpreter manually. \
Writing settings.json:
add the line in settings.json :
"python.pythonPath": "/home/python36/python", // the path of python
// interpreter in your desired conda environment.
Or In the editor to the bottom left corner, you should see something like below
Click it, you should see a prompt opening above, like
Select the interpreter and you're good to go.
I am using VS-Code and anaconda environment for python interpreter. I select the exact anaconda base environment by ctrl + shift + ` and it also reflects in the downside panel of vscode. But, when I checked the python version it shows my system's default python environment 3.7.9. If you see the below screenshot than, the anaconda environment is with 3.8.3.
Please give me solution, Thank you.
Changing the version in VSCode does not change the the instance that your PS instance will use. Try doing where python to see where the V3.7.9 that your PS instance is picking up is. Then remove that version from the environment variables and add the path to the V3.8.3 instead.
Additionally you can do: To forcefully use v3.8.3
Specify python version in command
py -3.8 <command>
OR set PY_PYTHON environment variable to set which version to use.
Take a look at this for further help Python docs
after changing the environment, you can restart the vs code again. it might be changed now. if not, then try changing now againg by clicking the interpreter name which is displayed on left bottom of the vscode window
For those tried these steps and achieved nothing:
select different interpreter
reboot VScode
reinstall VScode Python extension and delete its folders
Probably you are working in the workspace and not in folder. You probably set interpreter at workspace level, that can't be used in one of the folders of the workspace. Try to open your folder separately from the workspace and select interpreter you want. This worked for me.
To check & change vs code interpreter:
In top left menu bar Click view
In the dropdown menu, Click Command Palette
Click Python: Select Interpreter
Choose & Click on your desired Interpreter
Another way to be sure to use anconda interpreter, open anaconda navigator and launch vs code from there.
original vs code How-To
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.
I installed python virtual env. I use vs code. I imported scrapy in my code and vs code doesn't recognize the module. Actually, it works well when I run it. scrapy crawl tester
Just, vs code shows red underlines that mean "Unable to import scrapy" So this is just vs code issue, not venv or scrapy package install issue.
This code works well and actuallay, scrapy is imported without any issue. This is just vs code issue. Thank you.
You might be able to solve your problem by using CTRL+Shift+P to add "Python: Select Interpreter" to your project.
If you created a virtual environment and activated it as well
type which python on macOS/Linux,
type where python on Windows,
inside the activated terminal session.
(env) userpc#pc:~$ which python
/home/userpc/Desktop/foldername/env/bin/python
In VSCode press Ctrl+Shift+P, under >Select Interpreter paste the location you receive using the former command.
Once done, restart VSCode.
VSCode will also ask you to install pylint in your environment and if it doesn't you can do the same by activating you environment and typing
pip install pylint
inside the activated terminal session.
Ran into the same problem - selected the correct Python interpreter in VS Code, pip installed all the desired Python libraries but import is still underlined in VS Code.
What you need to do
What worked for me is to make sure that the linter that you are using in this VS Code instance (I was using pylint) is from the bin folder of the virtual environment, not somewhere else.
How you can do it
I'll use the absolute path to the desired virtual environment /User/ProjectFolder/env as an example.
To check that you meet the conditions stated in What you need to do, toggle the settings.json file in VS Code by pressing ⌘ + ,:
Make sure that the correct Python interpreter is selected. i.e. "python.pythonPath":/User/ProjectFolder/env/bin/python3.
Make sure that the linter (e.g. pylint) is located in that bin folder, not anywhere else. i.e "python.linting.pylintPath":/User/ProjectFolder/env/bin/pylint, NOT something like "python.linting.pylintPath":/usr/local/bin/pylint.
This means that you have to install your desired linter in the virtual environment.
Hope this helps.
I tried #cleon-w's answer which worked for me. (Thanks Cleon)
I was using pyenv on my Mac M1 (Big Sur) with vscode. vscode could not resolve the
paths to python that the pyenv provides, so I looked within the pyenv scripts to find the absolute paths
to the underlying python and the pylint files.
As #Cleon W says I ensured pylint and python are in the same bin directory.
Then edit VS Code settings.json to point to them directly (bypass .pyenv) and the imports could be found.
settings.json (vscode)
"python.defaultInterpreterPath": "/Users/USERNAME/.pyenv/shims/python",
"python.pythonPath": "/Users/USERNAME/.pyenv/versions/3.9.1/bin/python",
I have similar problem with Django. What solved my problem was:
create .vscode/settings.json file inside root dir for project.
add this json {"python.defaultInterpreterPath": "path to bin directory in root dir for project"}
Ctrl-Shif-P -> Python: Select Interpreter -> select one that said: Use Python from python.defaultInterpreterPath. If it's not in the list, hit refresh (circled refresh button on the top of settings box).
I am getting a red message every time I use the console from the debugger and try to see the output of a pandas object
"tput: terminal attributes: No such device or address"
I am using PyCharm 2016.3.2 and according to me I didn't update anything - it just started to appear all of a sudden. I attach a screenshot of the debugger with info on my ipython and debugger versions. Would appreciate your help to understand how to get rid of this message. It seems to appear both in the Python Console and Debugger Console, but not in the PyCharm Terminal. Thanks.
There is a workaround inside PyCharm itself without affecting the rest of the system:
You can hard code PATH as part of Environment set up under this menu:
Click menu Run -> Edit/Configurations...
when the configuration window opens, click on Defaults -> Python.
On the right side under Environment Variables hard code PATH without git/cygwin. See below how to get that path from cmd prompt
import os, re; print ';'.join([p for p in os.getenv('PATH').split(';')
if not re.search('Git', p)])
This PATH variable seems to overwrite system's path variable.
Image of these steps overlaid in PyCharm.
Another workaround is to simply move tput to a different cygwin PATH directory,
e.g. if tput is in ~/git-for-windows/usr/bin, and the same directory is in the windows PATH, moving tput.exe to ~/git-for-windows/usr/local/bin will fix the issue in pycharm and still work within cygwin
Have you installed git or cygwin recently? If so, try to delete git or cygwin related paths from system environment variable PATH. If had the same problem as you and now have fixed it by doing so.
Just to complement the answer from H.J. Liu, if you are unable to delete cygwin from the system PATH, then it is probably in the user PATH. Please see this related post Unable to remove cygwin from PATH environment variable
additional for Anaconda2 Users, you will find tput.exe in following two path:
Anaconda2\Library\usr\bin
Anaconda2\pkgs\git-xxx\Library\usr\bin
mv them to xxx\usr\local\bin
My colleague had the same error. It occurred only for pandas.
We resolved it by changing the project interpreter in PyCharm to the anaconda one, where he previously set up everything else. If for example, you use Anaconda navigator, then all the tput paths are set up there. (Maybe the settings got lost when downloading a new PyCharm version.)
I'm sharing this to give people some more ideas where to look for, not to get too focussed on cygwin.