AttributeError: Extension instance has no attribute '__version__' - python

Here is the script..
from distutils.core import setup, Extension
nmap = Extension('nmap',sources = ['nmap/nmap.py',
'nmap/__init__.py', 'nmap/example.py'])
from nmap import *
setup (
name = 'python-nmap',
version = nmap.__version__,
author = 'Alexandre Norman',
author_email = 'norman#xael.org',
license ='gpl-3.0.txt',
keywords="nmap, portscanner, network, sysadmin",)
... and i got this error:
Traceback (most recent call last):
File "C:\Python27\nmap.py", line 6, in <module>
from nmap import *
File "C:\Python27\nmap.py", line 17, in <module>
version = nmap.__version__,
AttributeError: Extension instance has no attribute '__version__'

There are a number of problems here.
Your nmap package isn't an extension, it's a pure-Python package; don't create an Extension object for it. Python extension are written in C or C++.
You're trying to access nmap.__version__, presumably because you defined that variable in nmap/__init__.py, but nmap here is that Extension object you created; it's trying to access the variable from the wrong thing.
Even if you remove the Extension object, you still wouldn't be able to access nmap.__version__, because you imported your package incorrectly; you meant to use import nmap.
You never actually pass your package to setup, so distutils won't know about it. There are a few examples of how to do that in the documentation.
The distutils documentation is pretty big, but it's a good idea to read through all of it at least once.

Related

Cythonized Python.Net code cannot find system assemblies

When I compile with Cython the Python code which uses Python.NET to access .NET assemblies, it can't find those assemblies:
ModuleNotFoundError: No module named 'System'
Without compilation it works ok.
For demo code, I used https://github.com/pythonnet/pythonnet/blob/master/demo/helloform.py
My setup.py file:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
ext_modules = [
Extension(
'helloform',
sources = ['helloform.py'],
language = 'c++'
)
]
setup(
name = 'helloform',
ext_modules = cythonize(ext_modules),
)
Then I build it with python setup.py build_ext --inplace.
I wanted to load compiled module from Python prompt with import helloform but it failed with
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "helloform.py", line 8, in init helloform
ModuleNotFoundError: No module named 'System'
This answer is untested - I don't think I can easily set up an environment to test so it's a bit of a guess. If it doesn't work I'll remove it.
This probably is a bug, and if you want it fixed in the longer term you should report it. Cython does try to be compatible with Python wherever possible.... A quick investigate suggests that Python.NET overrides the built-in __import__ function. Cython looks to lookup and use this function in Python 2, but not in Python 3. This is no longer the preferred way of customizing import behaviour (but is still supported). I'd guess it would work in Cython + Python 2?
As a workaround you should probably just run the import statements in Python. There's two obvious ways to do it:
Write a small separate module just containing the import statements, then in Cython import from that module:
from import_module import WinForms, Size, Point
Run the import statements in exec; extract the values out of the global dict you pass it:
import_dict = {}
exec("""import clr
# etc...
""", import_dict) # pass a dict in as `globals`
WinForms = import_dict['WinForms']
# etc.

Yocto setup.py requires pyserial

I am trying to package https://github.com/kliment/Printrun in a Yocto recipe but cannot get it working. My recipe currently looks as follows:
LICENSE = "AGPLv3"
LIC_FILES_CHKSUM = "file://COPYING;md5=d32239bcb673463ab874e80d47fae504"
PV = "2.7"
SRCREV = "0193a9dbe31458c45059bf2dcb0a9905b7bb06fc"
SRC_URI = "git://github.com/kliment/Printrun.git;protocol=git;branch=master"
RDEPENDS_${PN} = "python-cython \
python-pyserial \
"
S = "${WORKDIR}/git"
inherit distutils
I am assuming this is what I need to do because it has a setup.py that inherits from distutils? If so, this does not work and I get an error complaining about a lack of the serial module:
DEBUG: Executing shell function do_compile
WARNING: Failed to cythonize: No module named Cython.Build
Traceback (most recent call last):
File "setup.py", line 36, in <module>
from printrun.printcore import __version__ as printcore_version
File "/home/gerhard/Jethro/yocto/build/out-glibc/work/armv7at2hf-vfp-neon-angstrom-linux-gnueabi/printrun/2.7-r0/git/printrun/printcore.py", line 20, in <module>
from serial import Serial, SerialException, PARITY_ODD, PARITY_NONE
ImportError: No module named serial
ERROR: python setup.py build execution failed.
ERROR: Function failed: do_compile (log file is located at /home/gerhard/Jethro/yocto/build/out-glibc/work/armv7at2hf-vfp-neon-angstrom-linux-gnueabi/printrun/2.7-r0/temp/log.do_compile.15542)
I would also like to be able to compile the small cythonable module with cython. For some reason both cython and psyerial are not availible even though I added them as rdepends, what am I doing wrong?
You added dependency on python cython only on runtime. I guess you need to add also for compilation.
DEPENDS_${PN} = "python-cython \
python-pyserial"

Shared library _sqlite3.so not found when importing sqlite3 on Python 2.6.6

