Eugene Sajine
Eugene Sajine

Reputation: 8200

How to create, share and run python programs with pip and virtualenv

I have created my program using virtual env. It is working in my project folder fine. Now i need to take this program and release it to the production environment that is supposed to be accessible by everybody.So this program should be runnable as is or it might be incorporated into other programs as a step. How am i supposed to deploy it? Zip the whole project folder? Is it possible to do without requiring clients to copy it and then unzip and run? Or the only way is to create a commonly accessible script that automates unzipping of the thing and configuring virtual env and then running it or there is a smarter way?

More complicated scenario is when it supposed to be used as library. How to deploy it so others could specify it as their dependency and pick it up? Seems like the only way is to create your own PyPi-like local repository - is that correct?

Thanks!

Upvotes: 1

Views: 556

Answers (2)

Eugene Sajine
Eugene Sajine

Reputation: 8200

So here is what i have found:

If we have a project A as API:

  1. create a folder where you will store the wheels (~/wheelhouse)

  2. using pip config specify this folder as one to find links in http://www.pip-installer.org/en/latest/configuration.html

i have:

[global]

[install]
no-index = yes
find-links = /home/users/me/wheelhouse

Make sure the wheel package is installed.

  1. In your project create setup.py file that will allow for the wheel creation and execute

    python setup.py bdist_wheel

copy the generated wheel to the wheelhouse so it has:

~/wheelhouse/projectA-0.1-py33-none-any.whl

Now we want to create a project that uses that projectA API - project B

we are creating a separate folder for this project and then create a virtual environment for it.

mkdir projectB; cd projectB
virtualenv projectB_env
source projectB_env/bin/activate
pip install projectA

Now if you run python console in this folder you will be able to import the classes from the projectA! One problem solved!

Now you have finished the development of projectB and you need to run it. For that purpose I'd recommend to use Pex (twitter.common.python) library. Pex now supports (v0.5.1) wheels lookup as dependencies. I'm feeding it the content of requirements.txt file to resolve dependencies. So as the result you will get the executable lightweight archived virtualenv that will have everything necessary for the project to run.

Upvotes: 2

Related Questions