Python SSL Import Error in PyInstaller generated executable - python

I'm trying to distribute an executable file of a Python program I've created.
For this purpose I'm using PyInstaller .
I use Windows 7 Professional with Python 3.7
The program runs successfully when inside PyCharm and produces the expected results.
As described in the PyInstaller manuals I've created the dist folder with:
pyinstaller main.py
The log output specifies that the process finished successfully.
When trying to run the main.exe from the distribution folder, the below error is produced:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 11, i
n <module>
File "c:\users\tim\anaconda3\envs\timtf2_37\lib\site-packages\PyInstaller\load
er\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified procedure could not be found.
[6236] Failed to execute script pyi_rth_certifi

So after a couple of hours, I managed to solve the problem.
I did several things and still unsure what was the actuall fix, but this is the step-by-step flow I've done:
Created a new environment using Anaconda. I've downgraded the Python version to 3.6.8, since I saw mentions of several compatibility problems with Python 3.7
Installed all the requirements from my project, using Anaconda, since some mention problems due to mixed usage of pip and conda.
Installed the latest version of pyinstaller.
Activated the new environment and run pyinstaller from within it on the main.py script of the program with --hidden-import pyodbc argument.
That's it, now the distibuted program works on all Windows computers in the office.

Related

VMTK custom script producing "No module named: vmtk.custom" error

I'm working with a module called VMTK to build scripts for analyzing vascular models. They have a tutorial for writing these scripts so they're easily identifiable by the system allowing you to pipe scripts together known as PypeS. I've followed the tutorial exactly as shown and end up with the same "No module named: vmtk.name_of_script" error.
What I've tried:
Putting the script in the same directory as all the preloaded scripts provided by the module so __init__.py could catch it
Copy-pasting the code from the website into my editor so I'm sure there are no errors in what I've written
Submitting the question directly onto the forum meant for questions about VMTK, with no responses for the past week and a half
Changing my Python interpreter to the one within the VMTK directory
The tutorial says you can put the custom script anywhere in your filesystem, but the issue arises no matter what. I'm currently using Python 3.6.10 through Anaconda and a binary install of VMTK (as opposed to building from source) on MacOS High Sierra.
I really don't want to have to build from source, as I only need the preloaded scripts and ability to write new scripts, as opposed to using the C++ files that the scripts are built from. I've been stuck for so long and am not at all sure what the issue is. The closest thing I found was an SO question addressing this problem, but the OP simply said the problem had been resolved and provided no other information.
Here's the aforementioned code:
#!/usr/bin/env python
import sys
from vmtk import pypes
from vmtk import vmtkscripts
customscript = 'customScript'
class customScript(pypes.pypeScript):
def __init__(self):
pypes.pypeScript.__init__(self)
def Execute(self):
pass
if __name__=='__main__':
main = pypes.pypeMain()
main.Arguments = sys.argv
main.Execute()
As per the tutorial, I've also run chmod u+x customscript.py to change the permissions of my file so I can execute it. I get stuck at exactly the point where the script should do the most basic of operations within VMTK. Any ideas?
Edit: Here's the exact error:
$ ./customscript.py
No module named 'vmtk.customscript'
Traceback (most recent call last):
File "/vmtk/lib/python3.6/site-packages/vmtk/pype.py", line 290, in Execute
module = importlib.import_module('vmtk.'+scriptName)
File "/vmtk/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 978, in _gcd_import
File "<frozen importlib._bootstrap>", line 961, in _find_and_load
File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'vmtk.customscript'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./customscript.py", line 21, in <module>
main.Execute()
File "/vmtk/lib/python3.6/site-packages/vmtk/pypescript.py", line 688, in Execute
pipe.Execute()
File "/vmtk/lib/python3.6/site-packages/vmtk/pype.py", line 298, in Execute
self.PrintError(str(e))
File "/vmtk/lib/python3.6/site-packages/vmtk/pype.py", line 102, in PrintError
raise RuntimeError(errorMessage)
RuntimeError: No module named 'vmtk.customscript'
I want to backtrace the issue but I don't think editing any of the preloaded files should be necessary.
The solution I found after a week of trying and searching:
git clone the vmtk repo onto your computer and follow the VMTK download instructions on how to build it from source. It will absolutely feel like a headache and be a long process; just wait it out.
If you don't have cmake installed on your computer, they offer a GUI version on their website. I'm not sure how it would work for Windows, but it definitely works for Mac.
Once you've built it, make sure that each terminal instance of VMTK you set source vmtk_env.sh from the path where that file is. This will allow you to use the VMTK environment.
After writing the custom script, making sure to follow all the directions in the tutorial (assuming this is the first time), make sure to save it in the "site-packages" folder in the build folder. The path for me is "vmtk-build/lib/python-2.7/site-packages/vmtk/". I'm currently looking to update the version of Python it's using, but that's not the point.
Once that's done, navigate to that folder from within terminal and run chmod u+x name_of_script.py (if you're in a Unix-based operating system). This will allow you to execute the file with ./name_of_script.py.
I believe it was due to the version of Python VMTK is using, but I had two "No module named" errors come up, each one a separate time: one for tkinter and one for joblib.
The tkinter issue is one that can be resolved by going into the file where the import is, I believe vmtkscripts.py, and changing all instances of tkinter to Tkinter (capital T). This is because they changed the name of the module from Python 2 to 3, so in newer versions of Python it would be tkinter. That fixed the first error.
When I attempted to fix the joblib issue, I saw that the other version of Python available on my system already had it, thus I couldn't install it through pip. Instead, you can do python-2.7 -m pip install joblib to install it for a specific version of Python. Although I did uninstall joblib as a whole from my computer before doing that.
That should fix everything. The next time I attempted to run my script, it ran without any errors.
Extra: I wasn't using any Anaconda environment for this. Might go back and try to set that up, but I'm just glad I got it to work. Additionally, I deleted the binary install of VMTK from my computer. No need to keep it around if you're just going to build it from source. You can run VMTK through the terminal after setting source vmtk_env.sh by running the vmtk command.

