Search code examples
pythonpyramid

Deploy a pyramid application with sdist


I am new at pyramid framework and I recently started to play with it. However, I'm a bit confused about how a tarball created with 'sdist' gets installed in a production virtual environment. My scenario is as follows:

  1. After finishing a project I created in pyramid called 'myapp', I run: python setup.py sdist in order to create the distribution tarball.
  2. The tarball gets created under 'dist' folder and it contains all my project sources as well as the .ini files (development and production).
  3. I then create a new production virtual environment by executing: virtualenv --no-site-packages envprod
  4. To install the 'myapp' distribution tarball I execute: envprod/bin/easy_install src/myapp/dist/myapp0-0.tar.gz.
  5. It then starts to download and install all the requirements for the project and it also installs the sources of my application under envprod/lib/python2.7/site-packages/myapp

The problem is that neither development.ini nor production.ini are installed in the new prod environment so I have no way to execute 'pserve' since it needs the .ini file.

Am I doing something wrong? Or is there a way to start serving 'myapp' without the .ini files?

Thanks!


Solution

  • First of all you misunderstand what is product and configuration. Your .ini file isn't part of your application. The same way as nginx configuration isn't part of nginx distributions. So, generally you need to separate your product code from configuration. Depending on what do you prefer you can use virtualenv or buildout. I see that you already use virtualenv but it is only allow you to install python eggs, if you want to have more automation then you can find set of buildout recepies for pyramid that can make initial setup little easier.

    Usually .ini files contains database connection string, ports or path to log files folder. You can have example .ini file for your project but you don't need to store it inside of project eggs file (in your example myapp0-0.tar.gz.).

    Second part is how to distribute your project. You noticed that most python projects can be downloaded from PyPI (virtualenv do this). If you don't want to open source your work then you can install own PyPI server (just google it, there is plenty of examples). Then you will be able to deploy on your production server just using your own PyPI mirror and don't need to upload project files in public. Upload with this command:

    $ python setup.py sdist upload
    

    And install string on your server:

    (envprod)$ pip install -i http://my.mirr.or/path $PACKAGE
    

    Last part is how to organize skeleton for your production environment. You can make another project in your DCVS with .ini, requirements.txt files examples, initial folder structure and just clone to your production server. I prefer to use Makefiles to do initial work like setup virtualenv, run pip, download static jQuery (if you don't use CDNs for example).