Image Not Found error when bundling pycups with pyinstaller - python

I'm trying to create a one-file executable from a python file using pyinstaller. The executable fails when trying to import cups and gives the following error:
Failed to execute script ImportPyCups
Traceback (most recent call last):
File "ImportPyCups.py", line 1, in <module>
import cups
File "/Library/Python/2.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "build/bdist.macosx-10.8-intel/egg/cups.py", line 7, in <module>
File "build/bdist.macosx-10.8-intel/egg/cups.py", line 6, in __bootstrap__
ImportError: dlopen(/var/folders/jp/8v0hvshd585dw9v_7bnlxsqh0000gn/T/_MEILQoRVs/cups.so, 2): image not found
Here's the python file (yes, just a single import statement):
import cups
Here are the commands I run to turn the python file into a single file executable:
$ /usr/local/bin/pyi-makespec --onefile --console ImportPyCups.py
$ pyinstaller ImportPyCups.spec
I then run:
$ dist/ImportPyCups
This is where I get the error message that I pasted above. If I just run the python file from the terminal like below, no errors occur:
$ python ImportPyCups.py
The environment this runs in is (Mac OS X 10.8.5):
PyInstaller: 3.2
Python: 2.7.2
Platform: Darwin-12.5.0-x86_64-i386-64bit
The installed pycups is:
Metadata-Version: 1.0
Name: pycups
Version: 1.9.68
Summary: Python bindings for libcups
Home-page: http://cyberelk.net/tim/software/pycups/
Author: Tim Waugh
Author-email: twaugh#redhat.com
License: GPLv2+
Location: /Library/Python/2.7/site-packages/pycups-1.9.68-py2.7-macosx-10.8-intel.egg
Requires:
Classifiers:
Intended Audience :: Developers
Topic :: Software Development :: Libraries :: Python Modules
License :: OSI Approved :: GNU General Public License (GPL)
Development Status :: 5 - Production/Stable
Operating System :: Unix
Programming Language :: C
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 3
From the error message it seems that something is missing, but I haven't been able to figure out what.

I've finally found the answer here: https://github.com/pyinstaller/pyinstaller/issues/1728
If you use easy_install to install PyCups, you'll get an .egg file. Pyinstaller doesn't look for C extensions inside zipped .egg files and never will.
The solution is simple: Remove the .egg file and install PyCups using pip.
This is not specific to PyCups, but to easy_install.

Related

ModuleNotFoundError When running own Project installed from PyPi using Setuptools and Wheels

