Importing PyVISA via Py2exe - python

My script file test.py is:
try:
from visa import *
except Exception as ex:
print ex
raw_input()
My setup.py file is:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
I then run:
python setup.py py2exe
If I then launch test.exe on my pc I see no issues but if I copy it to a new pc with no prior python installs I get:
[Error 126] The specified module could not be found
I don't know how to get the full traceback to print. Has anyone else seen this issue?
Running Python 2.7, PyVisa 1.4. Both machines are Win7.

Turns out the problem was that the visa libraries that pyvisa relies on weren't installed on the test pc. These libraries don't come included with pyvisa.

Related

Py2exe [Errno 2] No such file or directory: 'm'

I'm trying to use py2exe to create a simple exe using py2exe. Unfortunately, when I try to create even a simple py2exe, it fails.
My Python script is:
print("Hello World")
My setup script is:
from distutils.core import setup
import py2exe
setup(console="mouse-check.py")
When I run py py2exe-installer.py py2exe I get the following output:
C:\Users\...\Python\py2exe-installer.py:1: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
from distutils.core import setup
running py2exe
error: [Errno 2] No such file or directory: 'm'
I'm using Python 3.10.4 on Windows.
My py2exe version is: 0.11.1.1
My directory structure is:
/Python
- py2exe-installer.py
- mouse-check.py
Anyone have any idea what "m" is referring to here?
The setup function requires console argument is list, you can read more details in the implementation. Or tutorial and examples here.
Your setup script should be looked like this
from distutils.core import setup
import py2exe
setup(console=["mouse-check.py"])

Problems with Pynput and Pyinstaller on Ubuntu 20.04LTS GUI

I have a python script that uses the Pynput module. When I run the python script from terminal on Ubuntu [20.04LTS GUI] it runs perfectly.
$ pyinstaller --onefile vTwo.py
cd ./dist
./vTwo
Error occurs when running ./script:
ImportError: this platform is not supported: No module named 'pynput.keyboard._xorg'
Try one of the following resolutions:
* Please make sure that you have an X server running, and that the DISPLAY environment variable is set correctly
[5628] Failed to execute script vTwo
If someone could advise me on what may be going wrong. I have had a look at the Pynput requirements page where they mention that it requires X server to be running in the background which should not be an issue as I have a GUI installed.
Also is there anyway to use Pynput on a system without a gui?
The Solution
The solution is easy. Just include this module as a hidden import in the PyInstaller program:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg
If you also use the mouse with pynput, then you'll get the same error with the module pynput.mouse._xorg. So do this:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg --hidden-import=pynput.mouse._xorg
Warning! You'll likely get a different module that it doesn't find, if you're packaging for Windows or Mac. This is what you get for Linux. If you want your program to be cross-platform, then you'll have to package the program, for example, for Windows and test it to see which module it doesn't find and include it as a hidden import.
For example, if you want your program to work on Linux and Windows, use this command:
python -m PyInstaller your_program.py --onefile --hidden-import=pynput.keyboard._xorg --hidden-import=pynput.mouse._xorg --hidden-import=pynput.keyboard._win32 --hidden-import=pynput.mouse._win32
If you have a lot of hidden modules, then you may edit the .spec file and add the modules to the hiddenimports list like so (on PyInstaller 4.1):
hiddenimports=['pynput.keyboard._xorg', 'pynput.mouse._xorg'],
Why The Error
When you see an ImportError in a Python program packaged by PyInstaller, there is a high chance that the problem is that PyInstaller couldn't detect that specific import and didn't include it in the binary, hence the import error.
In the error message it tells you which module it didn't find:
ImportError: this platform is not supported: No module named 'pynput.keyboard._xorg'
which is pynput.keyboard._xorg, because you're on Linux.
It couldn't find the module, because it was imported in a "non-traditional" way. Look at the source code for pynput/_util/__init__.py in the backend function:
def backend(package):
backend_name = os.environ.get(
'PYNPUT_BACKEND_{}'.format(package.rsplit('.')[-1].upper()),
os.environ.get('PYNPUT_BACKEND', None))
if backend_name:
modules = [backend_name]
elif sys.platform == 'darwin':
modules = ['darwin']
elif sys.platform == 'win32':
modules = ['win32']
else:
modules = ['xorg']
errors = []
resolutions = []
for module in modules:
try:
return importlib.import_module('._' + module, package)
except ImportError as e:
errors.append(e)
if module in RESOLUTIONS:
resolutions.append(RESOLUTIONS[module])
raise ImportError('this platform is not supported: {}'.format(
'; '.join(str(e) for e in errors)) + ('\n\n'
'Try one of the following resolutions:\n\n'
+ '\n\n'.join(
' * {}'.format(s)
for s in resolutions))
if resolutions else '')
You can see that it uses the import_module function from the importlib module to import the correct module for the platform. This is why it couldn't find the
pynput.keyboard._xorg module.
Your Second Question
Also is there anyway to use Pynput on a system without a gui?
I don't know.

