Using google's protobuf in python without installing it - python

It seems to me that when I'm using protobuf in python I need to install it first so that I also have setuptools installed. To me it seems like this is severly limiting portability as I would have to install protobuf on every machine on which I want to use any kind of python code using protobuf.
So my question is: Is there a way to package protobuf for python in such a way, that it can be distributed with the python code using it?
Any info on this would be appreciated.

The package contains an experimental C++ extension, and the setup file generates Python files, but with the extension disabled by default, you should be able to include the setup.py build result with your script just fine.
Note that the Python package still needs the command-line tool to be installed. The tool is used to generate some Python for you.
Once that is available, run:
cd python
python setup.py build
and copy the build/lib/google directory to your script distribution, it needs to be on your sys.path to be importable.
Alternatively, use setup.py bdist --formats=zip and add the path to the resulting zipfile (located in dist/protobuf-<version>.<platform>-<architecture>.zip) to your sys.path. Renaming it should be fine.
Do note that the package uses a namespace, and thus the pkg_resources module needs to be available as well. It is only used to declare the google namespace in google/__init__.py.

Related

Installing specific versions of python dependencies for a package inside that package

I have an installable python package (mypackage) and it needs to use specific versions of a number of dependencies. At the minute I have some .sh scripts that just pip these into an internal package folder (eg C:\Python27\Lib\site-packages\mypackage\site-packages). When mypackage executes it adds this internal folder to the beginning of the python path so that it will override any other versions of the required dependencies elsewhere in the python path.
I know this will only work if the user doesn't import the dependencies prior to importing mypackage but I will document this.
I want to remove the .sh scripts and integrate the above into either dist_utils install or pip standard installation process. What is the best way to do this? I know about install_requires but it does not seem to allow specification of a location.
I eventually found the virtualenv tool which solved the above problem much more elegantly than the solution I'd put in place:
https://docs.python.org/3/tutorial/venv.html

How to use precompiled headers with python wheels?

I'm writing a python packages which I would like to distribute as a wheel, so it can be easily installed via pip install.
As part of the functionality of the package itself I compile C++ code. For that, I distribute with the package some set of header files for the C++ code to include. Now, in order to speed up those compilation operations I'd like to provide a precompiled-header as part of the package.
I am able to do this if the package is installed via python setup.py install because I can add a step after the installation that generates the precompiled header in the installation directory (some-virtualenv/lib/python3.5/site-packages/...) directly.
But now I can't figure out how to do this when I distribute a wheel. It seems to me that the installation process of a wheel is supposed to be a simple unpack and copy and provides me no way of performing some extra configuration on the installed package (that would generate that precompiled header for example).
As part of my search of how to do this I stumbled across this, but no solution is offered there.
Is there any way around this or am I forced to use a source distribution for my package?

Use Python without installing

I have an installer which uses a Python script to install several components. I do not want to install Python on the users computer if they do not already have it and I also do not want having Python installed to be a prerequisite for using my installer. Is there a way of downloading Python without using the installer and just running it from a directory which can be easily removed after the installation is complete?
Portable Python is an easy tool to use on Windows. If you want to create .exe programs use PyInstaller to compile them. They can both work on top of each other, you can compile (make .exes) using Portable Python, Portable Python 3 is also available.
If the installer is for OS X or Linux, Python shall be there usually. Otherwise
Lazy way: Detect if Python is existed. If not, ask user to install it as dependency. e.g. A link for python download page.
Rewrite your script. If the logic is not complicated, use some other build-in shell script is a good idea.
Static linking Python. Yes, static linking is evil. However, it's still an option.
Found some project maybe helpful on github and google-code
(In addition to Owens points). Use py2exe or one of the other exe builder utils for python on windows. On other platforms just use the python script.
Try cx_Freeze
This program can freeze your python code into a .exe file and some other pyd files, it can be run without installing python.
NOTE: You will need to install python to freeze, but you don't need python to run the program.
You can download from here.
https://anthony-tuininga.github.io/cx_Freeze/index.html
TO FREEZE:
make a file setup.py
type
from cx_Freeze import setup, Executable
setup (name='NEW_EXE_FILE_NAME',
executables = [Executable("xx.py")])
xx.py will be the python code you want to freeze.
command line: python setup.py build
You can use python's embeddable package, available here:
Python Windows Releases
Once you download the package all you need to do is extract it into a folder. That's it... Installed.
You also might want to install pip to manage packages by running the pip install script pip install script and add pip.exe's relative path to python3xx._pth file.
PatriTech made a nice summary of the process on YouTube: Embedded Python Installation

SQLite Library - Python Portability

I am writing some code that is intended to operate with a pre-bundled version of Python included with a program.
The issue is that it doesn't as yet include the Sqlite3 library which is a requirement of my code. Currently I have a wrapped that calls a system installed version of python2.7 to use the import.
Is there any way I can manually include or package this library to go with the code to make it more portable? My concern is that I require this to operate on Windows systems but it is less likely that they will have python2.7 available. (2.7 is the minimum as this included an update to the library)
Yes in python you have setup tools. You can use requirements.txt file and use pip install -r requirements.txt to install dependency.
pip link
pysqlite is also maintained as an external project, looks like they've got downloads all the way back to Python 2.3

What exactly does distutils do?

I have read the documentation but I don't understand.
Why do I have to use distutils to install python modules ?
Why do I just can't save the modules in python path ?
You don't have to use distutils. You can install modules manually, just like you can compile a C++ library manually (compile every implementation file, then link the .obj files) or install an application manually (compile, put into its own directory, add a shortcut for launching). It just gets tedious and error-prone, as every repetive task done manually.
Moreover, the manual steps I listed for the examples are pretty optimistic - often, you want to do more. For example, PyQt adds the .ui-to-.py-compiler to the path so you can invoke it via command line.
So you end up with a stack of work that could be automated. This alone is a good argument.
Also, the devs would have to write installing instructions. With distutils etc, you only have to specify what your project consists of (and fancy extras if and only if you need it) - for example, you don't need to tell it to put everything in a new folder in site-packages, because it already knows this.
So in the end, it's easier for developers and for users.
what python modules ? for installing python package if they exist in pypi you should do :
pip install <name_of_package>
if not, you should download them .tar.gz or what so ever and see if you find a setup.py and run it like this :
python setup.py install
or if you want to install it in development mode (you can change in package and see the result without installing it again ) :
python setup.py develop
this is the usual way to distribute python package (the setup.py); and this setup.py is the one that call disutils.
to summarize this distutils is a python package that help developer create a python package installer that will build and install a given package by just running the command setup.py install.
so basically what disutils does (i will sit only important stuff):
it search dependencies of the package (install dependencies automatically).
it copy the package modules in site-packages or just create a sym link if it's in develop mode
you can create an egg of you package.
it can also run test over your package.
you can use it to upload your package to pypi.
if you want more detail see this http://docs.python.org/library/distutils.html
You don't have to use distutils to get your own modules working on your own machine; saving them in your python path is sufficient.
When you decide to publish your modules for other people to use, distutils provides a standard way for them to install your modules on their machines. (The "dist" in "distutils" means distribution, as in distributing your software to others.)

Categories

Resources