0

Python scripts are great for many things, but sometimes I just need to open a command line and do basic calculations. It would be nice not to have to run Anaconda, then start Python, then import the math package, then define basic constants (e.g. Na = 6.022e23) every time I need to calculate something simple.

Therefore, I have tried writing a batch file to run Anaconda, start Python, import the math package, and define constants. These lines in my BAT code will start Anaconda and Python:

call C:\Users\Chris\Miniconda3\Scripts\activate.bat C:\Users\Chris\Miniconda3
python

Unfortunately, any lines I add after this in the BAT code are ignored. I would be grateful for any recommendations to make this work.

  • https://stackoverflow.com/questions/4571244/creating-a-bat-file-for-python-script – Dhruv Mar 14 '20 at 04:05
  • look at Michael Villani's answer on that thread – Dhruv Mar 14 '20 at 04:06
  • If you want to run another command, before closing the python executable, try `start`. e.g. `start python`. Please open a Command Prompt window and enter `start /?` to read its help information. – Compo Mar 14 '20 at 07:10

2 Answers2

1
@echo off
call "C:\Users\Chris\Miniconda3\Scripts\activate.bat" "C:\Users\Chris\Miniconda3"
python -i -c "import math; Na = 6.022e23"

This code may suit your need if the Python code is compact. It will import and assign constants as arguments to Python. Use ; to separate Python code lines instead of newline character(s).

The -i is to enter inspection mode and -c allows Python code to be the following argument.

Based on answer by Michael Villani:

 @echo off
rem = r'''
:: From here and on, write any Batch file syntax and it will be ignored by Python
::
:: The Batchography book by Elias Bachaalany
::
call "C:\Users\Chris\Miniconda3\Scripts\activate.bat" "C:\Users\Chris\Miniconda3"
python -ix "%~f0" %*
exit /b
:: End of batch file commands
'''
# Anything here is interpreted by Python
del rem
import math
Na = 6.022e23

Recommend to use a raw string literal so that \ does not need to be escaped for the batch-file code.

Both concepts cause inspection mode -i after the Python code has executed. Use exit() or quit() to exit the inspection mode.

So you can use the interactive prompt and enter your math.

>>> Na
6.022e+23
>>> 3+2
5
>>>
michael_heath
  • 5,262
  • 2
  • 12
  • 22
0
@echo off
rem = """
:: From here and on, write any Batch file syntax and it will be ignored by Python
:: 
:: The Batchography book by Elias Bachaalany
::
python -x "%~f0" %*
exit /b %errorlevel%
:: End of batch file commands
"""
# Anything here is interpreted by Python
import platform
import sys
print("Hello world from Python %s!\n" % platform.python_version())
print("The passed arguments are: %s" % sys.argv[1:])

http://lallouslab.net/2017/06/12/batchography-embedding-python-scripts-in-your-batch-file-script/

Dhruv
  • 645
  • 9
  • 17
  • Thanks, @Dhruv. Unfortunately this code does not give me what I need, which is an open terminal (with Python started and some Python lines already run) that I can use to do basic math. When I try to put the above code in a batch file, the resulting window immediately closes. – thatguyfromcanada Mar 14 '20 at 05:22