Include a library in python application - python

I am trying to find a way to include a library in a Ubuntu/Python/PyGtk application even though It is not included in the stock python enviornment and I can't add it as a dependency in my setup.py file. The library is called pygal and does have a PIP package (No available .deb or ppa) but I was looking for a way to include it in my application. I would think I could just include the source .py files to the library but I am unsure of how to go about including it in my code. Pygal also requires the lxml python library. I can install it via pip on my machine and it works fine but didn't know if there was anyway to automate or include this in my .deb package's setup.py file. Any help or advice would be greatly appreciated.

I think this should work though I'm not sure.
You put the folder with the name pygal from the pygal egg in your application directory. You can take it from your python installation - it should have this path:
/usr/local/lib/python2.7/dist-packages/pygal-0.13.0-py2.7.egg/pygal
or if you use python 3.1
/usr/local/lib/python3.1/dist-packages/pygal-0.13.0-py3.1.egg/pygal
or download it from pipy.
You can use it like any python library:
from pygal import *

I've actually figured out how to package this as a *deb file that I can include in my future PPA (or possibly submit to the community) that will (hopefully) cleanly install the python package on the system. I found a great write up in the Debian wiki that actually doesn't seem cryptic to a newbie like myself, oddly enough.
http://wiki.debian.org/Python/Packaging#Example_1:_Python_extension

Related

My MANIFEST.in content installed to distinct location from rest of package

I am creating a package in python with setuptools. I am using a MANIFEST.in file to include a dependency that my package utilizes. I specify the contents of the MANIFEST.in as below:
recursive-include package/dependency_directory *
The package builds and installs without error. However, when I call the package from one of the entry points I see that the script fails because it cannot find the correct filepath to the contents of MANIFEST.in.
The path that the package is looking at is this:
/Users/Xerez/Library/Caches/Python-Eggs/qasar-0.1-py2.7.egg-tmp/qasar/fastqc/fastqc
The rest of the package is installed at:
/Users/Xerez/anaconda2/lib/python2.7/site-packages/qasar-0.1-py2.7.egg
However, inside my IDE when I ask where the package is using the code below:
DATA_PATH = pkg_resources.resource_filename('qasar', 'fastqc/fastqc')
I get the following filepath:
/Users/Xerez/anaconda/lib/python2.7/site-packages/qasar-0.1-py2.7.egg/qasar/fastqc/fastqc
When I run the script from within the IDE using this path everything works fine. I recognize that MANIFEST.in is installing in a distinct location from the rest of the package and that my IDE is calling yet another distinct form of python. How may I force the installation to put everything in the same version_of_anaconda/location?
I attach a link to a bitbucket repository which has a stripped down version of the package that may assist in answering this question.The link is also written below:
https://bitbucket.org/arp2012/stackoverflow/src/master/
I apologize if the answer to this question is trivial, but I haven't been able to find a solution to this problem for a bit and decided to ask for help. Thank you in advance!
Ultimately, I was able to solve the multiple python path issue by installing the package inside of a docker container that only had one version of python and anaconda installed. This solution, however, is neither ideal nor elegant.

If I install modules with pip, how can I make sure other people can run my program without having that module installed?

I'm making a program that uses PyMySql and I'd like people to be able to run my program without going through the manual installation of PyMySql, is there a way I can achieve that?
I've already tried compiling to .pyc but that doesn't seem to work, in fact when I uninstall PyMySql it doesn't work anymore.
PS: There probably are better languages to do that but it's a homework assignment for school and can't use anything but python, also sorry for my bad english
Since PyMySQL has MIT license, you can redistribute it without any legal issues and also is a pure python implementation so it doesn't matter on which operative system it runs.
Just go to your python library folder and look for the module folder and copy it to your project folder, after that you can uninstall and python should be able to import it from your project folder and you just need to send your assignment with the module included.
The python library folder varies depending on your operative system, you can look at this answer on how to find the module location.
Use cx_freeze, pyinstaller or virtualenv.
Or copy code and put in your. Read python import

Is there a way to embed dependencies within a python script?

