How to Run Python Scripts Files, Modules, Packages from the Command Line

    Beatme CodeBY-NC-ND
    12:23 read·1611 words· modified

    This article is about running Python scripts in the form of files, modules, or packages from the command line. As for how to run the code, you can check out the article How to Run Python Code from the Command Line.

    Commands to Run Python Script Files

    For different operating systems, you can type python or python3 (or some alias for Python) on the command line and give the path containing the Python script files to run them.

    Windows
    python <script>
    UNIX/Linux/macOS
    python3 <script>
    script argument

    The script argument, which can be a relative or absolute path, points to a Python file with the extension py, or a folder containing the __main__.py file, or a zip compressed file containing the __main__.py file.

    Run Python Script Files from the Command Line

    Replacing the script argument with a py file containing Python code will make that script file run. In fact, Python does not have a mandatory requirement for the extension of the file to be run, so extensions other than py are allowed, such as txt (for text files).

    Here’s a Python script file run.py for demonstration purposes, where we use the function print and the built-in variable __file__ to display the file path.

    run.py
    # Enter python or python3 on the command line and give the relative or absolute path to run.py to execute the print function
    print(f'I was executed? {__file__}')

    Control the command line to jump to the directory where the file run.py is located, and then enter the following, and Python code in the file will be executed.

    Windows
    python run.py
    I was executed? \command_line\run.py
    UNIX/Linux/macOS
    python3 run.py
    I was executed? /command_line/run.py

    Run the Zip File or Folder Containing the Python Script File __main__.py from the Command Line

    If the script argument points to a zip file or folder, and the folder or the root directory of the zip file contains the special Python script file __main__.py, then the script file __main__.py will be executed; otherwise, you’ll get a message like can't find '__main__' module in '…'.

    Python only supports executing compressed files in zip format

    Whatever the extension of a compressed file is, it should be in zip format. The rest of the compression formats, such as 7z, gzip, tar, etc., cannot be executed correctly by Python, even if they contain the special Python script file __main__.py.

    Next, we have prepared two different __main__.py files to be placed in the folder run and in the zip file run.zip located in the folder zip, the folder run is in the same directory as the folder zip.

    run/__main__.py
    # Enter python or python3 on the command line and give the relative or absolute path to the run folder to execute the print function
    print(f'__main__ was executed? {__file__}')
    zip/__main__.py
    # Enter python or python3 on the command line and give the relative or absolute path to run.zip to execute the print function
    print(f'zip was executed? {__file__}')

    Control the command line to jump to the directory where the folders run and zip are located, and then enter the following, and the __main__.py contained in the folder run and the file run.zip will be executed.

    Windows
    python run
    __main__ was executed? \command_line\run\__main__.py
    UNIX/Linux/macOS
    python3 run
    __main__ was executed? /command_line/run/__main__.py
    Windows
    python zip\run.zip
    zip was executed? \command_line\zip\run.zip\__main__.py
    UNIX/Linux/macOS
    python3 zip/run.zip
    zip was executed? /command_line/zip/run.zip/__main__.py

    Commands to Run Python Modules, Packages

    Since every py script file is a Python module, running a Python script file as described above can be thought of as running a Python module. Here, we’ll describe another way to run a Python module or package by typing python or python3 (or some alias for Python) on the command line and using the -m argument.

    Windows
    python -m <module-name>
    UNIX/Linux/macOS
    python3 -m <module-name>
    module-name argument value

    The module-name argument value is the fully qualified name of the Python module or package (without writing .py at the end), which may change due to changes in the Python Module Search Path.

    Run Python Modules from the Command Line

    If the fully qualified name given by the value of the module-name argument corresponds to a Python module, then the Python module will be run, which in most cases will have the same effect as running the module’s corresponding py script file directly. Since running a py script file directly from the command line may result in a different Python Module Search Path than running a Python module with the -m argument, there is a possibility that the module may not be located.

    The fully qualified name of a Python module, as indicated by the -m argument on the command line, can contain certain special characters

    In normal Python code, the fully qualified name of a module cannot contain certain special characters, even if they can appear in a filesystem path, such as -, if the name of the Python py script file or the associated folder contains the character -, then writing an import statement such as import all-words.all-verbs is not possible, because - will cause a syntax error.

    The -m argument is not mandatory for fully qualified names of Python modules; you can write paths that are similar to fully qualified names, such as paths that contain the - character, because it doesn’t involve the same syntax problems as writing Python code.

    In the folder all-words, whose name contains -, we create a Python script file all-verbs.py, whose name contains -, and write code for it.

    all-words/all-verbs.py
    # Enter python -m or python3 -m on the command line and give the "fully qualified name" of all-verbs to execute the print function
    print('Here is the module all-verbs')

    Control the command line to jump to the directory where the folder all-words is located and enter the following, and the Python script file all-verbs.py will be run as a module.

    Windows
    python -m all-words.all-verbs
    Here is the module all-verbs
    UNIX/Linux/macOS
    python3 -m all-words.all-verbs
    Here is the module all-verbs

    Python modules that run with the -m argument from the command line need to have a corresponding py script file or pyc bytecode file

    It should be noted that Python modules that can be run with the -m argument need to have a corresponding py script file, or a compiled pyc bytecode file. Therefore, it will not be feasible to run some built-in Python modules, such as the common sys built-in module.

    Windows
    python -m sys
    : No code object available for sys
    UNIX/Linux/macOS
    python3 -m sys
    : No code object available for sys

    Run the Python Package Containing the Module __main__ from the Command Line

    In Python 3.1 or later, you can assign the fully qualified name of a Python package to module-name to run the __main__ module in that Python package. In Python 3.4 or later, this also works for namespace packages (Python packages that don’t contain a __init__ module). When the module __init__ is missing from the Python package being run, you will get a message like No module named ….__main__; '…' is a package and cannot be directly executed.

    Running a Python package with the -m argument is similar to running a zip compressed file or folder containing a __main__.py file, but they may correspond to different Python Module Search Paths, as we discussed in the previous section on running Python modules.

    Also, since Python packages are a special kind of module, some of the problems with running Python modules apply to running Python packages as well. For example, a “fully qualified name” that contains the character -.

    Next, we create the Python script file __main__.py in the all-words folder and write code for it.

    all-words/__main__.py
    # Enter python -m or python3 -m on the command line and give the "fully qualified name" of all-words to execute the print function
    print('Here is the __main__ module for all-words')

    Control the command line to jump to the directory where the folder all-words is located and enter the following, and the __main__ module will be run.

    Windows
    python -m all-words
    Here is the __main__ module for all-words
    UNIX/Linux/macOS
    python3 -m all-words
    Here is the __main__ module for all-words

    Run the Python-Compiled Bytecode File

    In several of the execution methods described above, py files can be replaced with compiled pyc bytecode files. For example, a Python bytecode file named __main__.pyc can be placed in a folder or zip compressed file to replace the original __main__.py.

    Not running the Python pyc bytecode file compiled in the __pycache__ folder

    It should be noted that running pyc here does not mean that Python automatically runs the bytecode files compiled in the __pycache__ folder, but directly runs pyc from the command line.

    After compiling run.py into a pyc file using the py_compile module, control the command line to jump to the directory where the pyc file is located and enter the command to execute it. Here, we assume that the name of the compiled Python bytecode file is run.pyc.

    Windows
    python run.pyc
    I was executed? \command_line\run.pyc
    UNIX/Linux/macOS
    python3 run.pyc
    I was executed? /command_line/run.pyc

    Execution priority of Python py script files and pyc bytecode files

    If you execute a command that does not explicitly specify a format requirement, such as via the -m argument, and there are Python py script files and pyc bytecode files with the same name in a folder or zip compressed file (not in the __pycache__ folder), Python will prioritize the execution of the py script file by default, and the compiled Python pyc bytecode file will be ignored.

    Source Code

    src/en/command_line·codebeatme/python·GitHub