import C++ module, if fail: import Python version? - c++

I have a Python pyd module written in C++. I also have a Python version of the module (which is much slower).
My question is, as I want the program to run on multiple OSs, can I try to import the C++ version in the program and import the slower Python version if that one fails (other OS, architecture)?

Yes, you can import some thing like this:
try:
import CppModule as Module
except ImportError:
import PurePythonModule as Module

Yes you can:
try:
import CppModule
except ImportError:
import PythonModule
Edit: This answer, while not incorrect, is not really useful. As #Best Games' answer shows, this only really useful if you import the module using a common name.

Related

Pylint disagrees with file import

In reference to How do I import a Python script from a sibling directory?, Pylint will error with E0401:Unable to import at line 6.
if __name__ == '__main__':
if __package__ is None:
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from components.core import my_module
else:
from ..components.core import my_module
Is this just something that we have to accept, or is there a rigorous solution to correct this?
While the file in question is part of my unit tests and not critical to deployment, I would like to keep everything error free.
(pylint 1.6.4, Python 2.7.12)
If you are referring to importing components, then this would not work, since pylint does not understand modifications of sys.path. It would be theoretically feasible to understand them, but since it is a static analysis tool, it has its limitations with regard to what it can and what it should do. In order to help it, you might try running pylint with --init-hook="import sys; sys.path.append(path.dirname(path.dirname(path.abspath('your file'))))".

Python/pyserial cx_freeze Compilation error: no module named tools

I am trying to compile a python code with cx_freeze in Windows 7, python 2.7, but it keeps giving me trouble because of pyserial. I tried to import the pyserial tools module in many different ways but I keep getting compilation errors:
import serial
import serial.tools
from serial.tools import list_ports
#I also tried:
#import serial.tools.list_ports
#from serial import tools
#import serial.tools.list_ports
#from serial import tools
The error message varies between:
ImportError: No module named tools
and
ImportError: No module named tools.list_ports
Any ideas on how to solve this issue? Thanks.

ipyparallel imports: import own module created with cython

I have a problem in synchronising imports between ipyparallel engines. I want to run stochastic simulations in parallel. For this I have written a function named 'gillespie'. I have two versions of the function, one using Python and to gain some speed one in Cython. Both are working.
The problem now is how to make the compiled cython function available to all engines. The files 'gillespie.c', 'gillespie.so' and 'gillespie.pyx' are all in the current directory together with the notebook from where I want to use the functions. I wanted to import it like this
import gillespie as gc
%px import gillespie as gc
But this gives me:
Traceback (most recent call last)<ipython-input-50-801274ebf64a> in <module>()
----> 1 import gillespie as gc
ImportError: No module named gillespie
Just doing
import gillespie as gc
works just fine and I can use the compiled function.
Using the %px magic to import numpy or scipy to the engines works just fine.
How can I make my imports available to all engines?
Thanks for any help!
baumgast
%px import gillespie executes the import statement on the engines. For that to work, you need to make sure that gillespie.so is available to those engines. You can either 'install' it by placing it in site-packages or on PYTHONPATH, or you can rely on the current-directory-based loading that you are already relying on in the notebook, in which case you need to make sure that gillespie.so is in the working directory of the engines. If you are on a single machine or shared filesystem, you can accomplish this with:
%px cd /path/to/dir/containing/gillespie.so
or send gillespie.so to the machines, if the filesystem is not shared.

ImportError: cannot import name webdriver

I am implementing selenium and i have already included "from selenium import webdriver" but still getting this error
"ImportError: cannot import name webdriver"
Any idea how to resolve this error?
Cross check your language binding, check with older versions

ImportError: No module named NetCDF

I am trying to sort the following error but not getting the correct answer till now. Actually I am using Ubuntu 10.04 and I have installed netcdf-3.6.2 but even then i found this error, although I added the header like
import Scientific.IO.NetCDF
from Numeric import *
from NetCDF import *
But it does not make any impact and shows the following error:
ImportError: No module named NetCDF
If the module is Scientific.IO.NetCDF, then you need to do from Scientific.IO.NetCDF import * (and you don't need to do import Scientific.IO.NetCDF first). Also, importing everything is often a bad style, and should be avoided.