-
Notifications
You must be signed in to change notification settings - Fork 4
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
[dec 20 merge] feat: Add execute python code lessons #97
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Jonny Saunders <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ucodery this content is SOOOO good. I tried to add some clarification around some things that tripped up my brain while reading it. but take or leave or edit as you see fit. we can then merge this (if you want!!).
running-code/execute-package.md
Outdated
We have seen how The `python` command can be passed a file for execution, but it can alternatively be passed | ||
the name of a module, exactly as would be used after an `import`. In this case, Python will look up the module | ||
referenced in its installed packages, and when it finds the module, will execute it as a script. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have seen how The `python` command can be passed a file for execution, but it can alternatively be passed | |
the name of a module, exactly as would be used after an `import`. In this case, Python will look up the module | |
referenced in its installed packages, and when it finds the module, will execute it as a script. | |
In the [executive script lesson](executive-script), you learned that the `python` command could be passed a filename to be executed. Alternatively, you can also pass | |
the name of a module, exactly as would be used after an `import`. In this case, Python will look up the module | |
referenced in its installed packages, and when it finds the module, it will execute it as a script. |
when you say in its installed packages, do you mean in the user's environment? or could we clarify just a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I mean in the currently active (virtual or not) environment. I will try to better clarify this
running-code/execute-package.md
Outdated
path, but cannot be used in combination with a path, as there can only be one executing module. | ||
|
||
:::{tip} | ||
These commands both do the same thing, but one is much more portable, and easier to remember |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These commands both do the same thing, but one is much more portable, and easier to remember | |
The commands below both do the same thing, but one is much more portable, and easier to remember |
I'm not sure what you mean by is more portable. Could we just say one is easier to remember and use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by portable, I mean the latter works regardless of where packages are installed (which is an environment implementation detail) and on all filesystem types (also Windows). I could possibly make that point clearer.
running-code/execute-script.md
Outdated
### Pros of passing a file to `python`: | ||
- don't need execute permissions | ||
- works on every system | ||
- explicit about what you expect to happen | ||
|
||
### Pros of inserting a shebang to the file: | ||
- file is associated with specific python | ||
- don't have to remember which | ||
- don't have to use the `python` command | ||
- don't have to even remember it is a Python script | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### Pros of passing a file to `python`: | |
- don't need execute permissions | |
- works on every system | |
- explicit about what you expect to happen | |
### Pros of inserting a shebang to the file: | |
- file is associated with specific python | |
- don't have to remember which | |
- don't have to use the `python` command | |
- don't have to even remember it is a Python script | |
### Pros of passing a file to `python`: | |
- It's safe: You don't need to execute permissions | |
- It's reproducible: This approach work the same way on every system | |
- You are being explicit about what you want to happen | |
### Pros of inserting a shebang into your Python file: | |
- The file is associated with a specific Python installation (the installation associated with your currently active environment). | |
- You don't have to remember which Python you want to run as it will default to your current, active Python. | |
- You don't have to use the `python` command when calling the file | |
- You don't even have to remember that you are calling a Python script | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jeremy, I'm unsure about some of the above. What do you mean that you don't have to remember you are calling a Python script? What do you mean by "remember"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the changes to the first section, but I think I need to work on the second section. The Pro of the shebang is that it doesn't have to be the currently active Python.
That might sound odd, but it means you can execute a command where the shebang is like #!/usr/local/bin/python
(which might be the system python, let's say version 3.11) even while inside of an active virtual environment that is not meant to handle that code (say we are in a 2.7 venv 😬 )
running-code/execute-script.md
Outdated
You may not have expected it to print the hello twice, but it did. This is because `my_program` is set to | ||
_always_ call `shiny_hello`, and now `guess_my_number` also calls it. That's two times. How can we make | ||
`my_program` only call `shiny_hello` when it is used as a script? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may not have expected it to print the hello twice, but it did. This is because `my_program` is set to | |
_always_ call `shiny_hello`, and now `guess_my_number` also calls it. That's two times. How can we make | |
`my_program` only call `shiny_hello` when it is used as a script? | |
You may not have expected it to print the hello twice, but it did. The line prints twice because `my_program` is designed to | |
_always_ call `shiny_hello`. Now `guess_my_number` also calls `shiny_hello`. | |
```python | |
def shiny_hello(): | |
print("\N{Sparkles} Hello from Python \N{Sparkles}") | |
# my_program runs the shiny_hello function | |
shiny_hello() |
This is why the line prints twice! How can you make
my_program
only call shiny_hello
when it is run as a script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think your suggestion was mis-typed?
Co-authored-by: Leah Wasser <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love it :):):)
i wouldn't say any of my comments here are blockers, take 'em or leave em as always
### Executing Python scripts on macOS / Linux / Non-Windows | ||
|
||
On Linux or Mac systems, the Python file can itself be turned into a command. By adding a [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) | ||
as the first line in any Python file, and by giving the file [executable permissions](https://docs.python.org/3/using/unix.html#miscellaneous) the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs a little bit more scaffolding - what is in the shebang? just a little bit more of a hint like "the shebang points to another cli program, and gives the path to the file as its first argument. the /usr/bin/env
part does [...]. so this shebang is equivalent to /usr/bin/env python {file_path.py}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this suggestion, but I came up blank myself trying to rewrite this paragraph
- You don't have to remember when using the command which Python installation the script should be associated with, or even that the script is written in Python | ||
|
||
(execute-script-name-eq-main)= | ||
## Separating script from import behavior |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya ya as said above i think this works better in the executing packages lesson since importing scripts is super fragile and ime rare
Co-authored-by: Jonny Saunders <[email protected]>
@lwasser @sneakers-the-rat these reviews are great!! I don't think I managed to get to all your points, but the lesson is looking better already! |
No description provided.