Sublime Forum

Directory Issue Python

#1

I just installed Sublime and I am working through the book “Python Crash Course” by Eric Matthes. I am attempting to run a simple hello world program to make sure everything is in order, but I keep getting the following message:

python: can’t open file ‘/Users/hcgareis1/Desktop/hello_wolrd.py’: [Errno 2] No such file or directory
[Finished in 0.1s with exit code 2]
[shell_cmd: python -u “/Users/hcgareis1/Desktop/hello_wolrd.py”]
[dir: /Users/hcgareis1/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

Any help would be greatly appreciated, otherwise I will just use a different text editor.

0 Likes

#2

The error just says, python can’t find your hello_world.py. That’s not an text editor’s fault.

Did you save the file, before running the build system?

Can you run the file via shell?

The build system works fine on Windows.

Animation

0 Likes

#3

Yes, I saved the file and it ran properly via shell and Thonny. I am working on a Mac and I got it working when I built with the default Python, but not with Python 3. This is a problem for me because the default Python will not recognize f strings.

0 Likes

#4

Got it working myself, thank you for your help!

0 Likes

#5

You could just def main( );

That is a function meant for packages, even modules… Basically if you run a python module or package from the command line, main is the default thing it targets. It is useful for packages because you can set up example usage information within it, or an args system with /?, etc…

0 Likes

#6

I’m pretty sure main isn’t some special function in python. No function is targeted by default if you run a python file from the command line.

1 Like

#7

Indeed; Python executes the code top-down and has no concept of main() as C and other languages do; instead the common idiom is to detect when the module name is __main__ and then invoke some function (such as the example above), which is generally called main() but can be named anything you want.

From https://docs.python.org/3/library/__main__.html:

'__main__' is the name of the scope in which top-level code executes. A module’s name is set equal to '__main__' when read from standard input, a script, or from an interactive prompt.

A module can discover whether or not it is running in the main scope by checking its own __name__ , which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:

if __name__ == "__main__": 
    # execute only if run as a script 
    main()

For a package, the same effect can be achieved by including a __main__.py module, the contents of which will be executed when the module is run with -m .

1 Like