Closed github-actions[bot] closed 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
🤷 🤣
Hahaha det er så fedt! Du får mig virkelig til at dykke ned i noget spændende her :D
There are roughly two different approaches to setup your python environment.
pip
orconda
)pipenv
)In this tutorial we focus on
pip
rather thanconda
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 usepipenv
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
usesrequirements.txt
file in your root (conda
usesenvironment.yml
and that was the lastconda
comment!).requirements.txt
lists all your packages and dependencies, and you start your development by installing whatever is mentioned inrequirements.txt
directly on you system:pipenv
which usesPipfile
(and aPipfile.lock
file) in your root to setup an isolated virtual environment - you run it like this:Both approaches are common;
requirements.txt
andpip install...
is old andpipenv
andPipfile
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 withpip3 install ...
you will be stopped.The way you use the virtual environment created with
pipenv
is either to open a shell in this environment:or to use
pipenv run
as a prefixhead spin
Consider this - which is absolutely valid and maybe even (sometimes) be meaningful ```shell pipenv shell pip3 install -r requirements.txt ``` 🤔This will both install the two dependencies and create (1st run) and update (2nd run) your
Pipfile
andPipfile.lock
for future use.Try to run
pipenv graph
Open the
.devcontainer/postcreate.sh
script we created earlier and add the following: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.Run and explore the output from:
Run:
The output is exactly what
pip3 install
would expect to read from arequirements.txt
So you can pipe the output into
requirements.txt
like this:To test this setup try to delete the
Pipfile
and then rebuild the devcontainerMaybe we want to go with the flow and prioritize
pipenv
over therequirements.txt
approach. So lets go back:Lets wrap up this development branch and deliver it to
main