Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hello world! from Command Prompt Error #12

Closed
MarOPhotos opened this issue May 1, 2024 · 9 comments
Closed

Hello world! from Command Prompt Error #12

MarOPhotos opened this issue May 1, 2024 · 9 comments

Comments

@MarOPhotos
Copy link

Eric, just started on your book Python Crash Course 3rd Edition.

On page 12 about getting Hello world! to work on Windows Command Prompt, when inside the correct directory, I only needed to type the command hello_world, instead of hello_world.py.

@ehmatthes
Copy link
Owner

Thank you for letting me know. I believe your system recognizes .py files as executable files, and knows where to find the Python interpreter when it needs to run a .py file.

@MarOPhotos
Copy link
Author

Yes, was a little bit frustrating when I can’t even execute the first “Hello world” assignment. Almost grabbed me a book of py for dummies. This is for those who shall share my similar faith and to never give up 😁
Thank you for responding Eric.

@ehmatthes
Copy link
Owner

Are you saying this worked:

> hello_world

But this did not:

> python hello_world.py

Or was it this?

> python hello_world

@MarOPhotos
Copy link
Author

The second one.
python hello_world

@ehmatthes
Copy link
Owner

Okay, that's interesting. Are you sure you saved your file as hello_world.py, and not hello_world?

For a simple script like hello world, the .py extension isn't always needed:

~/test_code/py_ext_tester$  ls -alh     
-rw-r--r--    1 eric  staff    23B May  3 08:31 hello
-rw-r--r--    1 eric  staff    23B May  3 08:31 hello.py
-rw-r--r--    1 eric  staff    23B May  3 08:34 hello.txt
~/test_code/py_ext_tester$  python hello.py
Hello world!
~/test_code/py_ext_tester$  python hello.txt
Hello world!
~/test_code/py_ext_tester$  python hello
Hello world!

This example is on macOS, but the same thing should apply to Windows.

It gets confusing because file browsers such as Windows Explorer used to show all file extensions by default. Now they tend to hide extensions unless you configure them to show all extensions. So you might see hello_world in Explorer, but it might actually be hello_world.py. Even more confusing, people will sometimes see hello_world.py in Explorer, when the file is actually named hello_world.py.py!

You can check the filename definitively in the terminal with the dir command. Can you run dir in the directory where your hello world file is saved, and share the output?

@MarOPhotos
Copy link
Author

That is a good point. I’ll check the file name when i get home later and share the output. I remember saving it as hello_world.py from the vs window though it dropped the .py when it went windows to save as i recall but i did not put much thought to that as my default does not show the file extensions.

@MarOPhotos
Copy link
Author

C:\Users\marop\OneDrive\Desktop\python_work>dir
Volume in drive C is BOOTCAMP
Volume Serial Number is 0246-9FF7

Directory of C:\Users\marop\OneDrive\Desktop\python_work

05/03/2024 04:39 AM

.
05/03/2024 04:39 AM ..
05/01/2024 05:15 PM .vscode
05/01/2024 09:49 PM 399 bicycles
05/03/2024 04:39 AM 1,235 Cars
05/01/2024 06:05 PM 182 hello_world
05/03/2024 05:20 AM 589 lapamilya.py
05/01/2024 06:12 PM 172 mensahe.py
05/01/2024 09:22 PM 433 name.py
05/01/2024 07:04 PM 249 name_cases.py
05/01/2024 07:10 PM 392 name_quotes
05/01/2024 09:21 PM 223 numbers
05/01/2024 09:32 PM 13 Zen_Python
10 File(s) 3,887 bytes
3 Dir(s) 422,718,844,928 bytes free

C:\Users\marop\OneDrive\Desktop\python_work>/test_code/py_ext_tester$ ls -alh
'
' is not recognized as an internal or external command,
operable program or batch file.

@MarOPhotos
Copy link
Author

You're right. Apparently, I saved it as hello_world, and not hello_world.py

@ehmatthes
Copy link
Owner

I'm glad we got that sorted. :)

I see numbers in there. There's a module in the Python standard library named numbers.py. If you end up needing that module, or using a library that depends on that module, this can cause some bugs that can be hard to sort out. You might consider renaming that file.

I'll admit I had a file called numbers.py in the first edition of PCC, and it caused some issues for some readers. 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants