Copyright 2017-2024 Jason Ross, All Rights Reserved

A Terminal Window on the Desktop

Everyone who's developed with Python knows that you can run scripts from the command line using:

python myscript.py

and run applications or modules using:

python -m mymodule

This is all very straightforward, but how do you run Python scripts and applications directly from the command line WITHOUT using the Python command, like regular scripts and programs?

Running A Python Script Without The Python Command

Linux and MacOS

If you want to run a Python script directly from the command line, without the python command, you need to use another way to tell the operating system how to run the script. You do this in the same way as with other scripts, by adding the information in the script's first line. The "shebang" #! is used to specify the interpreter to use, so to run a python script using the default python interpreter on your system, the first line of the .py file should be:

#!/usr/bin/python

You can also specify a particular version of the interpreter in this line:

#!/usr/bin/python3.9

This can be useful, but you need to make sure that wherever you run scripts that are marked like this, the correct version of the Python interpreter is installed too.

You also need to set the permissions of the script to allow execution, so:

chmod +x myscript.py

Once you've done that, you can run the script directly.

If the script is in the current directory:

./myscript.py

If the script is in the PATH:

myscript.py

Or to run the script directly from another directory:

path/to/myscript.py

Windows

Running a script in Windows is straightforward as long as the .py file extension is associated with py.exe, which should happen when you install Python. Assuming that's still intact, you just need to run the script using:

myscript.py

or:

start myscript.py

Windows will also take notice of the Linux-style "shebang" line, so you can select a specific version of Python to run the script.

Running A Python Package Without The Python Command

Python packages can consist of one of more modules or packages, and they're usually distributed as wheel files. In the same way that scripts have to contain details to tell the operating system how to run them, so must applications. The actual way that this information is specified depends on how you create the package and, to some extent, how you want to run it.

In the following examples, I've assumed a project that looks something like this:

\myproject
    \mypackage
        __init__.py
        \module
            __init__.py
            main.py
            <other files>
    setup.py or pyproject.toml

Linux and MacOS

Standard Python Packaging

Despite, or maybe because of, being the original way of doing it, the setup.py file is probably the most flexible way to set up your package to run without the python command. To do this, you need to use the entry_points section of the setup tuple, like this:

setup(
    ...
    entry_points={  # Optional
        "console_scripts": [
            "domything=mypackage.module:main",
        ],
    },
    ...
)

The above setting will call the main() method in mypackage/module.py (or mypackage/module/__init__.py) when you type domything from the terminal after you've installed the package.

Note: The setup.py gives you a lot of control over the package you're creating, even if it seems a little old-fashioned. Take a look at this example to see what else you can do with it.

If you've moved on to using the newer pyproject.toml file format, this entry does the same as the setup.py one above:

[project.scripts]
domything="mypackage.module:main"

Again, the sample project's setup.py contains an example of this.

Poetry

Poetry also uses a pyproject.toml file, but the syntax differs from the standard Python one. To add the domything command described in the above section, you need to add the following to your Poetry pyproject.toml file:

[tool.poetry.scripts]
domything="mypackage.module:main"

Windows

Standard Python Packaging

The good news is that you can use exactly the same file entries to run packages from the command line in Windows, as you do on MacOS and Linux systems.  The difference is that clicking on the script in the UI will open a terminal window to run it.

If this isn't what you want, you need to use the standard Python pyproject.toml file, with the following entry:

[project.gui-scripts]
domything="mypackage.module:main"

Running the script now will run it in the background and return control to the current process. This is the only way to do this, and only works on Windows.

Summary

This should cover everything you need to do to call your script, module or application from the command line without having to use the python command. It might seem unimportant, but it simplifies calling your code and that can't be bad.

Made In YYC

Made In YYC
Made In YYC

Hosted in Canada by CanSpace Solutions