Distributing Python application as a debian package, but as service - python

Hello I'm trying to create a debian package for my application, well this is th structure:
Project
|start.py
|ProyectPackage2/...
|ProyectPackage1/...
|DataExtra/...
|Settings/service.cfg
When I want to run my app I write:
python start.py
and it works.
but I want to distribute my app in a debian package and I want the following:
1- After installing debian package I want have a new command into /usr/sbin/ like my_service
2- Extra Content must located into /usr/lib/my_service/extracontent
3- Service must run when package were installed
I have tried
python stdeb
and it create a debian structure for packaging but, i'd like add script to do: create command, copy content, service install.
Thanks in advance.

In your debian/ folder, you need the following files to solve your problem:
init with your init script. This file will be installed to /etc/init.d/myservice and will be started automatically at boot. You can (and should) use /etc/init.d/skeleton as a template for your init script.
install with a list of files to be installed, along with target directories. For example
DataExtra/* usr/lib/my_service/extracontent
might fit your project. Check the manual page of dh_install.
Other files that you need are debian/rules, debian/control and debian/changelog (you need to change control and changelog according to your project).
Check the Debian New Maintainers' Guide for more detailed instructions.

Related

How to export a python project with its full dependency tree for execution anywhere

Say you have a project, and you want to export it so that you can run it on another machine that:
You don't have root access on
You cannot assume any python packages to be installed other than python itself (not even pip)
Is there a way to export the project, even if it is just a simple script, with everything that it imports, and then everything that the imports need etc.
For example, my project uses a library called python-telegram-bot. It has a list of requirements, and I have tried running pip -r requirements.txt --target myapp to install the requirements into the app's folder, but this is not recursive. For example, requests is not in the library, yet it is needed by the app. And if I manually add requests, there are things that requests needs that aren't part of that.
Is there a way to collect every last bit of requirements into a single folder so that my script functions on an entirely vanilla installation of python?
Pyinstaller creates an executable and you can either roll all dependencies into 1 file (makes it a bit slow to load though) or create a folder with all the packages, modules, imports etc. your script will need to run on any machine. You can Google the docs for pyinstaller, it's all pretty well covered.
Hope that helps, Kuda

How to pack simple Python application?

I have a simple Python GUI application (with a standard structure, similar to https://github.com/kennethreitz/samplemod) it consists of several .py files in a subdirectory. I would like to send the folder with all the source code files to person B so he could easily run it.
I need to zip the folder and transfer it on physical drive, so
hosting on PyPI is not possible.
Person B is likely to have python installed, but providing a simple way of
how he can install dependencies would be nice. Person B might have
Linux, Windows or MacOS.
It is preferable for person B to be able to run the application in
an easy way (preferably by running a script or few commands) that is
considered a good-practice. I have read that for
example altering PYTHONPATH is not a good practice.
Please explain in which format would YOU want to receive such application folder to be able to easily run it (consider that you want to run it once and then delete it and forget about it).
How should I prepare the application? What instructions should I give to person B?
Add a file to your application that contains your dependencies (can be generated with pip freeze for instance). All of your dependencies can then be installed by pip install -r yourDependencyFile.txt
You can also easily make a bash-script or something similar for this that installs the dependencies and then starts the application. This will depend on your setup, but it could be as easy as
pip install -r dependencies.txt && python main.py

How to "build" a python script with its dependencies

I have a simple python shell script (no gui) who uses a couple of dependencies (requests and BeautifulfSoup4).
I would like to share this simple script over multiple computers. Each computer has already python installed and they are all Linux powered.
At this moment, on my development environments, the application runs inside a virtualenv with all its dependencies.
Is there any way to share this application with all the dependencies without the needing of installing them with pip?
I would like to just run python myapp.py to run it.
You will need to either create a single-file executable, using something like bbfreeze or pyinstaller or bundle your dependencies (assuming they're pure-python) into a .zip file and then source it as your PYTHONPATH (ex: PYTHONPATH=deps.zip python myapp.py).
The much better solution would be to create a setup.py file and use pip. Your setup.py file can create dependency links to files or repos if you don't want those machines to have access to the outside world. See this related issue.
As long as you make the virtualenv relocatable (use the --relocatable option on it in its original place), you can literally just copy the whole virtualenv over. If you create it with --copy-only (you'll need to patch the bug in virtualenv), then you shouldn't even need to have python installed elsewhere on the target machines.
Alternatively, look at http://guide.python-distribute.org/ and learn how to create an egg or wheel. An egg can then be run directly by python.
I haven't tested your particular case, but you can find source code (either mirrored or original) on a site like github.
For example, for BeautifulSoup, you can find the code here.
You can put the code into the same folder (probably a rename is a good idea, so as to not call an existing package). Just note that you won't get any updates.

Getting RabbitMQ and Graphite to connect

I'm trying to connect RabbitMQ to Graphite(0.9.9) using https://github.com/somic/graphite-rabbitmq
However, I'm not entirely sure which directory in Graphite the graphite-rabbitmq files should be placed.
When I run carbon-agent-rabbitm1.py I get
Failed to import the graphite package. Please verify that this package
was properly installed and that your PYTHONPATH environment variable
includes the directory in which it is installed.
For example, you may need to run the following command:
export PYTHONPATH="/home/myusername/lib/python/:$PYTHONPATH"
Help would be very much appreciated
The convention with python modules, you can just put the downloaded files in an arbitrary temp directory, cd into that directory, and run:
python setup.py install
The standard distutils package will do the work of making sure everything gets to the right place.
You may also want to download pip which will manage the process of downloading and installing these packages for you, in which case you can then just type:
pip install graphite-web
But aside from all that, you really can put the files anywhere, so long as you add the directory to an environment variable called PYTHONPATH, just like the error message you quoted says.
Unless you've got a specific reason for using the scripts in that github repo, I wouldn't as they appear to be about 3 years old and graphite now has support for RabbitMQ by way of the Twisted Python AMQP library (txamqp), which makes those scripts completely unnecessary.

How would I package and deploy Python application like Mercurial hooks?

In this case, the application consists of one or more Python files, plus a settings.ini file. Now the Python files when being installed need to be installed in ~/.hg (as default) or prompted where the user want them installed. The installation also requires text to be appended to files like hgrc.
Is there already a specific Python package that does all of this, or if anyone has any experience in this area please share.
As far as I have looked, Python packaging refers to setuptools and easy_install.
The basis for packaging is a setup.py file. A problem with this is that such a setup file is used for a couple of dissimilar tasks:
Generating documentation.
Creating a release (source/binary).
Actually installing the software.
Combining these tasks in one file is a bit of a hazard and leads to problems now and then.
or distutils, but I am not sure if these packages support the notion of user prompting and deployment like appending text to existing files, and creating new ones.
I would include a custom script (bin/ command) which will poke the users' .hgrc and others. Doing it without the user consent would be rude.
User story
Install package: easy_install pkgname and this deploys myproject-init-hg (UNIX executable, can be also written in Python)
The installation finished and tells the user to run commmand myproject-init-hg
setup.py include mechanism to distribute and deploy bin/ style scripts.

Categories