Nuitka error Cannot find ' ' in package ' ' as absolute import - python

I'm trying to use the nuitka tool to turn my python program into executable on ubuntu. It works fine if the program doesn't have any import statements but breaks when I use it on a program that imports something e.g.
test.py
import numpy
print "hello, world."
type this on commandline
nuitka --recurse-all --python-version=2.7 test.py
and gives me these errors
Nuitka:WARNING:/usr/lib/python2.7/dist-packages/numpy/numarray/functions.py:45: Cannot find 'copyreg' in package 'numpy.numarray' as absolute import.
Nuitka:WARNING:/usr/lib/python2.7/dist-packages/numpy/distutils/npy_pkg_config.py:11: Cannot find 'configparser' in package 'numpy.distutils' as absolute import.
Nuitka:WARNING:/usr/lib/python2.7/dist-packages/numpy/distutils/system_info.py:1765: Cannot find 'Numeric' in package 'numpy.distutils' as absolute import.
Nuitka:WARNING:/usr/lib/python2.7/dist-packages/numpy/distutils/system_info.py:1770: Cannot find 'numarray' in package 'numpy.distutils' as absolute import.
Nuitka:WARNING:/usr/lib/python2.7/dist-packages/numpy/f2py/diagnose.py:48: Cannot find 'numpy_distutils' in package 'numpy.f2py' as absolute import.
Nuitka:WARNING:/usr/lib/python2.7/dist-packages/numpy/f2py/diagnose.py:87: Cannot find 'numpy_distutils.command.build_flib' in package 'numpy.f2py' as absolute import.

I don't know about your particular use case but I also faced similar Cannot find '' in Package errors when using nuitka.
I was using sqlalchemy and had a similar issue with configparser.
After about a day of debugging I found out that Nuitka trips up with SWIG (Dynamically Loaded shared objects).
What it means basically is, some programs/modules try to increase compatibility by using conditional imports.
For eg:
If python_version==3.5:
import thislibrary
else:
import thatlibrary
specifically the configparser library is named configparser in python3 and ConfigParser in python2.
So what basically is happening is that nuitka is trying to import python 3 stuff when you clearly are using python 2.
For me the fix was to modify the source code of sqlalchemy and change the if else construct to:
import thatlibrary
You can find more information in this Guide
written by Tom Sheffler

Official answer from Nuitaka.
What problem you facing same like another problem one user facing package 'matplotlib' as absolute import. this problem, then there nuitaka given comments below check if it useful for below comment.
I think you might be using 32 bits Python on Windows and hit the 2GB RAM boundary. Use 64 bits for better luck. Incidentally I am working on scalability improvements for the next releases which ought to make this not happen. For now Nuitka loads all say 1000 modules into RAM and compiles them all globally. Needs a lot of RAM.

Related

Python - How do you import downloaded directories/modules?

