Saving entered command in windows command prompt - 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.

Related

How to make a command that runs Python a script in Windows command line?

Background:
I'm using Windows. I know some of programming with python. I don't know much about batch, which I think I might need to do what I want.
I will show a example so it becomes more clear what I'm trying to do.
Example:
When using git, after you install it, you can call the git command from anywhere of your computer, you can execute git commands, like git init and this will create a git file in your current folder.
I don't know exactly how git works or what language they use but I want to do the same thing, create my own command that I can execute from anywhere in my computer after I "install" my program.
What I'm trying to do:
I want to make my own command and when I call it, it executes a python script.
e.g.
I install my program and it creates a command called myprogram and when I type myprogram in the command line, it's like if I typed python myprogram.py. And myprogram -someargument would be the same as python myprogram.py -someargument.
What I tried until now:
I'm searched for How to make a environment variable that runs Python script? but I never get exactly what I want, is always something like How do I set environment variable using Python script?.
Maybe I'm making the wrong question and the result I want are not showing?
I'm looking for a explanation on how to do this or at least a tutorial/guide.
Edit 1:
As UnholySheep said in the comments, it's not environment variable, its commands, so I changed the question even does what I want to know is the same thing.
Files you need:
First you need a python script (obviously) so I created a file called myprogram.py that have this simple line:
print("This should be from a command")
After you need to make a batch file, in my case I used a .cmd file called myprogram.cmd that have:
#ECHO OFF
python_directory\python.exe python_script_directory\myprogram.py %*
Configurations to make:
You need to set in PATH environment variable the location of the batch file batch_file_directory\myprogram.cmd
And now if you execute in the command line myprogram it will print This should be from a command.
You can also use .exe or .bat files.

Unable to source .profile in shell script

To start off, I am a complete noob to Debian, Python, and shell scripts, so please speak to me like I am a toddler.
I have a python script I am running through a virtualenv, and I want to execute it via a shell script. Here is what I'm typing in to the terminal to do so:
source .profile
workon cv
cd Desktop/Camera
python main.py
I tried turning this into a shell script, but I receive the error -- source: not found
I've found no answer to my problem, at least not in any terms I can understand. Any advice would be appreciated. Furthermore, before you answer, I also have no idea why it is I need to execute source .profile, I'm simply following a beginner guide for the project which can be found here: https://www.hackster.io/hackerhouse/smart-security-camera-90d7bd
Thanks in advance, and sorry if this is a dumb question.
Best practice for Shell would be shebang at the begging like #Veda suggested.
Execute the shell script using bash like bash shell.sh as the link suggests using relative locations rather than absolute ones
Add a hashbang at the top of your script (should be the first line):
#!/bin/bash
This will ensure you are running your shell-script in bash. Having a shell does not mean it's bash. Not all shells have the source function that you are using (bash has it).
Some prefer the following:
#!/usr/bin/env bash
Since you are a "beginner" I think it does not really matter. The first makes sure it's using the bash in /bin, the second is using the PATH variable to find bash, so the user of your script can provide it's own bash if he/she wants.

Unable to run .py file from putty, Syntax error: word unexpected (expecting ")")

