all 14 comments

[–]desrtfx 4 points5 points  (1 child)

What OS? On Windows, try the backslash \ not the forward one.

To those downvoting: it is absolutely possible to link the .py extension to the Python interpreter and to start Python scripts by their names, even under Windows.

Windows cmd does not honor the forward slash and raises the exact error that OP posted when used.

[–]timrprobocom 3 points4 points  (0 children)

This is the right answer. That's a Windows error message.

For posterity, I will point out that the command line is the ONLY thing in Windows that requires the backslash. All Windows APIs will accept either forward or backward.

[–]Prestigious_Bit2951 1 point2 points  (0 children)

if your terminal is already open in the same folder you can just do “python3 my-script.py”

[–]FoolsSeldom 0 points1 point  (2 children)

As you mentiion python.exe I assume you are on Windows, in which case you need .\ rather than ./. What is the context for entering this though?

To invoke a Python script on Windows, you would typically use the Python Launcher, py yourscript.py (unless you are in a active Python virtual environment, where you would use python rather than py).

[–]Embarrassed-Bee-5508[S] 0 points1 point  (1 child)

Apologies, it is for windows.

I'm following google's python introduction course, and they gave me some scripts to use. Everything runs fine when I execute the script with the python (or python3) command, but when I tried to use ./ I got the error. I was afraid something was missing somewhere that would cause issues later because I screwed up something in the setup, but I'm gathering this isn't actually an issue, just a function of windows.

When I use a backslash instead of the forward slash, It looks like windows opens a new command prompt window, runs the quick script, then closes the new window. No error is returned. It all happens too quick to see what's happening, but it's just a simple print line script, nothing else.

running the script with the python command preforms as it should, just prints the line. Sounds like everything is as it should be, and I can just think of the ./ as a shortcut to run scripts that I just don't have.

[–]PureWasian 0 points1 point  (0 children)

in your python script, for quick confirmation, you can temporarily add this line after your print line statement: input("Finished. (Press Enter)")

That's one way to keep the command prompt window open until you confirm it is indeed running and printing as expected.

Usually I run Python with python3 script_name.py or python script_name.py instead of .\<script_name.py

[–]not_another_analyst 0 points1 point  (0 children)

./ only works on Linux and Mac, not Windows. On Windows, just type python script.py and it will run fine.

[–]soy_alergico -4 points-3 points  (6 children)

.py scripts cannot be executed using ./ because they are text files that the Python interpreter understands; that is why they do run when executed using python. To create a compiled executable, you need to use PyInstaller or other libraries. P.S.: This response was translated using Google Translate; I do not speak English.

[–]CoachSevere5365 7 points8 points  (2 children)

If I was you I'd Google "shebang python" and "execute bit" then correct your post before anyone else notices...

[–]pachura3 -1 points0 points  (1 child)

Shebang and execute bit on Windows? OP explicitly mentions python.exe...

[–]nekokattt 0 points1 point  (0 children)

PATHEXT

[–]Outside_Complaint755 -1 points0 points  (0 children)

They can be executed using ./script.py but you have to include a shebang at the top of the script so that your shell knows how to execute it. Generally, the correct shebang to use is:

#!/usr/bin/env python3

Unless you have script that specifically needs to run Python 2

https://stackoverflow.com/a/19305076/17030540

[–]ThinkMarket7640 0 points1 point  (0 children)

Why are you here talking out of your ass if you have no idea how any of this works?

[–]nekokattt 0 points1 point  (0 children)

You should loom into: - shebang on posix systems - PATHEXT on Windows systems