-1

I am not sure if this is possible but I would like to run PYTHON CODE from a .bat file and not a python file.

Something like this in a .bat file:

python
print('Hello World')

Edit: I didn't add it to the question, but in addition I would like for an active python console to remain open after running the code.

Al-Baraa El-Hag
  • 770
  • 6
  • 15
  • You create a .py with `print('Hello World')` and then from the .bat you do `python myfile.py` – dcg Dec 13 '19 at 21:39
  • You could call a `.py` file from a `.bat` file, but `python` and `batch` are two difference languages – Brydenr Dec 13 '19 at 21:40
  • I know that you can call a python file from .bat. But is there a way to directly run python code? – Al-Baraa El-Hag Dec 13 '19 at 21:42
  • 1
    Looks like an *XY Problem*. Why do you need this behavior? – CristiFati Dec 13 '19 at 21:46
  • check this: https://stackoverflow.com/a/41642050/388389 – npocmaka Dec 13 '19 at 21:47
  • @ChristiFati. When coding python, I use IdleX because it is very lightweight. Unfortunately, it cannot handle tensorflow's outputting methodology yet (has an issue with carriage-return and backspace. So I want to run my python code that contains tensorflow in a command window shell that is still 'active' after the code is done running. Using the `exec(open(filename).read())` is perfect for what I need, but it is a bit of a bother doing it manually everytime. I would like to automate it – Al-Baraa El-Hag Dec 13 '19 at 21:53
  • `.bat` indicates that the file contains commands in Windows OS command format. What are you trying to accomplish? The 2-line example you give is not legal in either Python or WinOS; I don't see how you expect it to work. – Prune Dec 13 '19 at 21:54
  • Ah ... so write a trivial `bat` script that takes `filename` as a command-line argument. – Prune Dec 13 '19 at 21:55
  • The problem with that is that it doesn't open a python console. I would like the code to run, and a python console to remain open – Al-Baraa El-Hag Dec 13 '19 at 21:57
  • Why do you want a Python console to remain open? @CristiFati is right, this definitely feels like an XY problem. – AMC Dec 13 '19 at 22:22
  • I mentioned it above. I am using IdleX as my IDE and it's shell cannot handle tensorflow's output yet. – Al-Baraa El-Hag Dec 13 '19 at 22:24

1 Answers1

1

You can use the -c or -cmd option, -c cmd : program passed in as string (terminates option list)

python -c "print('hello world')"

hello world

If you try to use double quotes in your code however, that will cause a problem

python -c "print("hello world")"

  File "<string>", line 1
    print(hello

So you must escape them with a backslash

python -c "print(\"hello world\")"

hello world
Hymns For Disco
  • 7,530
  • 2
  • 17
  • 33
  • So I tried it, and it closes the command prompt window really quickly. I put in this as a code and it still didn't work `>python -c "input('hello world')"` – Al-Baraa El-Hag Dec 13 '19 at 21:48
  • 1
    Sorry for the confusion, the `>` character is not part of the script. I will remove that. – Hymns For Disco Dec 13 '19 at 21:50
  • Working great, but is it possible to add multiple line codes? Also, I would like the python console to remain open afterwards – Al-Baraa El-Hag Dec 13 '19 at 21:55
  • 1
    adding the `-i` option before `-c` will keep the python shell open after executing your code. As far as multi-lines in the code, I'm not sure – Hymns For Disco Dec 13 '19 at 22:06