Unable to use .exe frozen with cx_Freeze on another pc with the following error related to rpy2 - python-3.x

I created a small program with rpy2 to import R in Python and packaged it with cx_Freeze. Then I tested it on my development PC. It works well. However, once I copy my .exe to another PC with a similar OS (Windows 10), the same python (3.7) and R version (3.5.2), the following error message jumps out:
Traceback(most recent call last):
File "D:\software\Python3.7.3\lib\site-
packages\cx_Freeze\initscripts\__startup__.py",line 14, in run
File "D:\software\Python3.7.3\lib\site-
packages\cx_Freeze\initscripts\Console.py",line 26, in run
File "TR_SNP.py", line 32, in <module>
File "D:\plot.py",line 23, in <module>
dplR = importr('dplR')
File "D:\software\Python3.7.3\lib\site-packages\rpy2\robjects\packages.py",line 453,in importr
rpy2.rinterface.RRuntimeError
It seems like something is wrong with the R lib import.
I tried to search for the answer. Here is one that I think would be similar to my problem: rpy2 works good in PyCharm and doesn't work in .exe file
However, that answer does not provide details like where I should put the R libs.

The frozen application probably does not look for the R installation at the right place on the target PC. Try to add the following lines to your main application:
import platform
import rpy2.situation
lib_path = rpy2.situation.get_rlib_path(r_home, platform.system())
print(lib_path)
This will display the location where the application is looking for the R lib. This path (and probably the whole r_home) should exist on the target PC and contain a compatible R installation.
If the path is wrong, you need to tell the frozen application where the R installation is located on the target machine using something like
import os
os.environ['R_HOME'] = path_to_r_home_on_target_machine
But this will be difficult as you will not know in general where the R installation is located. You could try to add the following lines to your application (not sure if it works):
import sys
import os
import rpy2.situation
if getattr(sys, 'frozen', False):
# The application is frozen
# reset R_HOME and try to find a R installation using the fallback mechanisms
del os.environ['R_HOME']
os.environ['R_HOME'] = rpy2.situation.get_r_home()

Related

Issue in importing awkward array package: DLL load failed while importing _ext

I am trying to use awkward in my Windows 10 system. I am using python 3.8.2.
After installing the package, when I import it, I am getting this DLL import error.
>>> import awkward as ak
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\MSS\Work\PyWorkspace37\awkward_poc\venv\lib\site-packages\awkward\__init__.py", line 23, in <module>
import awkward.layout
File "C:\Users\MSS\Work\PyWorkspace37\awkward_poc\venv\lib\site-packages\awkward\layout.py", line 5, in <module>
from awkward._ext import Index8
ImportError: DLL load failed while importing _ext: The specified module could not be found.
How to know which DLL is missing and how to mitigate it?
The DLL is Awkward Array's C++ part, "awkward/_ext.pyc", which the installation should have put into place. I see two possibilities: (1) the installation went horribly wrong and should be restarted from a clean slate, or (2) the file is there but can't be loaded. If the directory it was installed to doesn't have executable permissions, that could be it. (Python code can be executed from a disk/directory without executable permissions, but native machine code can't be, which can cause some surprises.)
When it comes to installation issues, I can only make guesses because your system is different than mine. We try to use standard installation procedures, so if you use the standard Python inhalation tools (pip, conda) then it ought to work without problems, but evidently that didn't happen here.

cx_Freeze converted exe: window closes immediately

I am trying to just convert my pygame python py to a .exe file using cx_Freeze. The setup file executes correctly and without error, but the issue is that when I run my .exe file the console window (the black cmd-like window)will open quickly and close. My .py which I want to convert is called Salary.py, and it includes a .input('str') codes in it so that the
user can decide which csv file they want to use.
Description of my Salary.py: if the user input an interger, Salary.py help them to parse through one existed csv file and run the
script and output to another csv file.
Chinese is included inside the code.
Once again, there is no error running when I run Salary.py in python3.6, and no error when building the exe file for now.
The setup.py I am using:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'D:\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'D:\Anaconda3\tcl\tcl8.6'
setup(name='Salary',
version='0.1',
description='Salarycount',
executables= [Executable("Salary.py")])
I try to execute the Salary.exe in cmd. And it gave this to me:
Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "D:\Anaconda3\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "Salary.py", line 8, in <module>
File "D:\Anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
Apperently, numpy has already installed to my python packages, I don't even know what does it mean.
my environment:
Anaconda 3
python 3.6
cx-Freeze 6.0b1
It looks like your Salary.py script uses the pandas package, is this correct? The pandas package requires the numpy package to work, and one needs to tell cx_Freeze explicitly to include the numpy package. Try to add the following options to setup:
from cx_Freeze import setup, Executable
import os
os.environ['TCL_LIBRARY'] = r'D:\Anaconda3\tcl\tcl8.6'
os.environ['TK_LIBRARY'] = r'D:\Anaconda3\tcl\tcl8.6'
setup(name='Salary',
version='0.1',
description='Salarycount',
options={'build_exe': {'packages': ['numpy']}},
executables= [Executable("Salary.py")])

