0

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?

asked Jun 23, 2017 at 14:16
4

4 Answers 4

0

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/

answered Jun 23, 2017 at 14:18
Sign up to request clarification or add additional context in comments.

8 Comments

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
@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?
I will edit my answer so that I hope I can get my rep back :-D
@EastonBornemeier Nope, I didn't mean bash. I meant Batch.
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.
|
0

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
answered Jun 23, 2017 at 14:26

Comments

0

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?

answered Jun 23, 2017 at 14:27

3 Comments

How about converting it to .exe? Will it help? If yes, I need a way to convert it without having too many files.
convert your python code to exe you mean? why do you want to do that?
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.
0

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

answered Jun 23, 2017 at 14:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.