I have a simple script that has a dependency on dnspython for parsing zone files. I would like to distribute this script as a single .py that users can run just so long as they have 2.6/2.7 installed. I don't want to have the user install dependencies site-wide as there might be conflicts with existing packages/versions, nor do I want them to muck around with virtualenv. I was wondering if there was a way to embed a package like dnspython inside the script (gzip/base64) and have that script access that package at runtime. Perhaps unpack it into a dir in /tmp and add that to sys.path? I'm not concerned about startup overhead, I just want a single .py w/ all dependencies included that I can distribute.
Also, there would be no C dependencies to build, only pure python packages.
Edit: The script doesn't have to be a .py. Just so long as it is a single executable file.
You can package multiple Python files up into a .egg. Egg files are essentially just zip archives with well defined metadata - look at the setuptools documentation to see how to do this. Per the docs you can make egg files directly executable by specifying the entry point. This would give you a single executable file that can contain your code + any other dependencies.
EDIT: Nowadays I would recommend building a pex to do this. pex is basically an executable zip file with non stdlib dependencies. It doesn't contain a python distribution (like py2app/py2exe) but holds everything else and can be built with a single command line invocation. https://pex.readthedocs.org/en/latest/
The simplest way is just to put your python script named __main__.py with pure Python dependencies in a zip archive, example.
Otherwise PyInstaller could be used to produce a stand-alone executable.
please don't do this. If you do DO NOT make a habit of it.
pydns is BDS licensed but if you try to "embed" a gpl module in this way you could get in trouble
you can learn to use setuptools and you will be much happier in the long run
setuptools will handle the install of dependencies you identified (I'm not sure if the pydns you are using is pure python so you might create problems for your users if you try to add it yourself without knowing their environment)
you can set a url or pypi so that people could upgrade your script with easy_install -U

Python MySQL and Django problem

I am having problems getting python/django to connect to a MySQL database. The error message is basically "Error Loading MySQLDb module: No module named MySQLDb".
This is a fresh install right off python.org, so I assumed that it would have the MySQLDb module included, but it does not seem to. I also can't seem to find the module or how to install it, except in some sleazy looking parts of the net.
Is there a central point for getting this module? Why isn't it in the standard install? Can someone point me to a tutorial or some such to get this module installed?
Newbie in python, MySQL and Django.
Thanks for help.
I believe http://pypi.python.org/pypi/MySQL-python/ is the Python module you need. In general, when looking for Python modules, http://pypi.python.org/ is where you should start (people will refer to it as either PyPI or "the cheese shop." If setuptools is installed (it may be already) then you can run easy_install MySQL-python.
As far as MySQL is concerned, you'll need to install that separately from a likely-looking package on http://dev.mysql.com/downloads/mysql/.
this is how i handled the issue on Fedora13:
you can get the module here: http://sourceforge.net/projects/mysql-python/
download to a convenient directory
read the README file
build the module, according to the instructions in the README, keeping in mind to use the version of python you are planning to use as an interpreter (2.6 for me). if there are more than one version of python, simply using 'python' will probably alias you into a particular version, which might not be the one you want.
after the build is complete, the .egg file will have been created and landed in a 'site-packages' directory associated with the version of python which was used for the build.
then, ensure that the .egg file created (you can see the install path in the output from the install) is placed on your PYTHONPATH
that did it for me, anyhoo...
good luck!
JR

How to run a clone of reddit.com website. Reddit.com source code gives error while implementing on Ubuntu 9.10 (karmic)

I am implementing the reddit.com source code on ubuntu karmic 9.10.
I have followed all the steps and in one step where i am using paster command it throws an error.
$paster shell example.ini
File "/usr/local/lib/python2.6/dist-packages/Pylons-0.9.6.2-
py2.6.egg/pylons/middleware.py", line 11, in
from webhelpers.rails.asset_tag import javascript_path
ImportError: No module named rails.asset_tag
I have checked for the version for webhelpers, its the latest installed.
Could anybody tell me that by installing another version of webhelpers will solve this problem?
Or Shall i modify the code?
Its a reddit.com source code written in python, using pylons framework.
I am unable to decide that whether by installing any previous version of webhelpers helps or shall i modify the code.
If there is anybody who have implemented the reddit.com website please help me or suggest me the best way.
thanks
SIA
You need to ensure that all the libraries needed by your Reddit clone are on Python's module search path. There are a lot of different ways to accomplish this. The easiest is probably to just use setuptools' easy_install command to install them (though this is my own personal least favorite way to install Python libraries, and also opens new cans of worms in the form of "Why not use distribute or pip instead?").
The next easiest way is probably to download and unpack the source code for the libraries you need and either a) setup.py install them or b) add their directories to your $PYTHONPATH or sys.path variables.
The documentation linked above has more info on how to control the module search path.

Categories