Python Dependency – Surviving and Supplementing

Python Dependency – Surviving and Supplementing

Python is a great language that does a lot of things very well. One of the things it struggles with however is managing dependencies. Like many other languages, there have been a number of tools that have been developed to help out with this pain point.<a href="https://pip.pypa.io/en/stable/quickstart/" target="_blank">&nbsp;Pip and Pip3</a>&nbsp;are probably the most dominant package installers for Python right now. Today we are going to go over how you can take your running application and ensure that it still works somewhere else, using Pip and a few tips.

Python is a great language that does a lot of things very well. One of the things it struggles with however is managing dependencies. Like many other languages, there have been a number of tools that have been developed to help out with this pain point. Pip and Pip3 are probably the most dominant package installers for Python right now. Today we are going to go over how you can take your running application and ensure that it still works somewhere else, using Pip and a few tips.

I’m going to be using Python3’s PyMySQL as the specific dependency that we will be focused on carrying over, however this method will work for nearly every Python dependency on your machine. First we need a script that has a dependency that isn’t covered in the Python3 Standard Library, like PyMySQL:

#!/usr/bin/python3
import pymysql
print("If we made it this far then it works!")


THE PROBLEM


When we actually try to run our script we will get a message like this:

Traceback (most recent call last):
 File "./myScript.py", line 3, in <module>
 import pymysql
ModuleNotFoundError: No module named 'pymysql'

Typically when you see “ModuleNotFoundError” or “ImportError”, that means you need to install something to make it work. Python’s error messages are great and it gives us the exact module to install. Let’s do that now:

slingbox:~$ pip3 install pymysql
Collecting pymysql
 Using cached PyMySQL-0.8.0-py2.py3-none-any.whl
Installing collected packages: pymysql
Successfully installed pymysql-0.8.0

Now that we have the module installed, let’s see if the script runs:

slingbox:~$ ./myScript.py 
If we made it this far then it works!

Ok great! Our script works on this machine, but what about when we send it off to someone else to run? 


PIP FREEZE AND REQUIREMENTS.TXT


We can’t assume they will know what they are doing with missing modules, so this is our chance to help them out a bit. We have to first know what we have before we can start saying what they should have, and luckily there are commands to make this easy. To see all the currently installed Python packages, you can run ‘pip freeze’ or ‘pip3 freeze’ depending on your version. When you run this, you will get the package names along with their version number, like this:

slingbox:~$ pip freeze
asn1crypto==0.22.0
cryptography==1.9
enum34==1.1.6
idna==2.5
ipaddress==1.0.17
keyring==10.4.0
keyrings.alt==2.2
pycrypto==2.6.1
pygobject==3.24.1
pyxdg==0.25
SecretStorage==2.3.1
six==1.10.0

The format of these installed modules are very important and we will be coming back to this in a bit. To have this command write the output to file, just add a “>” and the filename you would like to be written to, like this:

pip3 freeze > requirements.txt

“requirements.txt” is a mostly agreed upon standard for naming this file, however any name can work. Just realize that the person reading your code and files should intuitively know what they are and what they are for. We could send this to our recipient but it would probably cause more confusion than sending nothing at all. Luckily pip/pip3 has more tricks up their sleeve.

pip3 install -r requirements.txt

If you ran this today, it would fail. Pip isn’t perfect and they know it. Pip can’t resolve every dependency out there, even most that are included with Python. Bernhard Vallant gives some great advice from stackoverflow.

What this all means, is that we can still use the requirements file, but we should trim it accordingly. You should probably understand what code you are sending to a user, and you should also know the dependencies are required to run the code. We know that we are going to want PyMySQL installed for the user when they go to run the script. For this case, let’s make a requirements.txt file with just the PyMySQL line that came from ‘pip3 freeze’. Our requirements.txt file now looks like this:

PyMySQL==0.8.0


INSTALLING THE REQUIREMENTS.TXT FILE


If we run this on our system, we get this output:

slingbox:~$ pip3 install -r requirements.txt 
Collecting PyMySQL==0.8.0 (from -r requirements.txt (line 1))
 Using cached PyMySQL-0.8.0-py2.py3-none-any.whl
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.8.0

When we run it on the target machine we will get something like this:

testbox1:~$ pip3 install --no-cache-dir -r requirements.txt 
Collecting PyMySQL==0.8.0 (from -r requirements.txt (line 1))
 Downloading PyMySQL-0.8.0-py2.py3-none-any.whl (83kB)
 100% |████████████████████████████████| 92kB 1.5MB/s 
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.8.0

SELF UPDATING SCRIPTS

Now PyMySQL is installed on the target machine! This is good, but we can do even better. Let’s run that command for the user when they go to run the script! Using a library like subprocess, we can actually have this script be “self-updating”:

#!/usr/bin/python3
import subprocess
subprocess.call(["pip3", "install", "-r", "requirements.txt"])
import pymysql
print("If we made it this far then it works!")

This is what we get when we run our script on a machine that does not have PyMySQL installed:

testbox1:~$ ./myScript.py 
Collecting PyMySQL==0.8.0 (from -r requirements.txt (line 1))
 Using cached PyMySQL-0.8.0-py2.py3-none-any.whl
Installing collected packages: PyMySQL
Successfully installed PyMySQL-0.8.0
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
If we made it this far then it works!

Now that’s awesome! This let’s us take our dependencies with us to some degree. We will still have to go download them but at least we don’t have to run the pip install commands one by one anymore. Requirements files are pretty commonplace among the Python community and they are understood pretty well. Do your users and yourself a favor by including a requirements file!

If you liked this post, please let me know and I appreciate you taking your time to read it. Feel free to check out my website at https://kodeslingr.io. Thanks.


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Guide to Python Programming Language

Guide to Python Programming Language