Different default terminals by language in VS Code - python

I am using Visual Studio Code in Windows 10 to code both in Fortran and Python. When compiling in Fortran, I use Ubuntu's WSL Bash terminal through the Windows subsystem for Linux. When running Python scripts, I use either the Powershell or the Windows command prompt.
I know that in VS Code I can have a default integrated terminal in Terminal: Select Default Shell.
Within VS Code, I have used WSL Batch for Fortran, and the Powershell for Python. My problem is that I have to manually select the terminal I want depending on the language I am working.
Is there any way of setting different default shells for different languages? So that, when compiling Fortran I could automatically get WSL Bash, and when using Python I could get the Powershell? Maybe setting a rule by file extension?

As suggested by #frode-f, if you can create separate workspaces then you specify the settings per workspace. Basically just separate the code into individual directories and then open those directories separately in VS Code. I don't know if multi-root workspaces will get you what you want since that's still a single VS Code window.
Otherwise open two terminal instances in VS Code using different shells and flip between them as necessary.

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

Arrow up / previous command in VSCode Python Interactive not working

I'm working with VSCode on an Ubuntu 18.04 machine. Everything is fine except that I can't get back to the previous command in Python Interactive Window via Arrow Up. It works in the integrated terminal though, integrated shell is /bin/bash.
I have no idea where this is coming from. I changed "keyboard.dispatch" to "keyCode", but that's not the problem. I also tried different versions of the python-extension.
Do you have any idea?
Thanks!
Not sure if this is helpful for you as an Ubuntu user, but I came across the same problem in Windows this week. In my case, the issue cropped up after I installed the Python extension in VS Code (or at least I didn't notice the issue before this). The combination of using the Python extension, Git Bash for terminal, and python virtual environment killed the up arrow feature for me. Using python outside a virtual environment works fine. My solution was to use Command Prompt as the terminal when I wanted to use a virtual environment interactively.
In my situation, although I delete all the shortcuts of UpArrow, I still can get the previous command through UpArrow in Python interactive. This means there's no way to configure this shortcut, it was built in the plugin which built in Python extension.
So it's some problem with your Python extension, but you said you have tried to install a different version of Python extension but still not work. Make sure you have deleted it completely -> delete the extension folder manually(it's under C:\Users[UserName].vscode\extensions\ms-python.python-xxx).

Visual Studio Tools for AI - Variable Explorer not loading

Installed VS Tools for AI and tried to run a python deep learning script.
The script runs well on the Anaconda Interactive provided by the IDE. Problem is with the 'Variable Explorer'. It shows an error saying "Error retrieving environment list".
The Default python environment is set to Anaconda 4.4.0.
Any solution to get the prevailing variables in the executing script through the explorer?
(The comment from Jespar is correct, so promoting it to an answer.)
That Variable Explorer belongs to R tools, so it will only work when you are using R.
There is currently no variable explorer for Python in Visual Studio, so this will not work. The two options to inspect variables are to debug a script (set a breakpoint and use the Watch/Locals windows), or use the interactive window (and the normal Jupyter-style commands, if you've enabled "IPython interactive mode" from the Python Environments window).

Spacemacs set python shell to Anaconda3 path

I have started to use Spacemacs to edit python files and also to use org-mode, but now I want to run python from Spacemacs instead of running Anaconda Prompt.
I've written this in .spacemacs file with SPC f e d:
(defun dotspacemacs/user-init ()
"Initialization function for user code.
It is called immediately after `dotspacemacs/init', before layer configuration
executes.
This function is mostly useful for variables that need to be set
before packages are loaded. If you are unsure, you should try in setting them in
`dotspacemacs/user-config' first."
(setq python-shell-interpreter "C:/Users/Mahesvara/Documents/Personal_Documents/Programs/Anaconda3/python.exe")
)
But when I try to run the script with C-c C-p it gives the following error: Searching for program: No such file or directory, python
I am not entirely familiar with Spacemacs on Windows, but here is my go at this...
Command line is not a shell
Note that anaconda prompt is not a shell, it is an command prompt interface to anaconda (see here).
Windows uses cmd. Spacemacs, Unix, and Python use shells, and are a completely different interface.
Welcome to Spacemacs
What you put in that variable shouldn't be a file path. It should be a name of a program.
Emacs has a list of places where executable are stored.
This variable is called exec-path.
Here is how to set it.
I would recommend not modifying it, but viewing it
(print exec-path)
There then needs to be a python shell in one of those paths (i.e. python, ipython, etc.).
A more experienced Windows developer may want to disagree with this. But it may be helpful to install some Babun/Cygwin shell to install python with. Especially if you are using Spacemacs. This would allow you to run in a more Unix-y environment which works better for Spacemacs. Just make sure the home directories match up.
Once you have a python shell program installed in one of the exec-path's,
You should be able to:
(setq python-shell-interpreter "python")
Change python to whatever python shell you chose.

Run a python script from the prompt in windows [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I run a python program in the Command Prompt in Windows 7?
This is a follow-up to this question: Run a python script in windows.
How would I do the equivalent of
`$ ./checksum.py <folder>
in Windows? Note, the checksum.py file starts thus:
#!/usr/bin/env python
For me, it works just to invoke the name of the script directly, e.g. > myscript.py.
if you have python installed on your system just make sure it's in the global variables.
Then you can type in "python " eg "python myscript.py abcd".
If it's not registered at global level you have to 'cd' (ChangeDir) to the location where python is installed, then run a command "python " eg:
"C:\Programs\Python>python C:\Users\User1\Desktop\MyScript.py abcd" where "C:\Programs\Python" is the current working directory.
If you want to run linux programs and commands on windows you can try MinGW or CygWin.
One potential solution to this problem, while possibly overkill, is to install Cygwin and use its environment to run the script. Of course you can just call the python command from your Windows command line (as long as it's in your PATH, as specified in autoexec.bat) followed by ./checksum.py [folder], but if you're coming from a *nix/OS X environment, you may find Cygwin makes your life simpler. Either way.
Make sure the filename extension .py is associated with the appropriate python.exe. Similarly, .pyw should be associated with pythonw.exe (this is a version of the Python interpreter that doesn't show a terminal window, suitable for use with Python GUI scripts).
The Python for Windows installer does this, so you usually won't have to mess with it unless you have multiple Python installs on your machine. If you do need to change the association, this can be done by right-clicking a .py file, choosing Properties, and clicking the Change button next to "Opens with."
Windows ignores the shebang line, so there is no way (short of Cygwin) to have different scripts use different versions of Python by changing the shebang. You could use a different extension (e.g. .py3 for Python 3 scripts) and associate that with C:\Python31\python.exe -- but that will break the script's ability to be imported as a module (Python expects the .py extension), so use it carefully. Better practice is probably to just specify the desired python.exe directly on the command line.

Categories

Resources