Okay i've been battling for this for 2 days, that usually means its something too simple to realize.
I have an embedded linux system which I cross compile on my ubuntu. When compiling python, sqlite3 is not on the list of modules that have not been able to be compiled.
But, the _sqlite3.so library is not in the same location as for example json.so and ctypes.so array.so...
in Python-2.6.6/build/lib.linux868-2.6/
The actual module with the init-functions etc is in the right place at :
in Python-2.6.6/modules and it can also be found on the target system.
Since the so-file was missing, i tried compiling it myself as a shared library using my arm-compiler. This did not work either.
Without manually compiled so-file:
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rootfs/python/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
File "rootfs/python/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
ImportError: /python/lib/python2.6/lib-dynload/_sqlite3.so: cannot open shared object file: No such file or directory
With the compiled shared library found at lib-dynloads:
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "rootfs/python/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
File "rootfs/python/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
ImportError: dynamic module does not define init function (init_sqlite3)
Edit:
I was wondering if i had compiled the right library for sqlite3. As far as i now understand the _sqlite3.so is something the python builder makes and libsqlite3.so is the library it needs to build it? And libsqlite3.so is build from Sqlite3-source code. Am i mistaken here?
Anyone with more embedded Linux or Python experience have an idea what I am doing wrong here?
Try to compile and install sqlite3 first on your system, and compiler python later. Or just
easy_install pysqlite
Ok, figured this out. Somehow I did not manually compile the SO-file correctly. Got this to work like so:
First from setup.py , I added verbose debugging enabled for sqlite3 module. This added a printout that solved the problem:
skipping incompatible /usr/lib/libsqlite3.so
cannot find -sqlite3
That made me realize that the setup.py had chosen the first path where it found any module named sqlite3, ignoring it's architecture alltogether. Removing other search paths from the setup.py, but the one i had the ARM compiled library in, made it work. The _sqlite3.so was compiled nicely with all the other modules.

python select.epoll() not working

I build python2.6.1 on laptop. Build was successful. After that I tried to run python using ./python and tried to import select module
>>>import select
>>>select.epoll()
<select.epoll object at 0xb76140d0>
After that I copied the python build folder to anther laptop and tried to run python
>>>import select
>>> select.epoll()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'epoll'
How to solve this problem
From the manual:
select.epoll([sizehint=-1])
(Only supported on Linux 2.5.44 and newer.)
Returns an edge polling object, which can be used as Edge or Level Triggered interface
for I/O events; see section Edge and Level Trigger Polling (epoll)
Objects below for the methods supported by epolling objects.
New in version 2.6.
And obviously distromakers can disable it in python build settings or so. It does depend on glibc only as far as I know. Also are you sure you are importing from the system select module, and not from your own module named by the same name? (check select.__file__; as a builtin module it should not have a file ;)

Py2exe - win32api.pyc ImportError DLL load failed

I am trying to use py2exe to distribute a python application I have written. Everything seems to go OK, but when I run it on another machine it fails with the following error:
Traceback (most recent call last):
File "application.py", line 12, in <module>
File "win32api.pyc", line 12, in <module>
File "win32api.pyc", line 10, in __load
ImportError: DLL load failed: The specified procedure could not be found.
I have googled for this and not found very much, but have tried the following suggestions to no avail:
Imported pywintypes and pythoncom before win32api (in the setup.py for py2exe and in the main application)
Added some code to the setup.py -
# ModuleFinder can't handle runtime changes to __path__, but win32com uses them
import pywintypes
import pythoncom
import win32api
try:
# if this doesn't work, try import modulefinder
import py2exe.mf as modulefinder
import win32com
for p in win32com.__path__[1:]:
modulefinder.AddPackagePath("win32com", p)
for extra in ["win32com.shell"]: #,"win32com.mapi"
__import__(extra)
m = sys.modules[extra]
for p in m.__path__[1:]:
modulefinder.AddPackagePath(extra, p)
except ImportError:
# no build path setup, no worries.
pass
I'm quite new to all this, so any help would be greatly appreciated
Thanks
Jon
I've seen this problem when the package was built on Vista but executed on XP. The problem turned out to be that py2exe mistakenly added powrprof.dll and mswsock.dll to the package. Windows XP contains its own copies of these files though, and can't load the Vista ones which got installed with your app.
Removing them from the package did the trick, you can do this easy by adding this to the options dict in setup.py
'dll_excludes': [ "mswsock.dll", "powrprof.dll" ]
#Wim, I found the bit about "adding this to the options dict in setup.py" a bit confusing. If like me you did not have an options arg in your existing call to setup this might make things clearer:
setup(name='myprog',
...
options={"py2exe":{"dll_excludes":[ "mswsock.dll", "powrprof.dll" ]}},
...
)
Try adding win32api to your packages, in the options dictionary.
Here's an example:
excludes = ["pywin", "pywin.debugger"] # there will be more in real life...
options = dict(optimize=2,
dist_dir="build",
excludes=excludes,
packages=["win32api"])
setup(
name="MyCoolApp",
options=dict(py2exe=options),
# etc ...
Just as an added comment. When rebuilding your program with Py2exe be sure to delete the old "dist" directory. I was sitting for over 3 hours not understanding why my app was working on my dev envirnoment and not in production. deleted dist and rebuild with py2exe and it worked.

Categories

Resources