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

[dec 20 merge] feat: Add execute python code lessons #97

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

ucodery
Copy link
Contributor

@ucodery ucodery commented Dec 11, 2024

No description provided.

conf.py Show resolved Hide resolved
Copy link
Member

@lwasser lwasser left a 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 Show resolved Hide resolved
running-code/execute-package.md Outdated Show resolved Hide resolved
Comment on lines 22 to 24
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Contributor Author

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Contributor Author

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-package.md Show resolved Hide resolved
Comment on lines 105 to 115
### 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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

Copy link
Member

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"?

Copy link
Contributor Author

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 😬 )

Comment on lines 135 to 137
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?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Contributor Author

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?

running-code/execute-script.md Outdated Show resolved Hide resolved
running-code/execute-script.md Outdated Show resolved Hide resolved
running-code/execute-script.md Outdated Show resolved Hide resolved
@lwasser lwasser changed the title Running feat: Add execute python code lessons Dec 12, 2024
@lwasser lwasser changed the title feat: Add execute python code lessons [dec 20 merge] feat: Add execute python code lessons Dec 12, 2024
Copy link
Contributor

@sneakers-the-rat sneakers-the-rat left a 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

running-code/execute-package.md Outdated Show resolved Hide resolved
running-code/execute-package.md Outdated Show resolved Hide resolved
running-code/execute-package.md Outdated Show resolved Hide resolved
running-code/execute-package.md Outdated Show resolved Hide resolved
running-code/execute-package.md Outdated Show resolved Hide resolved
### 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
Copy link
Contributor

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}

Copy link
Contributor Author

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

running-code/execute-script.md Outdated Show resolved Hide resolved
running-code/execute-script.md Outdated Show resolved Hide resolved
running-code/execute-script.md Outdated Show resolved Hide resolved
- 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
Copy link
Contributor

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

@ucodery
Copy link
Contributor Author

ucodery commented Dec 13, 2024

@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!

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

Successfully merging this pull request may close these issues.

3 participants