How can I run a file in VSCode in the Command Prompt - python

I do a lot of Python development and I need my files to open directly in Command Prompt when I run them. Is there a way I can set up VSCode to run the current file in Command Prompt instead of the integrated terminal?
Thanks.

In VS Code, its internal terminal is an integrated terminal from the system, and for Windows systems, it uses the Powershell terminal by default.
For use the "cmd" terminal inside VS Code, we can use the settings in "settings.json":
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
If you want to run code outside of VS Code, you can use it in "launch.json":
"console": "externalTerminal",
Then click F5 to debug the code:
Reference: Integrated Terminal in VS code.

Find where your program is saved either by opening the save position in visual studio code or you can try the default save position here C:\users{username}\AppData\Local\Programs\Microsoft VS Code (You may have changed this) In this directory then find your program you want to run if you made a virtual environment make sure to enable it with cd venv/Scripts/activate.bat (Windows) or . venv/bin/activate (Linux) these will open your venv then to actually run your program use python <FileName>.py
Your installations may be slightly different from the standard so please ask if you have any difficulty.

Related

Is it necessary to run activate command after activating virtual environment using pipenv shell command?

As far as I know, for activating the virtual environment it's enought to just run pipenv shell
But in my case after running this command the parentheses at the beginning of the command prompt doesn't show up. Even after selecting the proper interpreter (the one inside venv folder)
To show the parentheses I should run activate command afterward.
So my question is: Is it important to show the project name in parentheses at the terminal prompt? Should I run the activate command after pipenv shell command every time?
I will appreciate if you describe the meaning of those parentheses. Tnx.
PS: I am using vscode in windows 10.
You do not need to activate the environment manually. When you select the python interpreter and open the terminal, vscode will activate it automatically for you.
The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Terminal command. In the latter case, VS Code automatically activated the selected environment.
You can read document for more details.

Is there a way that command prompt could run a python file from Visual Code Studio?

I was wondering if there is a way the command prompt could run a python file when I click the run button on visual code studio. Is that possible? Thanks in advance.
All you do is enter this into command prompt: PATH OF FILE. For example, if my file was hello.py in Documents, then I would type in C:/Documents/hello.py
Yes, that is possible (in fact, a lot of developers and learners do this) and it can be done by following these steps:-
Download and install Python on your system and add the bin folder to the path, environment variables. (Make sure to check the ADD PATH TO ENVIRONMENT option during the installation wizard)
Install Code Runner Extension from Extension tab in VSCode
Set the configuration of terminal in VSCode to Python
Ctrl+Shift+N is the hotkey to run the Python program in terminal in VSCode.
Following all the above steps, you will see the output in the terminal.

How to activate virtual environment in Vscode when running scripts are disabled in system?

I created a virtual environment in vscode in a folder called server by typing:
python -m venv env
And I opened the server folder, select interpreter Python 3.8.1 64-bit('env':venv)
then I got following error:
I can't find any solution to this and I am stuck for hours.
It seems that it is going to activate the environment through a powershell script. And running such scripts is turned off by default. Also, usually a virtual environment is activated through cmd and .bat script. You could either turn on running powershell script or make VS Code activate an environment through cmd and .bat file.
The first way - using cmd instead of Powershell
I just checked it in my PC and VS Code doesn't use Powershell at all. It activate an environment with cmd instead of Powershell. Probably it is worth to check VS Code settings, set cmd as a default terminal. It is probably such an option in the main settings.json (you can open it through ctrl+shift+p and type 'open settings (JSON)'): "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",.
The second way - changing Powershell execution policy
In order to change Powershell execution policy you can add "terminal.integrated.shellArgs.windows": ["-ExecutionPolicy", "Bypass"] to your main VS Code settings.
Also you can open a Powershell window as administrator and type the following:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then respond y to any questions.
An update with regards to using the Command Prompt instead of Powershell in VS Code:
When implementing "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe" in settings.json it gives the notice:
This is deprecated, the new recommended way to configure your default shell is by creating a terminal profile in #terminal.integrated.profiles.windows# and setting its profile name as the default in #terminal.integrated.defaultProfile.windows#. This will currently take priority over the new profiles settings but that will change in the future
After taking a look at the documentation I've found that the correct alternative would be to include a line as shown below.
"terminal.integrated.defaultProfile.windows": "Command Prompt"
Alternatively, you can also use the GUI as shown in the documentation. More advanced settings are also shown there.
Try Using Cmd in vscode and Run this command env(your Virtual Env name)\Scripts\activate

Visual Studio Code - Code Runner : 'python' is not recognized as an internal or external command, operable program or batch file

I have installed Visual Studio Code (VSC) and Code Runner.
When I use the terminal to execute my code it works fine and I get the results in the terminal window:
However, when I try to use Code Runner's keyboard shortcut Ctrl+Alt+NI keep getting this error message:
Which baffles me because VSC shows this:
I've tried rebooting (solution on another related SO question), creating a new conda environment inside VSC along with what seemed to be relevant from the VSC documentation here and here.
If anyone can help me understand what I need to be doing differently I would appreciate it.
You need to set a symbolic link between calling Python3 and Python since they're effectively the same thing. If you go into a normal command prompt / terminal and type Python3, you may get the same error. Whereas, if you type Python, it will launch the Python interpreter.
Close VSCode
If you're a Windows user you can use mklink to sort it out — open a Command Prompt as an Administrator, then navigate to your Python installation. e.g.:
cd C:\Python38
mklink python3.exe python.exe
In Linux, you can set a Symbolic link:
cd /usr/bin/python3
ln -s python.exe python3.exe
Startup VSCode again and it should work.

VS Code not running a Python file in the conda environment mentioned

I have both anaconda and VS Code installed in my system. When I start writing a script, VS Code gives me an option to chose the interpreter. I choose my particular conda environment. This works fine. However when I try to run the script using Ctrl+Alt+N or using the play button on the top right (using the extension Code Runner), the terminal that opens in the VS Code doesn't recognize that I am now working in a conda environment.
As a result, it gives an error that the module is not installed (because the module is available only in the conda environment).
When I try to manually activate the conda environment from the VS Code terminal, it doesn't work.
What is the workaround? I am using Windows.
Update: If I use cmd as the default integrated terminal in VS Code in place of PowerShell, then the command activate NAME_OF_ANACONDA_ENV works. And then, I can manually run the script. However, running the script using Ctrl+Alt+N still doesn't work; it doesn't recognize that I am in a conda env.
After a few tries i figured out how to do it. So, this works for me.
vs code settings search: code-runner.executorMap
Choose User and
Edit in settings.json
find: "python": "python -u"
change: "python": "$pythonPath -u $fullFileName"
if you encounter tempCodeRunnerFile or selection problem:
vs code settings search: code-runner.ignoreSelection
tick the box
On windows 10: Ctrl+Shift+P and selecting the environment gets the job done.
After setting the environment, I execute by pressing Crtl+F5 to execute without debugging and F5 to debug.

Categories