简体   繁体   中英

How do I run a function in a Python module using only the Windows command line?

To clarify, the Python module I'm writing is a self-written.py file (named converter ), not one that comes standard with the Python libraries.

Anyways, I want to somehow overload my function such that typing in

 converter file_name

will send the file's name to

 def converter(file_name):
    # do something

I've been extensively searching through Google and StackOverflow, but can't find anything that doesn't require the use of special characters like $ or command line options like -c. Does anyone know how to do this?

You can use something like PyInstaller to create a exe out of your py-file.

To use the argument in python:

import sys

if __name__ == "__main__":
    converter(sys.argv[1])

You can type in the windows shell:

python converter.py file_name.txt

to give the arguments to the sys.argv list within python. So to access them:

import sys
sys.argv[0] # this will be converter.py
sys.argv[1] # this will be file_name.txt

at the bottom of the file you want to run, add:

if __name__ == "__main__":
    converter(sys.argv[1])

To have a second argument:

python converter.py file_name1.txt file_name2.txt

This will be the result:

import sys
sys.argv[0] # this will be converter.py
sys.argv[1] # this will be file_name1.txt
sys.argv[2] # this will be file_name2.txt

I would recommend using something like the builtin argparse (for 2.7/3.2+) or argparse on pypi (for 2.3+) if you're doing many complicated command line options.

Only way I can think of is to create a batch file of the same name and within it, call your python script with the parameters provided.

With batch files you don't have to specify the extension (.bat) so it gets you closer to where you want to be.

Also, without any compiling .py to. exe , you may make your script 'executable', so that if you issue command line like myscript param param , the system will search for myscript.py and run it for you, as if it was an .exe or .bat file.

In order to achieve this, configure the machine where you plan to run your script:

  1. Make sure you have file associations set ( .py to python interpreter, that is, if you doubleclick at your script in the explorer -- it gets executed). Normally this gets configured by the Python installer.
  2. Edit the COMSPEC environment variable (look inside My Computer properties) to include .PY extension as well as .EXE , .COM , etc.
  3. Start a fresh cmd.exe from Start menu to use the new value of variable. Old instances of any programs will see only old value.

This setup could be handy if you run many scripts on the same machine, and not so handy if you spread you scripts to many machines. In the latter case you better use py2exe converter to bundle up your application into a self-sufficient package (which doesn't require even python to be installed).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM