Trouble importing shared object in Python - python-2.7

I am attempting to import a shared object into my python code, like so:
import bz2
to which I get the following error:
ImportError: ./bz2.so: cannot open shared object file: No such file or
directory
Using the imp module, I can verify that Python can actually find it:
>>> import imp
>>> imp.find_module('bz2')
(<open file 'bz2.so', mode 'rb' at 0xb6f085f8>, 'bz2.so', ('.so', 'rb', 3))
The shared object file is in my PYTHONPATH and my LD_LIBRARY_PATH.
Any insights into why I can't import this shared object? Thanks!

bz2.so is the shared object the provides the bzip functionality (which was written in C) for the python modules. You don't import it directly when you do import bz2 , you are actually importing a python module called bz2 which then uses the .so file.
This usually means you haven't got the development version of the bzip library installed or you don't have a c compiler setup for the pip installer to use to build this for you.
You don't say which linux you are using but the general pattern is look in the package manager for bzip2 dev or devel packages and install those.

Related

UHD import fails

I build uhd in windows (msvc 14.2, boost 1.72.0). Build works fine. I can run all of the command line utilities (eg rx_samples_from_file) without error.
But in python I cannot import uhd:
>>> import sys
>>> sys.path.append('C:\\Program Files (x86)\\UHD\\bin')
>>> sys.path.append('C:\\Program Files (x86)\\UHD\\lib')
>>> sys.path.append('C:\\Program Files (x86)\\UHD\\lib\\site-packages')
>>> sys.path.append('C:\\local\\boost_1_72_0\\lib64-msvc-14.2')
>>> sys.path.append('C:\\lib\\libusb-1.0.22\\MS64\\dll')
>>> import uhd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files (x86)\UHD\lib\site-packages\uhd\__init__.py", line 10, in <module>
from . import types
File "C:\Program Files (x86)\UHD\lib\site-packages\uhd\types.py", line 10, in <module>
from . import libpyuhd as lib
ImportError: DLL load failed while importing libpyuhd: The specified module could not be found.
Is there anyway to tell which DLL is not loading? I tried using dependency walker on libpyuhd but it did not show anything missing.
I had a similar issue, in that I could access the hardware via command line utilities or through GnuRadio Companion, but not directly through python (even though GRC is only launching the python files using the same python interpreter). I kept having these DLL issues.
Turns out that the location in sys.path are used by python to look for modules, but if the modules themselves are relying on .dll files, then they use the locations in the environmental variable path. I guess that the fetching of DLLS is handled directly by the OS and not by Python.
You can see the location in Python with os.environ['PATH'] . It should be the same list as by typing PATH the windows command line.
I solved the problem by crating a batch file that added the directories to the path before running python. In your case, you could try adding the directories to it (you would have to see which ones are missing):
#ECHO off
SET PATH=%PATH%;C:\\Program Files (x86)\\UHD\\bin;C:\\Program Files (x86)\\UHD\\lib;C:\\lib\\libusb-1.0.22\\MS64\\dll
python your_python_script.py
the path will be temporarily changed, (don't forget the %PATH% so that it appends the new folders to it) and once you exit the batch file, it will restore back to the default value.
You could also permanently change the path, by going to System Properties → Advanced → Environment variables.

Unable to import sikuli library in RIDE

I have to write automation scripts using python and Robot framework. I have installed python, Robotframework, RIDE, wxpython. I have installed sikuli library but when I import it in my project, library is not imported. I have tried 'Import Library Spec XML'. My question is from where do I import this .xml or how do I create it?
You might need to declare the Pythonpath on RIDE preferences (Tools>Preferences>Importing), you must add there your libraries location, it helped me with suds library.
I have added there:
C:\Python27\lib\site-packages\robot\libraries
(This could be little different for you if you have installed your python at different location)
Edit:
This could be an issue on the way you are trying to import your library, or you need to download a specific sikuli library that work with robot.
Please check then if you are loading your library like this:
*** Settings ***
Library SikuliLibrary
got it from: https://github.com/rainmanwy/robotframework-SikuliLibrary
First check whether Sikuli is installed in python directory's \Lib\site-packages.
Robot test should contain as below:
* Settings *
Documentation Sikuli Library Demo
Library SikuliLibrary mode=NEW
* Test Cases *
Sample_Sikuli_Test
blabh blabh etc

Installing a module on Choregraphe

I am trying to install a python module to use in Choregraphe. For windows this does not appear to be an issue. All I have to do is make sure that the module is installed in Python on the operating system.
For OSX, it does not appear to recognise the module. I have also tried importing it as a folder to a project, but it still can't see it.
Do you try playing with the python system path: the location when he looks for library.
Like that (in your choregraphe box):
import sys
sys.path.append("path containing your_module folder")
import your_module
From the linked site:
1. Download python scripts of Requests and its dependencies from github.
2. Copy these scripts into my Project content(I made 'lib' directory in the project and copied all scripts into the folder)
3. Add the following code to import the modules
import sys, os
framemanager = ALProxy("ALFrameManager")
folderName = os.path.join(framemanager.getBehaviorPath(self.behaviorId), "../lib")
if folderName not in sys.path:
sys.path.append(folderName)
import requests
4. Add the following to unload the modules
import sys
if self.folderName and folderName in sys.path:
sys.path.remove(folderName)

Python27 zipimport does not recognize .zip file

I need to import modules such as numpy as zip files from non-standard directories.
I try to import numpy from the full path C:\Users\Anders\Downloads\numpy-1.8.1.zip\ which is downloaded from here: http://sourceforge.net/projects/numpy/files/NumPy/1.8.1/numpy-1.8.1.zip/download
Script:
import zipimport
zipimport.zipimporter("C:\Users\Anders\Downloads\numpy-1.8.1.zip")
import numpy
Output:
zipimport.zipimporter("C:\Users\Anders\Downloads\numpy-1.8.1.zip")
ZipImportError: not a Zip file
What the heck is that about?
I've had similar problems with zipimport unable to parse the full path in Window environments.
The fix I follow is to change directories into the folder containing the zip file.
import zipimport
import os
os.chdir("C:\Users\Anders\Downloads")
zipimport.zipimporter("numpy-1.8.1.zip")
Then, I specify a relative path to the zip file. I tested this locally for the case you mentioned without any complications.

Where Should Shared Object Files Be Placed?

I am venturing into the land of creating C/C++ bindings for Python using pybindgen. I've followed the steps outlined under "Building it ( GCC instructions )" to create bindings for the sample files:
http://packages.python.org/PyBindGen/tutorial.html#a-simple-example
Running make produces a .so file. If I understand how .so files work, I should be able to import the classes in the shared object into Python. However, I'm not sure where to place the file and how to let Python know where it is. Additionally, do the original c/c++ source files need to accompany the .so file?
So far I've tried placing the file in /usr/local/lib and adding that path to DYLD_LIBRARY_PATH to the .bash_profile. When I try to import the module from within the Python interpeter an error is thrown stating that the module can not be found.
So, my question is: What needs to be done with the generated .so file in order for it to be used by a Python program?
Python looks for .so modules in the same directories where it searches python ones. So you have to install it as you would normal python module either somewhere that is on python's sys.path by default (/usr/share/python/site-lib or something like that—it'd distribution-dependent) or add the directory to PYTHONPATH environment variable.
It's python that's loading the module using dlopen, not the dynamic linker, so LD_LIBRARY_PATH (note, there is no DY) won't help you.
Same as all other Python modules. It must be within one of the locations given in sys.path.