6

I've developed a little script that searches through a wallpaper database online and download the wallpapers, I want to give this script to another person that isn't exactly good with computers and I'm kinda starting with python so I don't know how to include the "imports" of the third party modules in my program so it can be 100% portable, Is there something that can help me do this? or I will have to enter and analyse my third party modules and copy&paste the functions that I use?

2 Answers 2

4

Worse thing to do

An easy thing you can do is simply bundle the other modules in with your code. That does not mean that you should copy/paste the functions from the other modules into your code--you should definitely not do that, since you don't know what dependencies you'll be missing. Your directory structure might look like:

/myproject
    mycode.py
    thirdpartymodule1.py
    thirdpartymodule2.py
    thirdpartymodule3/
        <contents>

Better thing to do

The real best way to do this is include a list of dependencies (usually called requirements.txt) in your Python package that Python's package installer, pip, could use to automatically download. Since that might be a little too complicated, you could give your friend these instructions, assuming Mac or Linux:

  1. Run $ curl http://python-distribute.org/distribute_setup.py | python. This provides you with tools you'll need to install the package manager.
  2. Run $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python. This installs the package manager.
  3. Give your friend a list of the names of the third-party Python modules you used in your code. For purposes of this example, we'll say you used requests, twisted, and boto.
  4. Your friend should run from command line $ pip install <list of package names>. In our example, that would look like $ pip install requests twisted boto.
  5. Run the Python code! The lines like import boto should then work, since your friend will have the packages installed on their computer.
1
  • That domain (python-distribute.org) no longer exists. Care to update your answer? May 26, 2017 at 2:41
2

The easi(er) way:

  1. Start with a clean virtual environment.
  2. Install packages that you need to develop your code.
  3. Once you are done, create a list of requirements for your project.
  4. Send this file (from step 3) to your friend.

Your friend simply does pip install -r thefile.txt to get all the requirements for your application.

Here's an example:

D:\>virtualenv --no-site-packages myproject
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in myproject\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.

D:\>myproject\Scripts\activate.bat
(myproject) D:\>pip install requests
Downloading/unpacking requests
  Downloading requests-0.14.1.tar.gz (523Kb): 523Kb downloaded
  Running setup.py egg_info for package requests

    warning: no files found matching 'tests\*.'
Installing collected packages: requests
  Running setup.py install for requests

    warning: no files found matching 'tests\*.'
Successfully installed requests
Cleaning up...

(myproject) D:\>pip freeze > requirements.txt
(myproject) D:\>type requirements.txt
requests==0.14.1
3
  • You should probably note that this assumes Windows.
    – jdotjdot
    Oct 15, 2012 at 4:14
  • It works wherever virtualenv is available (basically, wherever Python is available); it is not Windows specific. Oct 15, 2012 at 5:05
  • virtualenv does, but your command line commands are Windows-specific (type, myproject\Scripts\activate.bat, etc.), and given that this is a question for newbies, I wouldn't want future ones reading to be confused about how to invoke virtualenv.
    – jdotjdot
    Oct 15, 2012 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.