Oli
Oli

Reputation: 57

How to ensure that needed packages are always installed/available

I'm working on a script in python that relies on several different packages and libraries. When this script is transferred to another machine, the packages it needs in order to run are sometimes not present or are older versions that do not have the same functionality and cause the script to fail.

I was considering using a virtual environment, but I can't find a way to have the script use the specific environment I design as it's default, and in order to use the environment a user must manually activate it from the command line.

I've also looked into trying to check the versions of the packages installed on the machine, and if they are not sufficient then updating them from the script as described here:

Installing python module within code

Is there any easier/surefire way to make sure that the needed packages will always be available regardless of where it's run?

Upvotes: 0

Views: 1285

Answers (3)

Oli
Oli

Reputation: 57

The solution that I've been using has been to include a custom library (folder with all of my desired packages) in the folder with my script, and I simply import them from there:

from Customlib import pkg1, pkg2,...

As long as the custom library and script stay together in the same folder, it will always have access to the right packages and the correct versions of those packages.

I'm not sure how robust this solution actually is or what possible bugs may arise from this if it is passed from machine to machine, but for now this seems to work.

Upvotes: 0

gfcs
gfcs

Reputation: 106

If the problem is ensuring the required packages are available in a new environment or virtual environment, you could use pip and generate a requirements.txt and check it in version control or use a tool to do that for you, like pipenv.

If you would prefer to generate a requirements.txt by hand, you should:

  1. Install your depencencies using pip
  2. Type pip freeze > requirements.txt to generate a requirements.txt file
  3. Check requirements.txt in you source management software

When you need to setup a new environment, use pip install -m requirements.txt

Upvotes: 1

silver
silver

Reputation: 386

The normal approach is to create an installation script and have that manage your dependencies. Then when you move your project to a new environment your installer will check that all dependencies are present. I recommend you check out setuptools: https://setuptools.readthedocs.io/en/latest/

If you don't want to install dependencies whenever you need to use your script somewhere new, then you could package your script into a Docker container.

Upvotes: 1

Related Questions