python 3 no module named _tkinter

I've been searching for it but could not find anything on the net on this topic.
When I'm working on a programm in python 3.5 which imports tkinter or pyglet I'm perfectly able to start it from the command line on my Linux Mint installation. As soon as I try to start it from pycharm or Visual Studio Code I get an error.
It is for tkinter:
Traceback (most recent call last): File "/home/b...", line 3, in <module>
import tkinter as tk
File "/usr/lib/python3.5/tkinter/__init__.py", line 35, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
Both, the tkinter and the pyglet script, are working when they are started from idle3 (with F5).
Can anyone help me out?
Best
B.
i'm having the same issue. The problem lie with Linux Mint Software Manager. If your vs code is install via Software Manager, it will be install in Flatpak virtual sandbox. Just download and run vs code from its homepage will do.

extracting python exe created via Iexpress getting ImportError: DLL load failed: The specified module could not be found. Error

I build one code and then created a exe from .py using py2exe .
Now i want to create a self expracted for the same so i used Iexpress to do that . I am able to create the exe from Iexpress but when i try to extract my application from the exe created i get following error :
Traceback (most recent call last):
File "C.py", line 44, in <module>
File "A.pyc", line 4, in <module>
File "PyQt4\QtGui.pyc", line 12, in <module>
File "PyQt4\QtGui.pyc", line 10, in __load
ImportError: DLL load failed: The specified module could not be found.
It seems , error is all coming in line :
from PyQt4 import QtCore, QtGui
I saw some ways to fix this on internet but couldnt succeed to fix this . Anyone have any ideas ?
I tried to reproduce this here, but it seems to work fine. Here's what I did:
Created a file hello-pyqt.py using this question:
How to implement a simple button in PyQt
Created a setup.py:
from distutils.core import setup
import py2exe
setup(console=['hello-pyqt.py'],
options={'py2exe': {'bundle_files': 1,
'dll_excludes': ['w9xpopen.exe', 'MSVCP90.dll'],
'includes': ['sip']}}
)
The dll_excludes and includes tricks came from an answer on py2exe fails to generate an executable and this Py2exeAndPyQt page, respectively.
Executed setup.py:
python setup.py py2exe
Tested the output before using IExpress:
dist\hello-pyqt.exe
Bundled it with IExpress in the usual way:
Added both hello-pyqt.exe and library.zip.
Set the Install Command to hello-pyqt.exe.
Enabled Long File Name support.
Compare your process with the one I'm using (above) and see if there's anything that might help. Failing that, I'd use Process Monitor to see which DLL it's failing to load and where it's looking.
The DLLs that are bundled in my library.zip are: QtCore4.dll and QtGui4.dll. You might want to check your library.zip to make sure those files are there.

Python, PySerial and cx-freeze

Trying to learn cx-freeze. I have a python program that I am trying freeze to exe.
I use PySerial and no matter how I try to include win32 nothing seems to help. I use Python 3.2 and win7.
I have searched the web thin, and others have had the same problem, but no solution seems to be appearing. But I doubt that no one have succeeded in cx_freezing something that uses PySerial.
I am completely stuck. Any help would be much appreciated
Error:
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\
7, in <module>
exec(code, m.__dict__)
File "snapper.py", line 8, in
File "C:\Python32\lib\site-packages\
from serial.serialwin32 import *
File "C:\Python32\lib\site-packages\
e>
from serial import win32
ImportError: cannot import name win32
Setup.py:
from cx_Freeze import setup,Executable
includefiles = ['caml.pkl', 'seql.pkl']
includes = ['DataBase', 'serial.win32']
excludes = ['Tkinter']
packages = []
setup(
name = 'Setup',
version = '0.1',
description = 'Snapper configuration utility',
author = 'LST',
author_email = 'info#-.com',
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includefiles}},
executables = [Executable('snapper.py')]
)
Any idea where to go from here?
Thanks in advance
I tried to do a blind import:
if False:
import serial.win32
no luck...
Maybe i am looking at this the wrong way....
Okay, problem solved.
You need to use packages to force cx_Freeze to include serial.win32 (not "include")
Following line works:
packages = ['serial.win32']
Memo to my self and others: Be sure to check the dist folder for actually included packages. I have no idea why all packages didn't get included by cx_Freeze in the first place, but this works for me.
If you can use a different tool to freeze your program, PyInstaller says it supports PySerial.

Resources