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?](https://stackoverflow.com/questions/3777301/how-to-call-a-shell-script-from-python-code) – Pedro von Hertwig Batista Jun 23 '17 at 14:17
-
2Possible duplicate of [Call Python script from bash with argument](https://stackoverflow.com/questions/14155669/call-python-script-from-bash-with-argument) – Easton Bornemeier Jun 23 '17 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 either – Easton Bornemeier Jun 23 '17 at 14:25
-
Will wrapping or converting to .exe help? – Niaz M. Sameer Jun 23 '17 at 14:30
4 Answers
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/

- 4,862
- 2
- 19
- 33
-
This isn't what OP is asking, he wants to run commands such as `python myscript.py arg1 arg2` from bash and have them pass as arguments into the program – Easton Bornemeier Jun 23 '17 at 14:20
-
@ArturoAndressi Thanks, however I want the python script to be run without an interpreter. I know it might sound stupid but is there any way around it? – Niaz M. Sameer Jun 23 '17 at 14:21
-
-
-
After your edit, I meant the script file (e.g. myscript.py) being executed from batch (without interpreter). I didn't mean the python file being converted into CMD script. I edited my question to make it clear. – Niaz M. Sameer Jun 23 '17 at 14:25
-
Python is an interpretated language, so either python.exe or CMD.exe must interpret it. But CMD does not support Python as a language for its scripting. So I maintain as is, this is not possible. If you want to execute Python code without doing code conversion then you MUST use an interpreter or BUNDLE (aka "compile" but this is not it really) the code: this is basically having a .exe embedding both Python Interpreter and your code. This is a standalone app that will be started like any other exe. – Fabien Jun 23 '17 at 14:29
-
-
You're welcome. I also provided a link to a windows python bundler for you in my answer :-) – Fabien Jun 23 '17 at 14:35
Look 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

- 676
- 4
- 15
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?

- 59
- 4
-
How about converting it to .exe? Will it help? If yes, I need a way to convert it without having too many files. – Niaz M. Sameer Jun 23 '17 at 14:28
-
convert your python code to exe you mean? why do you want to do that? – Avinash Rao Jun 23 '17 at 14:30
-
for what you are asking, this is the best way to execute it as far as i know. unfortunately i have never converted python code to .exe file, sorry cannot help you with that. – Avinash Rao Jun 23 '17 at 14:34
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

- 418
- 1
- 4
- 12