I need to implement a SRLS (Student Record Logging System). I'll be using two languages, Batch(CMD) and Python. However, I need to execute python scripts(NOT CODE) (with variables from the batch console) from Batch without the Python interpreter. Is there any way to start/execute python scripts directly from batch?
-
4Possible duplicate of How to call a shell script from python code?Pedro von Hertwig Batista– Pedro von Hertwig Batista2017年06月23日 14:17:40 +00:00Commented Jun 23, 2017 at 14:17
-
2Possible duplicate of Call Python script from bash with argumentEaston Bornemeier– Easton Bornemeier2017年06月23日 14:18:02 +00:00Commented Jun 23, 2017 at 14:18
-
I think you may be confused in terminology. Batch is for scripting files that hold a list of commands to be run. Bash is equivalent to CMD. You can start python scripts from eitherEaston Bornemeier– Easton Bornemeier2017年06月23日 14:25:54 +00:00Commented Jun 23, 2017 at 14:25
-
Will wrapping or converting to .exe help?Niaz M. Sameer– Niaz M. Sameer2017年06月23日 14:30:28 +00:00Commented Jun 23, 2017 at 14:30
4 Answers 4
Yes, you can register the .py extension file to be executed with python.exe
This is from memory, I am under Linux at the moment, but this looks like:
Select any file .py -> Shift Right Click -> Open With -> Browse -> Path to Python.exe -> Always Use this
EDIT:
there is NO way for the Python language code to be executed straight by CMD.exe
CMD.exe uses its own syntax and it would require a code conversion for this to work. AFAIK I do not know converters from Python to CMD scripts... They are too different in purposes.
Code conversion is not what people do if they want the code to execute anywhere: they 'compile' (in fact it's more bundling) the code so that both interpreter and code are part of one single exe.
E.g. look at: http://www.pyinstaller.org/
8 Comments
python myscript.py arg1 arg2 from bash and have them pass as arguments into the programLook here for running a batch file to run a python file in windows: https://gist.github.com/zhuzhuor/7271159
@ECHO off
set "script_path=%~dp0"
set "script_path=%script_path%my_script.py"
python %script_path% %*
If you want to run files in Linux:
$ run shell_script.sh
you can write a shell script
#!/usr/bin/python -- sets the environment of python to run the code.
# write the python code here
Comments
All python code is executed by the python interpreter, but i think i understand what you mean.
First you will need to install python and ensure that the PATH variable in the environment variable is set.
Next go to the directory where you have kept you python code in the command prompt using the cd command
To execute the python code, simply type "python [your python file name]"; to execute from any directory type "python [path to you python file]" eg: python hello_world.py or python C:\tmp\hello_world.py; when you execute the command it will call the python interpreter and pass the script/your code to it, the interpreter then executes it(in a nutshell)
also do take a look at How do I run a python program in the Command Prompt in Windows 7?
3 Comments
In windows add your python.exe's path to 'HOME' enviroment variable, than go to where ur .py file is, open up a command line and enter "python 《ur_filename》.py" OR, With bat file:
c:\python27\python.exe c:\《path_to_ur_file》.py %*
And If ur on linux system, This has already been answered here https://askubuntu.com/q/244378