Managing packages in Python has been a tricky subject. Whenever you create a program, you typically need to import other libraries user's have made. The typical way to do this is using pip install someoneslibrary
. This will install the library on your computer. Many problems can arise from this, but one example is the libraries are not very portable or localized to your program.
Say you install version 1 of somoneslibrary
and you write a new program using version 2. Both programs will be forced to use the same version if you install using pip
in the way I showed above. Let's take a look at how pipenv
solves this problem and others.
The Hitchhickers guide has a great example how to install pipenv
on many systems: https://docs.python-guide.org/dev/virtualenvs/#installing-pipenv . For our purposed, we will use pip
. Run the following:
pip install --user pipenv
You should now have pipenv
installed. Refer to the link above for platform specific instructions. Sometimes you will need to configure env variables for your system.
You can check to ensure pipenv
is installed correctly by doing the following:
pipenv --version
You should see details on your pipenv
install after.
OKay, let's start by creating a simple python program.
mkdir myProgram
cd myProgram
touch index.py
Now, let's say we want to use the popular requests
module for python. The package is localed here: https://2.python-requests.org/en/master/
We can install this using the following:
pipenv install requests
This will create a Pipfile
and Pipfile.lock
file. With these files in our directory, anyone can come to our folder and use pipenv install
to get all the packages we are using in our program. That makes our new package very portable.
Okay, now open index.py
and add the following code.
## index.py
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print(r.status_code)
To run our program, we use the following:
pipenv run python index.py
And there you have it! We can now use pipenv
to manage our application :D
Now, you may notice that running our program takes many more arguments from the command line. To solve this, we can use the classic make
program that comes on most operating systems.
To do this, create a file called Makefile
and add the following:
run:
pipenv run python index.py
Now, we can run our program using the following command.
make run
That is much easier. There is much more to do with make. Let me show you an example from a program I have written.
parseLogs:
pipenv run python index.py
runApp:
pipenv run python manage.py runserver
Here, I have two helper tasks I can run. If you are interested in Make, there are many good books and you can check out the documentation here: https://www.gnu.org/software/make/manual/make.html