python: py2exe paramiko showing no results

I am using python 2.7.11 on Windows 7 64bit, I am facing a problem with py2exe when I import paramiko module, the .exe file runs but does not show any results, after executing it closes immediately knowing that i did not get any error during the conversion.
import paramiko
print raw_input("press enter to exit")
the setup.txt file:
from distutils.core import setup
import py2exe
setup(console=['test.py'])
I have found that in the "dist" folder there are some dll files such as API-MS-Win-Core-ErrorHandling-L1-1-0.dll - API-MS-Win-Core-LibraryLoader-L1-1-0.dll - API-MS-Win-Core-LocalRegistry-L1-1-0.dll, such files I don't usually face them on another machine, so could anyone help please.
I was able to solve this after importing some modules:
import appdirs
import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements
hopefully that could be useful for someone.

scrapy Import Error: scrapy.core.downloader.handlers.s3.S3DownloadHandler

i have installed scrapy on my windows 7 machine in a virtualenv called scrapy.
scrapy startproject works and i made the tutorial from scrapy docs.
if i run
scrapy crawl dmoz
i get following error message:
File "C:\Users\mac\pystuff\scrapy\lib\site-packages\scrapy\utils\misc.py", line 42,in load_object
raise ImportError("Error loading object '%s': %s" % (path, e))
ImportError: Error loading object'scrapy.core.downloader.handlers.s3.S3DownloadHandler': DLL load failed: Could not find the specified module.
TESTS:
python console:
>>> import scrapy
>>> scrapy
>>> <module 'scrapy' from 'C:\Users\mac\pystuff\scrapy\lib\site-packages\scrapy\__init__.pyc'>
scrapy is available
powershell:
PS C: python -m pydoc scrapy.core.downloader.handlers
output:
NAME
scrapy.core.downloader.handlers - Download handlers for different schemes
FILE
c:\users\mac\pystuff\scrapy\lib\site- packages\scrapy\core\downloader\handlers\__init__.py
PACKAGE CONTENTS
file
ftp
http
http10
http11
s3
CLASSES
.
.
.
all other modules except s3 show the correct pydoc. for example:
python -m pydoc scrapy.core.downloader.handlers.file
only s3 gives the import error:
PS C: python -m pydoc scrapy.core.downloader.handlers.s3
PS C: python -m pydoc scrapy.core.downloader.handlers.s3.S3DownloadHandler
output:
problem in scrapy.core.downloader.handlers.s3 - <type 'exceptions.ImportError'>: DLL load failed: Could not find the specified module.
pywin32 was problem package. i ve tried to import it in python console and it could not be found. i copy these 3 packages from C:(yourpythonpath)\Lib\site-packages\pywin32_system32\ to C:(yourpythonpath)\Lib\site-packages\win32\
pythoncom27.dll
pythoncomloader27.dll
pywintype.dll
that solved the problem!
You can download it as an installation from here:
http://sourceforge.net/projects/pywin32/
I met the same problem and after a little bit research I tried almost all the suggestions and finally solve the problem. Here is my sequence:
Reinstall OPENSSL
Reinstall pywin32 amd64
Reinstall piwin32 32-bit
and magically the problem solved. My win server is 64-bit but the problem is solved by installing a 32-bit package. I guess this is because I previously install some other package that is 32-bit and didn't realize that. I'll keep you update if I have any progress.

Using Eclipse with PyWin32

I am running Eclipse (Indigo) with PyDev, Python 2.6 and PyWin32 (build 217, 32 bit), and having a problem that gets stranger the more I investigate.
I'm running a Python program that imports the following 3 libraries:
import win32con
import win32file
import pywintypes
win32con imports properly but win32file does not (note: win32con is a pyc and win32file is a pyd, in raw Python):
Traceback (most recent call last):
File "....\dynamic\testpywin32.py", line 2, in <module>
import win32file
ImportError: DLL load failed: The specified module could not be found.
I tried the equivalent in pure Python 2.6 (IDLE) and everything imported properly.
Comparing sys.path in IDLE and Eclipse, the only thing not in Eclipse is ['C:\Python26\Lib\idlelib']; the only extra thing Eclipse has (besides the workspace) is C:\eclipse\plugins\org.python.pydev.debug_2.2.4.2011121401\pysrc.
In addition, I went on and used the following commands right after startup, through the debugger:
sys.path = [sys.path from IDLE]
os.environ['PATH'] = [os.environ['PATH'] from IDLE]
which didn't seem to help.
What's going on here?
which windows version are you using??
i am using windows XP + pydev plugin and its works.
go to python path and try to fix the python eggs path, and press apply

Categories