1

Let's say I have a python file named myfile.py in a certain directory. How do I just call the python file directly without invoking python.

myfile.py

and not

python myfile.py

2
  • Operating system is not an oracle and will not guess what your intents are. Python is not the only program that can operate .py files, so you have to specify what program to run it with. I guess, there could be some way to run it with python if no other is specified, but why would you even want that in the first place?
    – go2nirvana
    Commented Mar 17, 2021 at 9:52
  • this is a bad idea because sooner or later you will have multiple version of python on your windows pc. alternative: for those scripts you want to start by name, create a .batfile which starts that program with your chosen version of python.
    – 576i
    Commented Mar 17, 2021 at 10:08

2 Answers 2

1

Edit:

To be more precise.

just typing the filename in the command line will not work.

Typing start program.py however should work.

Windows normally has information telling it which program is the default program for a given suffix.

Concerning @go2nirvan's comment: Though windows is not an oracle, it can have information for each file suffix to know what's the related default application. Even many linux desktop application associate a default application to certain mime types.

If you click on .xls (depending on what is installed) either Excel, or OpenOfficeCalc or LibreOffice will be opened)

Windows associates file suffixes to file types and file types to applications, that are supposed to start it.

If you open a CMD window and you type

assoc .py

You should get an output similar to: (I don't have a windows machine nearby, so can't tell you the exact output) .py=Python.File

Then type ftype Python.File or whatever the previous command gave you and you should see which executable shall be used.

This should be something like

c:\System32\py.exe

which is a wrapper program, calling the real python executable according to some rules

If this doesn't work, then please tell which version of python you installed and how you installed it (for all users, for current user, ...)

From command line you have to call (If I recall correctly)

start test.py and it will execute the file with the associated executable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.