Yes, now it is working. python -m cards98
Well... but only for invoking top level in console. It does not work for normal execution, like clicking 2 times __main.py__ with mouse.
This also makes debugging and testing harder, cause I have to change it always in __main__.py. Where can I use it? I think it just complicates everything.
This is, to my knowledge, the official (and only) way to structure a Python project. There are two to create an executable file to start everything.
Option 1: Native Script
Many Python projects offer a Bash script (on UNIX-like systems) or a Windows .bat file that will run the python3 -m cards98 command. I see this a lot.
Option 2: Python Script
This is the method I'd recommend, as it's the most portable.
Outside of your top-level package, you can write a separate Python script, such as run.py or cards98.py, and then use that to execute your main function.
For example, in cards98/__main__.py, you can put this...
defmain():# The logic for starting your application.
if__name__=="__main__":main()
And then, outside of the cards98 package, create the file cards98.py, with the following:
It shouldn't be messy. But, yes, you'd need to invoke your top-level package (not your top-level script.)
By the by, I recommend renaming
src
to your project name,cards98
, and then renaming the subpackage by the same name to something likegame
.Yes, now it is working.
python -m cards98
Well... but only for invoking top level in console. It does not work for normal execution, like clicking 2 times
__main.py__
with mouse.This also makes debugging and testing harder, cause I have to change it always in
__main__.py
. Where can I use it? I think it just complicates everything.Thanks for help in understanding this
This is, to my knowledge, the official (and only) way to structure a Python project. There are two to create an executable file to start everything.
Option 1: Native Script
Many Python projects offer a Bash script (on UNIX-like systems) or a Windows
.bat
file that will run thepython3 -m cards98
command. I see this a lot.Option 2: Python Script
This is the method I'd recommend, as it's the most portable.
Outside of your top-level package, you can write a separate Python script, such as
run.py
orcards98.py
, and then use that to execute your main function.For example, in
cards98/__main__.py
, you can put this...And then, outside of the
cards98
package, create the filecards98.py
, with the following:To start your Python application, just double-click
cards98.py
.P.S. Thanks for bringing up this situation. I realized I never addressed it in the book!