ttc-cases / pydevx-lindjacob

1 stars 0 forks source link

Setup Python environment on startup #3

Closed github-actions[bot] closed 2 months ago

github-actions[bot] commented 2 months ago

[!NOTE] ☝️ Learning goals in this issue

  • understand the differences between pip3 install.. and pipenv install
  • install a dependency now - and in all future environments
  • extend the postcreate.sh script to add more nice post configuration

There are roughly two different approaches to setup your python environment.

In this tutorial we focus on pip rather than conda but they are both in the same category: Package mangers.

pipenv is also a package manager but it's more than that. It also handles isolated virtual environments so you can use pipenv to maintain different Python environments, allowing you to be totally independent of what ius actually installe on the machine you run from.

Let's dive into it.

pip uses requirements.txt file in your root (conda uses environment.yml and that was the last conda comment!). requirements.txt lists all your packages and dependencies, and you start your development by installing whatever is mentioned in requirements.txt directly on you system:

pip3  install -r requirements.txt

pipenv which uses Pipfile (and a Pipfile.lock file) in your root to setup an isolated virtual environment - you run it like this:

pipenv install

Both approaches are common; requirements.txt and pip install... is old and pipenv and Pipfile is designed to improve and replace it.

Since pip3 install directly manipulates your system wide Python this approach is considered a bit brute force and can implicitly source unwanted differences. Fair to say though; not so much when we use it in a devcontainer or docker image but still not the contemporary recommended approach.

However they are a bit mutually exclusive. If you have setup your environment with pipenv install and then try to also install with pip3 install ... you will be stopped.

The way you use the virtual environment created with pipenv is either to open a shell in this environment:

pipenv shell
python my-script.py

or to use pipenv run as a prefix

pipenv run python my-script.py
head spin Consider this - which is absolutely valid and maybe even (sometimes) be meaningful ```shell pipenv shell pip3 install -r requirements.txt ``` 🤔

🏋️‍♀️ Exercise

  • [x] 👉 Add argparse and pytest to your environment 👈
pipenv install argparse
pipenv install pytest

This will both install the two dependencies and create (1st run) and update (2nd run) your Pipfile and Pipfile.lock for future use.

Try to run pipenv graph

🏋️‍♀️ Exercise

  • [x] 👉 Instruct the container to always load your environment on startup 👈

Open the .devcontainer/postcreate.sh script we created earlier and add the following:

pushd "$(git rev-parse --show-toplevel)" || exit 1
if [ -f "requirements.txt" ];then 
  pip3 install --break-system-packages --user -r requirements.txt || echo No "requirements.txt" file
fi
if [ -f "Pipfile" ];then
  pipenv install || echo no "Pipfile" file
fi
popd || exit 1 

Test your devcontainer by rebuilding it and when it's back up run pipenv graph

git add, commit - remember to add a clever commit message.

🏋️‍♀️ Exercise

  • [x] 👉 generate the requirements.txt from the Pipfile 👈

Run and explore the output from:

pipenv -h

Run:

pipenv requirements

The output is exactly what pip3 install would expect to read from a requirements.txt

So you can pipe the output into requirements.txt like this:

pipenv requirements > requirements.txt

To test this setup try to delete the Pipfile and then rebuild the devcontainer

Maybe we want to go with the flow and prioritize pipenv over the requirements.txt approach. So lets go back:

git reset --hard HEAD
git clean -f

Lets wrap up this development branch and deliver it to main

lakruzz commented 2 months ago

hej @lindjacob

Tager lige din chat fra LinkedIn herover:

I Issue #3, skal jeg installere requirements.txt, men når jeg følger instruktionerne får jeg en fejlen "externally-managed-environment". Når jeg læser om det, virker det til jeg først skal oprette et virtuelt miljø. En anden løsning foreslået i fejlbeskeden er: "note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages."

Kan du pege mit i den rigtige retning?

Du er helt på rette spor, det strider mod den logik der ligger i at lave et virtuelt miljø, også at lave en pip install.

Men nogen gange kan det alligevel give menning. Måske en spændende diskussion: "hvornår" har man brug for begge dele?

I det konkrete tilfælde har du ikke meget andet valg end at tilføje --break-system-packages.

MEN SÅ - til allersidst viser det sig, at det hele bare var en drøm 💤 alt det der requirements.txt var bare en øvelse:

git reset --hard HEAD
git clean -f

🤷 🤣

lindjacob commented 2 months ago

Hahaha det er så fedt! Du får mig virkelig til at dykke ned i noget spændende her :D