This is the first time I have attempted to use anything other than what's provided by python.
I have recently gotten into pythons provided Tkinter, though due to some issues I decided to use another GUI, and heard that PyQt was highly recommended, so I downloaded that and looked into various tutorials. In these tutorials, I cannot seem to execute any of the import statements in said tutorials that relate to PyQt, primarily PyQt5 (I have checked I have the correct version number by the way).
So for instance:
import PyQt5
raises the error:
Traceback (most recent call last):
File "/Users/MEBO/PycharmProjects/Music/testing.py", line 1, in <module>
import Qt
ImportError: No module named 'Qt'
[Finished in 0.1s with exit code 1]
I have a lot of research into this. I've heard people talk of using pip to install modules, and I have done this be safe (as well as downloading it from the internet), I've tried changing the project interpreter to versions Python3/ 2.7/ 2.6, appending the path name to the sys.path directory, (which I really know nothing about to be honest, I was hoping I'd get lucky), though nothing seems to work.
Are you supposed to be able to just import a module off the bat, or do you have to set some things up first?
For windows download the package and extract it to (path where python installed)\Python27\Lib and then try to import.
Specific to PyQt
This package cannot just be downloaded and imported, it must be built because it is not pure python, it uses Qt (C++) and requires dependancies. Read this tutorial on installation.
There is also a very complete python package distribution, Anaconda, that includes pyqt and much more. Almost all the packages I ever looked at are in there.
In general to pure python code
In other cases, if you place modules/code that has been download into the directory that your python script is run from, you can import off the bat, or you can append/insert any folder to the sys.path.
# importer will search here last
sys.path.append('/path/to/code/')
# importer will search here second, right after script's directory
# this can be useful to override a module temporarily...
sys.path.insert(1,'/path/to/code/')

Bad magic number while trying to import .pyc module

I have some problems while trying to import some module (compiled .pyc) in my program. I know that it compiled in Python 2.6.6 (r266:84297), I have installed the same version, but had an error "bad magic number" while trying to import it :(
Does anybody know what I did wrong?
Or maybe it's possible to change magic number in .pyc module?
As the answer linked by Matthew explains, your problem is almost certainly due to different versions of Python being used for compiling and loading the module. You can determine the magic number like this:
with open('pyuca.pyc', 'rb') as f:
print struct.unpack('<H', f.read(2))
You can determine your Python version by printing sys.version (it is also echoed on interactive startup). If you are using Python 2.6.6, the magic number should be 62161. If it is different, you will need to switch to a different Python to be able to import the module.
The exact same applies to .pyo files.
I solved this by running
find . -name '*.pyc' -exec rm {} +
which deleted all the pyc files in my directory. After that it was OK.
If your running python2 and python3 and removing old *.pyc files is too messy you can add the following to try to patch this python3 bug by adjusting the code loader suffix for bytecode files.
import sys
if(sys.version_info.major>=3): # switch byte files end extension on 3
import importlib.machinery
altsuffix = ['.pyc3'] # or some other ending that doesn't create conflicts
importlib.machinery.BYTECODE_SUFFIXES = altsuffix

py2exe cannot import Module from other directory

I am bundling python source code with py2exe. The directory structure is as follows:
some_Mod.py
some_dir/another_dir/some_Mod.py
Inside the latter some_dir/another_dir/some_Mod.py I am trying to import the other Python Module with
from ..some_Mod import *
Using the import causes no problems with the python interpreter, but if I run the same constellation in the bundled package, I get an Exception:
ImportError: No module named some_Mod
Can somebody explain why?
Remark: Renaming the Modules is actually no problem, but I was just wondering, why py2exe cannot deal with this constellation.
If you have __init__.py files in each of those sub-directories then all import statements should work correctly.
Assuming that's not the problem, here's an excellent guide to importing best practices:
http://blog.habnab.it/blog/2013/07/21/python-packages-and-you/
In summary, never use relative imports - always absolute (see the link above for why).
Second (and I'm not entirely sure why), always keep your py2exe setup.py script in the exact folder where your main script is. I've tried modifying py2exe's 'script' option to allow my script to be somewhere else... but your exact problem happened to me. So, try making sure it is right where the main script is.
Finally, you can always give py2exe a little help. I usually have to add the root directory to the system path so the import statements are valid. Note, I'm not modifying sys.path in any of my application's code - only the py2exe script I use to build the exe.
At the top of my py2exe setup script:
import sys
sys.path.append(PATH_WHERE_PACKAGES_ARE)
# add any packages that need explicit importing here located in root directory:
import package1 # apparently it wasn't found...
import package2 # apparently same thing
Generally I don't import packages though, adding the project root where they exist usually is enough.
I'm not sure that py2exe now how to handle the from ..some_Mod import * syntax, check this to ensure that the some_Mod.py module is correctly packaged : python -m py2exe.mf -d some_dir/another_dir/some_Mod.py as explained in the py2exe FAQ

Error importing module _md5, and relevant RPM

I have a python script that used to run, although since moving servers at work it now throws up a strange error:
>>> import _md5
ImportError: No module named _md5
The general setup is all correct, as is my python path and seemingly everything else. I was told that I need to install the relevant RPM for this to work, but have no idea what this might be - could anyone please point me in the right direction?
These machines have a setup that prohibits me using yum, so I need to make a request to those maintaining the system about which RPM I want installed.
Based on extra bit of information from the OP, they use Python 2.5+ on the new server.
Suggested remedy is to use standard hashlib module. Which provides MD5 hash implementation among other things.
Install openssl-devel and rebuild.
Or better yet, build your own Python package and deploy everywhere.

Pyinstaller no module named 'pyfiglet.fonts ' pyinstaller '

Hey i want to export my Python Selenium Projekt with Pyinstaller but every time when im trying to do this it wotrks but when i start the exe file i became this error:
ModuleNotFoundError: no module named 'pyfiglet.fonts'
to start pyinstaller i used this command:
pyinstaller --onefile SickoAIO.py
Without a little more information, it's hard to tell what's wrong. However, I'm going to assume some things you haven't stated and provide some ideas:
Assuming SickoAIO.py works fine before you try to use pyinstaller, sometimes if in your Python code you only imported the more general module:
import pyfiglet
but need to use pyfiglet.fonts, a "hack" for it to work in pyinstaller could be to make the import more specific:
import pyfiglet.fonts
I've not used pyfiglet, but this hack worked for me with the numpy module, for example.
Another thing that has helped me troubleshoot a similar issue with numpy, scipy, etc. is updating the versions of the module and/or pyinstaller
It's possible you may need to use hooks, especially since it seems like pyfiglet may not play well with pyinstaller. This is explained specifically for pyfiglet here:
https://hugomartins.io/essays/2019/06/pyinstaller-pyfiglet-trouble/
I didn't try it, and it looks like you may need to click on more links in that webpage as well as learn more about how hooks work in pyinstaller (https://pyinstaller.readthedocs.io/en/stable/hooks.html?highlight=hook) to understand the full issue.

Categories

Resources