How to make executable Python file from .ipynb (Jupyter Notebook)?

I wrote a Python program in Jupyter Notebook. The program uses libraries installed through Anaconda. I need to get a separate executable Python file that would work on forks of Ubuntu and Debian.
I created the .py file from the .ipynb file through the menu in the Jupyter Notebook:
File -> Download as -> Python (.py).
Next, I try to run .py file through the Terminal in Linux:
>>> python3 name_of_created_file.py
And I get an error:
Traceback (most recent call last):
File "name_of_created_file.py", line 11, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
As I understand it, there are not enough software libraries to run the program. On the same computer (Linux) in Jupyter Notebook, my program works well.
How to get a working program separately from Jupiter Notebook? To do this, I need to separately install software libraries?
Can you please check the version of python in your local, whether python or python3. Your issue might be because of two different python versions.
Depending upon your version of python in local machine, you might have to use the same version of pip to install pandas.
Check the version of python you are running in Jupyter.

How to use Vpython (visual) from an IDE

I'm very new to python and I don't entirely understand versions and libraries, so there may be a simple solution to my problem.
I am working on a project in which it is vital that we visually display our simulation. I installed Vpython on Mac OSX 10.8.4 and so far visualizations work when running the program from the command line. However I'd like to use an IDE (Eclipse, Sublime Text, Spyder. Ideally Spyder) but whenever from visual import * shows up, I get the following error:
>>> runfile(r'/Users/robinnewhouse/code/Spyder/astro/maintest.py', wdir=r'/Users/robinnewhouse/code/Spyder/astro')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Applications/Spyder.app/Contents/Resources/lib/python2.7/spyderlib/widgets/externalshell/sitecustomize.py", line 523, in runfile
execfile(filename, namespace)
File "/Users/robinnewhouse/code/Spyder/astro/maintest.py", line 7, in <module>
from visual import *
ImportError: No module named visual
So I tried using Spyder's PYTHONPATH manager to add the path to the package. I got the same error.
Then in a last ditch effort, I tried to manually add the "visual" package to Spyder's directory of included packages (/Applications/Spyder.app/Contents/Resources/lib/python2.7/) and I got a different (albeit worse) error: Fatal Python error: PyThreadState_Get: no current thread
I have no idea how to deal with this issue, but the most frustrating part is that if I run the same file in command line, it works fine. (speaking of which, if it helps, the version of python that comes up when I use which python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Thanks for any advice you can offer.
(Spyder dev here) The right solution is this: In Spyder you need to go
Tools > Preferences > Console > Advanced Settings > Python Executable
and then change the file that appears there to be
/usr/bin/python
However, before doing this you need to reinstall Spyder again, because you added the visual module to it and now it's broken.

DLL load failed: The specified procedure could not be found [duplicate]

I created python executables with py2exe with both 64bit python interpreter and 32 bit python interpreter.
In my program, I use the module pywin32 com, and so I dl'ed and installed both the 64bit and 32bit versions of the program prior to creating the executable.
The 64bit exe works fine, but the 32 bit one has the following problem:
Traceback (most recent call last):
File "program.py", line 11, in <module>
File "win32com\__init__.pyc", line 5, 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 tried both versions by running the source directly, ie python program.py and C:\python_32\python.exe program.py and both work fine.
Note I tested the 64bit exe on a 64bit windows 7 computer and the 32bit exe on a 32bit windows XP computer.
Older than everything ever, but I stumbled on this problem today, and if anyone else does, what I ran was:
python /c/Python26/Scripts/pywin32_postinstall.py -install from the commandline (change values to fit). That worked:
$ python /c/Python26/Scripts/pywin32_postinstall.py -install
Copied pythoncom26.dll to C:\WINDOWS\system32\pythoncom26.dll
Copied pythoncomloader26.dll to C:\WINDOWS\system32\pythoncomloader26.dll
Copied pywintypes26.dll to C:\WINDOWS\system32\pywintypes26.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\2.6\Help[None]=None
-> Software\Python\PythonCore\2.6\Help\PythonwinReference[None]='c:\\Python26\\Lib\\site-packages\\PyWin32.chm'
Pythonwin has been registered in context menu
Creating directory c:\Python26\Lib\site-packages\win32com\gen_py
Shortcut for Pythonwin created
Shortcut to documentation created
The pywin32 extensions were successfully installed.`
(On WinXP SP3)
Had the above noted errors with my python27 pywin32 installation that got installed after my installation of ArcGIS on a windows2008 R2 server (trying to centralize our scripts).
Found that for me, I had to start a command line window with run as adminstrator (right click to get that option in windows) and then run the following command line from the c:\arcpy27\arcgis10.1>python c:/arcpy27/arcgis10.1/scripts/pywin32_postinstall.py -install
I see a lot of people have run into this issue and I tried multiple things, but this was finally the solution that managed to get pywin32 installed.
Thanks so much for your solution. Took me a couple of install and uninstalls before I finally ran across your solution and modified it for my version.

dll load errors in python executable made from pywin32

I created python executables with py2exe with both 64bit python interpreter and 32 bit python interpreter.
In my program, I use the module pywin32 com, and so I dl'ed and installed both the 64bit and 32bit versions of the program prior to creating the executable.
The 64bit exe works fine, but the 32 bit one has the following problem:
Traceback (most recent call last):
File "program.py", line 11, in <module>
File "win32com\__init__.pyc", line 5, 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 tried both versions by running the source directly, ie python program.py and C:\python_32\python.exe program.py and both work fine.
Note I tested the 64bit exe on a 64bit windows 7 computer and the 32bit exe on a 32bit windows XP computer.
Older than everything ever, but I stumbled on this problem today, and if anyone else does, what I ran was:
python /c/Python26/Scripts/pywin32_postinstall.py -install from the commandline (change values to fit). That worked:
$ python /c/Python26/Scripts/pywin32_postinstall.py -install
Copied pythoncom26.dll to C:\WINDOWS\system32\pythoncom26.dll
Copied pythoncomloader26.dll to C:\WINDOWS\system32\pythoncomloader26.dll
Copied pywintypes26.dll to C:\WINDOWS\system32\pywintypes26.dll
Registered: Python.Interpreter
Registered: Python.Dictionary
Registered: Python
-> Software\Python\PythonCore\2.6\Help[None]=None
-> Software\Python\PythonCore\2.6\Help\PythonwinReference[None]='c:\\Python26\\Lib\\site-packages\\PyWin32.chm'
Pythonwin has been registered in context menu
Creating directory c:\Python26\Lib\site-packages\win32com\gen_py
Shortcut for Pythonwin created
Shortcut to documentation created
The pywin32 extensions were successfully installed.`
(On WinXP SP3)
Had the above noted errors with my python27 pywin32 installation that got installed after my installation of ArcGIS on a windows2008 R2 server (trying to centralize our scripts).
Found that for me, I had to start a command line window with run as adminstrator (right click to get that option in windows) and then run the following command line from the c:\arcpy27\arcgis10.1>python c:/arcpy27/arcgis10.1/scripts/pywin32_postinstall.py -install
I see a lot of people have run into this issue and I tried multiple things, but this was finally the solution that managed to get pywin32 installed.
Thanks so much for your solution. Took me a couple of install and uninstalls before I finally ran across your solution and modified it for my version.

Categories

Resources