Recently I am playing around with Rich. It's really helpful while debugging and tracking code running progress. However, if I use task scheduler to auto-run python script, it will open command prompt to run the script instead of others like Window PowerShell. All the output from Rich will not show in command prompt. Is there anyway to set python.exe run by other instead of command prompt?
My batch file looks like this:
"C:\Python39\python.exe" "C:\PATH\TO\PyScript.py"
Yes, there is a way to do this.
Just use this command:
powershell.exe "C:\Python39\python.exe C:\PATH\TO\PyScript.py"
Related
I would like to run a script like in the picture in the Powershell but from python. [1]: https://i.stack.imgur.com/K7XGI.png For instance, I would like to have a code like this:
def run(namefile):
command that opens powershell
command that type in the powershell .\spim.exe .\namefile.txt
command that run the script
Does someone know how to do this ?
If you just want to run the exe file you can use subprocess module.
If you really want to run it inside the Powershell terminal you can run the Powershell binary with -Command option.
Currently, in Visual Studio Code (under Windows 10 64bits), at a Python file called path\myfile.py, if one clicks with mouse right-button for context menu and then chooses 'Run Python File in Terminal', an integrated CMD terminal is open and file is automatically run there with:
python.exe path\myfile.py
After the file stops running, one is naturally left at the integrated CMD cursor.
This behavior is quite different, for instance, from what one has with an IDE like Spyder. There, when you run code (e.g. with F5), at the end one is left still at the Python cursor and can access content of variables created when code was run.
Is there a way to achieve a similar behavior in Visual Studio Code?
You can configure VS Code Python extension to use the -i command line option
Described in https://docs.python.org/3/using/cmdline.html#cmdoption-i
You only have to add the setting bellow (inside settings.json file)
"python.terminal.launchArgs": ["-i"],
This will execute the command python.exe -i path\myfile.py.
I don't know if it is a new feature, but I've been using it for while.
If you would like to use the terminal IPython, like in Spyder, you can use a different set of options, as the following:
"python.terminal.launchArgs": ["-m","IPython","-i"],
With these, VS Code will execute the command python.exe -m IPython -i path\myfile.py.
Then, it will run IPython module as a "script" (with -m option), which will use the options -i path\myfile.py, i.e., IPython will run the file and remain opened.
BTW, another thing is: you can run "cells" in Spyder's integrarted terminal (regions of code with #%%). But in VS Code it seems you can't.
I've made a question with a "work around" to run cells of Python files in VS Code Integrated terminal, which is posted Here
Yes. Open a terminal window and it's like a terminal window on your computer. You can type python filepathandname and the python script will execute like it does from the command line.
The closest you can come is to run the code under the debugger and set a breakpoint at the end to pause the exiting of the execution. Otherwise feel free to file a feature request at https://github.com/microsoft/vscode-python.
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.
What I would like to do is launch an interactive ipython session from notepad++, and keep the window open (in interactive mode) after a script completes, but for the window to close once I exit from ipython. This seems like a fairly simple task, but I'm having trouble finding the answer.
In notepad++, I have entered the following for the Run command (F5):
cmd /k ipython -i "$(FULL_CURRENT_PATH)"
This works fine: it opens and runs the script I am editing using ipython, and keeps the ipython session open once the script is complete.
However, after entering the exit command, I have to enter exit a second time at the command prompt) to close the window (or close it with the mouse). It would be nice if I didn't have to enter exit twice. Is there a solution to get things working the way I want them to work?
I have tried removing /k flag (my understanding is this flag keeps the window open):
cmd ipython -i "$(FULL_CURRENT_PATH)"
However, the script does not seem to run at all in this case.
Well I discovered the answer just before I was about to post the question! Instead of deleting it altogether, I thought I would go ahead and post the resolution so that others can find it later. It turned out to be pretty simple!:
ipython -i "$(FULL_CURRENT_PATH)"
(Note that in order for this to work, ipython must be available as a program to be run from a command prompt.)
I have opened Anaconda - then i maneuvered to the directory where a certain python program i want to run actually lies.
I then tried the %run command.
But the command does not seem to work!
So how am i to run that program?
Does anyone know the right command that one has to use in the black colored Anaconda console command line, to run a Python program existing in a certain directory (to which the command line has been taken to)
%run is a command that's run from inside of IPython. To use it, you should start ipython first. Or just run python program.py (if your program is named program.py).