When I install one of my own Python applications from PyPi, it fails to run citing
File "/home/me/.local/lib/python3.9/site-packages/refrapt/refrapy.py", line 20, in
from classes import (
ModuleNotFoundError: No module named 'classes'.
I have the following directory layout in my local area:
/refrapt
setup.py
/refrapt
classes.py
helpers.py
refrapt.conf
refrapt.py
settings.py
__init__.py
To build the project, I'm using setuptools, running the following command:
python setup.py sdist bdist_wheel
This builds and works happily enough, and I'm able to upload the resulting /dist.
I then install the project using pip3 install refrapt. When I run it using refrapt, I get the error ModuleNotFoundError above.
When I run the development code locally, it runs fine, but installed via pip, it is failing. I assume it's a problem with my setup.py, but this is my first time and I haven't really a clue what is correct. I tried adding the init.py (which is empty) as suggested by some python docs, but to no avail. The contents of setup.py are as follows:
import pathlib
from setuptools import setup, find_packages
HERE = pathlib.Path(__file__).parent
README = (HERE / "README.md").read_text()
setup(
name='Refrapt',
version='0.1.5',
description='A tool to mirror Debian repositories for use as a local mirror.',
python_requires='>=3.9',
long_description=README,
long_description_content_type="text/markdown",
packages=find_packages(),
install_requires=[
'Click >= 7.1.2',
'Colorama >= 0.4.4',
'tqdm >= 4.60.0',
'wget >= 3.2',
'filelock >= 3.0.12'
],
classifiers=[
"Development Status :: 4 - Beta",
"Operating System :: Microsoft :: Windows :: Windows 10",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: Implementation",
"Topic :: System :: Archiving :: Mirroring"
],
keywords=['Mirror', 'Debian', 'Repository'],
entry_points='''
[console_scripts]
refrapt=refrapt:refrapt
''',
)
If anyone could help, I'd greatly appreciate. I'm out of my depth on this one, and haven't been able to find an answer so far.
from classes import …
In Python 2 this was relative import — the statements imports classes from the current directory of the importing module.
But in Python 3 it was changed to absolute import. The import fails because there is no a global module or a package named classes. You need to convert the import to absolute or relative. Either
from refrapt.classes import …
or
from .classes import …
Potentially I've found out the answer to my question, but it's not the answer I wanted.
I spun up a virtual environment, and installed an application that I've used before via pip. When I went to run the app, I got the ModuleNotFoundError: No module named 'app'.
I tried to run it manually via the .py file by using python .\.venv\Lib\site-packages\app\cli.py, which resulting in the same error.
Seems to be something about the environment setup in Windows VS Code that just doesn't operate the same as on a Linux machine.
I guess I'll just have to remove the "refrapt." prefix from import statements when developing locally, and then add it back when pushing to GitHub.

Determining the issue regarding python versions for running repo init

When I am using repo init to initialize some repo from a source, I keep getting:
Traceback (most recent call last):
File "/home/workspace/.repo/repo/main.py", line 56, in <module>
from subcmds.version import Version
File "/home/workspace/.repo/repo/subcmds/__init__.py", line 38, in <module>
['%s' % name])
File "/home/workspace/.repo/repo/subcmds/upload.py", line 27, in <module>
from hooks import RepoHook
File "/home/workspace/.repo/repo/hooks.py", line 472
file=sys.stderr)
^
SyntaxError: invalid syntax
The following are the python versions I have:
python -V -> 2.7
python3 -V -> 3.6
According to this link, python 3.6 is required starting with repo-1.14 but I'm not certain if that's what my repo version is - didn't have any luck finding a way around determining the version.
Looks like you are running an old repo launcher script version that is incompatible with the latest repo version. To fix this, download the latest repo launcher python script from Google and replace your outdated one.
The launcher script is the executable python script called "repo" and is located in your PATH, usually ~/bin (according to Google's instructions) or ~/.local/bin/. To find out where yours is located, type which repo in the terminal and it will print the script's location.
Let's say your repo script is in ~/bin. To download the script (and make it executable), you can do:
curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
Also, you do not need to remove Python 2.7 for repo to work. I use Ubuntu 18.04 and have both Python 2.7 and 3.6 installed as well, and repo runs fine.

Why did installing VS break Python scripts from running at Command Prompt?

I'm using Python 3.7. I've installed a few packages using pip install. I usually run the scripts I've written directly from the command line, in the Windows Command Prompt.
The other day I installed Visual Studio 2019 Community Edition, mainly for coding C++ console apps. While installing, I noticed it has support for Python, so I checked the box to include the Python features of Visual Studio. I've done nothing further with respect to configuring VS.
Now, when I try to run my Python scripts from the command line, I get a ModuleNotFoundError. For example:
C:\Users\me\Python\MyScript.py
Traceback (most recent call last):
File "C:\Users\me\Python\MyScript.py", line 25, in <module>
import pyvisa.errors
ModuleNotFoundError: No module named 'pyvisa'
Here are the import statements at the top of my script:
# System imports
import sys
import os
import re
import argparse
from enum import Enum
# Third-party library imports
import pyvisa.errors
import pyvisa as visa
The library in question is still installed:
C:\Users\me\Python>pip show pyvisa
Name: PyVISA
Version: 1.10.1
Summary: Python VISA bindings for GPIB, RS232, TCPIP and USB instruments
Home-page: https://github.com/pyvisa/pyvisa
Author: Torsten Bronger, Gregor Thalhammer
Author-email: bronger#physik.rwth-aachen.de
License: MIT License
Location: c:\users\me\appdata\local\programs\python\python37-32\lib\site-packages
Requires:
Required-by:
My path and PYTHONPATH are:
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\me\AppData\Local\Programs\Python\Python37-32\Scripts\;C:\Users\me\AppData\Local\Programs\Python\Python37-32\;C:\Users\me\AppData\Local\Microsoft\WindowsApps;
PYTHONPATH=C:\Users\me\Python\pkg
Here is the sys.path:
C:\Users\me\Python>python -m site
sys.path = [
'C:\\Users\\me\\Python',
'C:\\Users\\me\\Python\\pkg',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages',
'C:\\Users\\me\\Python\\common',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\win32',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages\\win32\\lib',
'C:\\Users\\me\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-p`enter code here`ackages\\Pythonwin',
]
USER_BASE: 'C:\\Users\\me\\AppData\\Roaming\\Python' (doesn't exist)
USER_SITE: 'C:\\Users\\me\\AppData\\Roaming\\Python\\Python37\\site-packages' (doesn't exist)
ENABLE_USER_SITE: True
I've read several of the top Google hits for ModuleNotFoundError, but a simple explanation is not to be found.
What is a straightforward solution to this?

PyBuilder can't find src module when running from virtualenv?

Here's my build.py:
import os
import shutil
import subprocess
from pybuilder.core import use_plugin, init, task, Author
use_plugin('python.core')
use_plugin('python.unittest')
use_plugin('python.coverage')
use_plugin('python.distutils')
use_plugin('python.install_dependencies')
use_plugin('pypi:elasticsearch')
authors = [Author('eagle', 'email')]
version = '1.0'
description = 'Pipeline thing test'
requires_python = '>=3.0'
default_task = ['install_dependencies', 'publish', 'setup', 'analyze']
#init
def initialize(project):
project.build_depends_on('elasticsearch')
project.set_property('dir_source_unittest_python','tests')
project.set_property('dir_source_main_python','src')
project.set_property('unittest_module_glob','test_*')
project.set_property('coverage_branch_partial_threshold_warn ',80)
project.set_property('coverage_threshold_warn',80)
project.set_property('coverage_break_build',False)
project.set_property('distutils_use_setuptools',True)
project.set_property('distutils_classifiers',['Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'Topic :: Software Development :: Pipeline', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 3.6'])
#task
def setup():
subprocess.check_output(['python3.6','target/dist/pythonutilities-1.0/setup.py', 'install'])
It's specifically looking for the test directory under projectDir/tests
as specified in project.set_property('dir_source_unittest_python','tests')
And it does look there and find the correct test file:
File "/Users/adc1blz/Desktop/Work/pythonutilities/tests/test_elastic_controller.py", line 4, in <module>
But I still receive the following error:
ImportError: Failed to import test module: test_elastic_controller
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/adc1blz/Desktop/Work/pythonutilities/tests/test_elastic_controller.py", line 4, in <module>
from src.elastic_controller import ElasticController
ModuleNotFoundError: No module named 'src'
I've tried using the default project structure by deleting
project.set_property('dir_source_unittest_python','tests')
project.set_property('dir_source_main_python','src')
and then moving around my test and src directory, but still I get the same error. The specific commands that I'm running that cause this are:
> python3.6 -m virtualenv env
> source env/bin/activate
> pip3.6 install pybuilder
> pyb -X
Why can't PyBuilder find the src module while in a virtual environment? Please, this is has been so frustrating.
Chris

error in running Google apps in python in windows OS

in google app engine
get error in runing the appicaition
in logs of googleappengine
2012-04-21 13:24:04 Running command: "['C:\Python32\pythonw.exe', 'C:\Program Files\Google\google_appengine\dev_appserver.py', '--admin_console_server=', '--port=8080', 'C:\udacity\googleapps\hello-udacity']"
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\dev_appserver.py", line 125, in
run_file(file, globals())
File "C:\Program Files\Google\google_appengine\dev_appserver.py", line 121, in run_file
execfile(script_path, globals_)
NameError: global name 'execfile' is not defined
2012-04-21 13:24:07 (Process exited with code 1)
You appear to be running Python 3.2 (your Python executable is on this path 'C:\Python32\pythonw.exe').
Google AppEngine requires Python 2.5 or 2.7 at this stage, see this article. You need to download Python 2.7, install it in a parallel directory, and make it the default executable.
You may find it helpful to read this answer on running more than one version of Python
Also, I had the same problem. This is because I have installed both Python 2.7 and Python 3.x (the latest version of the framework Django uses the latter). I installed Python 3.x later so this is the version that is used by default in my system. To use Python 2.7 you could do the following:
C:\Python27\python.exe dev_appserver.py path-to-your-app
Notice that here
C:\Python27\python.exe
is the path to the python 2.7 executable (in my case, but this could vary).
I did that and it worked.
More info and reading here.

Categories

Resources