Install python module from within script

5851
11
Jump to solution
06-06-2016 02:50 AM
AnthonyCheesman1
Occasional Contributor II

I've got a small project that I'm working on that requires installation of the dropbox Python library, as the script needs to a write files to it.

Ideally I'm going to deploy this to a number of users either via a toolbox or a Python add-in.

The catch is that dropbox is not a standard module, and at Python 2.7.5, pip (which I would normally use to install new module) is not a standard module either.

What I'm wondering is:

1/ how would I programatically go about installing a new module from inside a running script; or

2/ is there a way I can bundle the module either into my code, or as part of my code package ('bundle of files') so that the script can find and install the missing module?

3/ I posted a similar question on Reddit and it was suggested that I look at 'freezing' my code (effectively convert to an .exe) - but I'm unsure whether this will allow the script to interact with the toolbox to access parameters etc. Would compiling the code to a .pyc allow this to work?

Any and all help gratefully received!

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Just the dropbox directory. You'll also need the urllib3 and requests directories and six.py file as they're 3rd party libraries required by dropbox, see the "Requires Distributions" section in dropbox 6.4.0 : Python Package Index

View solution in original post

11 Replies
Luke_Pinner
MVP Regular Contributor

You can distribute it with your code, i.e as a 'bundle of files'. The directory your script is in is part of the module search path - 6. Modules — Python 2.7.11 documentation

So you would have something like:

some_dir

+-- your_script.py

+-- dropbox

You can also modify the search path at runtime, i.e with sys.path.append('path/to/folder/dropbox/is/in')

AnthonyCheesman1
Occasional Contributor II

Thanks Luke - really good information to know.

At the risk of sounding like a complete dumb-arse - what installer files would I need to put in that location for the dropbox module to work? As I installed the module via pip from the command line, I'm not sure what files actually contribute to the library.

In my C:\Python27\Lib\site-packages folder, I have a /dropbox and /dropbox-6.3.0.dist-info folders - do these constitute the module as such?

0 Kudos
Luke_Pinner
MVP Regular Contributor

Just the dropbox directory. You'll also need the urllib3 and requests directories and six.py file as they're 3rd party libraries required by dropbox, see the "Requires Distributions" section in dropbox 6.4.0 : Python Package Index

AnthonyCheesman1
Occasional Contributor II

Fantastic! Thanks mate, you've saved me hours of chaos and confusion.

0 Kudos
AnthonyCheesman1
Occasional Contributor II

Postscript to this:

I have tested Luke's proposed fix and it works.

There were a couple of additional bits and bobs to copy in to get it to run, but I now have a script that is happily functioning on a clean install machine without have any external dependencies, which should also not cause any administrator type issues.

For future reference, I needed the following stored in the same folder as my .py file to get it functioning:

dropbox (folder)

pip (folder)

pkg_resources (folder) (note: duplicate as also inside pip folder)

requests

urllib3

Also six.py (file) (note:duplicated as also inside pip folder)

Huge thanks to Luke. As to packaging and making pretty, I think this is going to have to live inside a Python add-in toolbar, so all the modules etc are packaged together in one location to allow full portability.

0 Kudos
PaulDavidson1
Occasional Contributor III

Just curious, why did you need pip if you weren't installing any externals?

Just setting it up for future maybes?

0 Kudos
AnthonyCheesman1
Occasional Contributor II

Paul, can't give you a definitive answer on that other than I think it's one of the dependencies - which I found out when I got an error message of the effect of pip not being available. Once I dropped the pip directory in the same folder, it worked.

Whether or not it needs the whole module or not I don't know - but I'm putting this in the category of 'it's working - I don't know why - but I won't mess with it any more'.

PaulDavidson1
Occasional Contributor III

Interesting...  Sounds like something either is using a dependency in pip or might use pip to install missing dependencies or...  Regardless, it works which is what matters. 

thanks.

0 Kudos
Luke_Pinner
MVP Regular Contributor

You don't need to put everything in a python add-in (unless you want to of course...).  You can still have all the supporting modules in a single folder.

How would you prefer to run your script? As a toolbox tool or an addin?