I am new to both Python and Linux and as such request simple explanations with minimal assumed knowledge where possible please, however I am more than willing to invest time and effort to learn.
I have a Raspberry Pi 2 (Model B V1.1) that is running Linux. I interact with this pi via putty.
I am trying to create a simple competitive reflex game, consisting of 2 buttons and a single LED. My goal is to have the LED light up after a short interval, and the first player to press their button wins.
I am writing the script for this with python (specifically 2.7.3)
My issue is that i am unable to run ANY .py file from within putty, i always receive the same error:
Syntax error: word unexpected (expecting ")")
To determine if the issue was an error in my code, i created a very very simple .py file, to check if the same error occurs, and it did. So i currently believe even if my code was functional, something is stopping me from running ANY .py file.
The process I am using is as follows:
First I create a new python file from within putty:
sudo nano test.py
Next I enter my python code (very simple for now, as i cannot get ANY .py file to run)
for each in range(5):
print 'hello'
I then press CTRL + O to write the file, hit enter, then CTRL + X to exit
Finally, I make the file executable using
sudo chmod u+x test.py
and try to run it
sudo ./test.py
again, a similar error occurs
Syntax error: "(" unexpected
I then decided to enter the code directly into the python shell, using
sudo python
>>>for each in range(5):
... print 'hello'
This time the output is the desired outcome:
hello
hello
hello
hello
hello
So there is no problem in executing python code directly from the shell, I am just unable to execute any previously saved .py file
Any insight into what could be causing this is much appreciated, and I apologise if I have not provided enough information to be useful for you.
Thanks in advance!
Short answer: Either run these as python filename.py, or else add the line #!/usr/bin/python to the top of your Python scripts.
Long answer: When you run a file from the command line in Linux (which is what the Raspberry Pi is running), by default it assumes that the file is a shell script file (usually Bash script). So it uses the Bash shell (or some other shell, but it's usually Bash) to interpret the file, and Bash doesn't know Python syntax. If you want to run your file using a different interpreter (Python, in this case), you have to add a "magic line" at the top of the file starting with #! (usually pronounced "hash-bang", and sometimes pronounced "shebang" for short). Following the #! characters is the full path of the interpreter to use, e.g. /usr/bin/python for Python scripts. (You can also use /usr/bin/env python as another answer suggested; I prefer /usr/bin/python because it's impossible to get the wrong Python interpreter that way. But that's getting into advanced topics that may be more than you need right now.)
So when you put the line #!/usr/bin/python at the top of your Python scripts, you're telling the Linux system which interpreter to run the program with, and then it should All Just Work™.
Also, STOP using sudo to edit and run these! That's just asking for trouble.
If you wish to execute like this you need the following line as the first line
#!/usr/bin/env python
This will tell bash (or equivalent) to execute the file with the Python interpreter.
If you don't want to do that then you can execute the script like this:
$ python test.py
If you go this route then you don't need to grant execute permissions on the script itself.
Also, scripts shouldn't be executed with sudo unless absolutely necessary.

Changing called command in Powershell

I am trying to run a bat script via Powershell that uses Python calls, using Cygwin's Python. The script is pre-made (i.e. not mine) and rather large, but since it invokes python, rather than python.exe calls, it does not execute properly, since the proper command would be python27.exe. How can I change that in Powershell?
I guess you can create an alias for that in your PowerShell script or profile.
Something like below could be an option for you (include this at the top of your PowerShell script, before the first call to python):
New-Alias -Name python -Value "C:\Program Files\Python\Python27.exe"
Also check in help in PowerShell itself for this.
Get-Help about_alias
https://technet.microsoft.com/en-us/library/hh849959.aspx

Python open default terminal, execute commands, keep open, AND then allow user-input

I'm wanting to open a terminal from a Python script (not one marked as executable, but actually doing python3 myscript.py to run it), have the terminal run commands, and then keep the terminal open and let the user type commands into it.
EDIT (as suggested): I am primarily needing this for Linux (I'm using Xubuntu, Ubuntu and stuff like that). It would be really nice to know Windows 7/8 and Mac methods, too, since I'd like a cross-platform solution in the long-run. Input for any system would be appreciated, however.
Just so people know some useful stuff pertaining to this, here's some code that may be difficult to come up with without some research. This doesn't allow user-input, but it does keep the window open. The code is specifically for Linux:
import subprocess, shlex;
myFilePathString="/home/asdf asdf/file.py";
params=shlex.split('x-terminal-emulator -e bash -c "python3 \''+myFilePathString+'\'; echo \'(Press any key to exit the terminal emulator.)\'; read -n 1 -s"');
subprocess.call(params);
To open it with the Python interpreter running afterward, which is about as good, if not better than what I'm looking for, try this:
import subprocess, shlex;
myFilePathString="/home/asdf asdf/file.py";
params=shlex.split('x-terminal-emulator -e bash -c "python3 -i \''+myFilePathString+'\'"');
subprocess.call(params);
I say these examples may take some time to come up with because passing parameters to bash, which is being opened within another command can be problematic without taking a few steps. Plus, you need to know to use to quotes in the right places, or else, for example, if there's a space in your file path, then you'll have problems and might not know why.
EDIT: For clarity (and part of the answer), I found out that there's a standard way to do this in Windows:
cmd /K [whatever your commands are]
So, if you don't know what I mean try that and see what happens. Here's the URL where I found the information: http://ss64.com/nt/cmd.html

Categories

Resources