PyInstaller SSL error with requests module - missing module ssl (_ssl) - python

In my python script, I use the requests module to call an API to GET and POST data.
Python environment: anaconda3, python 3.7 / packages: requests 2.24.0, pyinstaller 3.6, openssl 1.1.1h...
I have the following problem with the EXE-file generated by PyInstaller: When I run this file, I get the following error message. This error doesn't occur when I run my script from Python:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 13, in <module>
File "c:\programdata\anaconda3\envs\...\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified module could not be found.
[20188] Failed to execute script pyi_rth_certifi
If I follow to the excepted error lines (ssl.py):
import _ssl # if we can't import it, let the error propagate
Why can't ssl be imported?
I searched a long time in serveral post on SO but all these answers didn't help, ex.:
Python Requests throwing SSLError
Python Requests - How to use system ca-certificates (debian/ubuntu)?
Twilio Python Module Errors After Compiling
Fixing SSL certificate error in exe compiled with py2exe (or PyInstaller)
Does anyone have an idea how to fix this?
If you need more informations, please write a comment. THX
EDIT: (for Mooncrater's comment)
added Screenshot from python console, entered:
import _ssl
EDIT 2:
Tested "FIX" from SO Question:
Python SSL Import Error in PyInstaller generated executable
-> this didn't fix my problem
EDIT 3: Answer by cbolwerk
THX for your answer, this works!
Python 3.7 anaconda environment - import _ssl DLL load fail error
An additional fix is to install the latest OpenSSL Lib, I used 1.1.1h package in my python script, on my PC was installed an older version and that causes the error too.
cbolwerk's answer I tested on a PC where NO OpenSSL is installed and , as I wrote, it works!

If you can import ssl in the python within your environment, it means that the ssl module is probably inside your environment. This answer mentions the files you can look for inside your environment. They are either inside your env/Library/bin or env/DLLs. I think you have these installed, but pyinstaller doesn't recognize them. To make sure that pyinstaller knows these files, you can add them to datas. This can be edited in the command when building the .exe file or in the .spec file that is probably created when you have run this command once. This link may be useful there.
In short, add the paths of the DLLs I mentioned to the datas (or binaries inside .spec actually) so that they are recognized by pyinstaller.
So either run the command
pyinstaller --onedir --add-data libcrypto-1_1-x64.dll;. --add-data libssl-1_1-x64.dll;. myscript.py
or change your .spec to something like
datas = [('/path/to/libcrypto-1_1-x64.dll', '.'), ('/path/to/libssl-1_1-x64.dll', '.'),
(...) ]
...
a = Analysis(...,
datas=datas,
...)
...
and then run
pyinstaller --onedir myscript.spec
This fixed DLL issues for me at least.

Related

ImportError: No module named pygments.styles when attempting to run Pythontex

When I try to run pythontex via my command line((base) Zachs-MacBook-Pro:mat_300 zachmaurus$ pythontex pythontex.tex) the following occurs:
Traceback (most recent call last):
File "/Library/TeX/texbin/pythontex", line 50, in <module>
import pythontex2 as pythontex
File "/usr/local/texlive/2019/texmf-dist/scripts/pythontex/pythontex2.py", line 61, in <module>
from pygments.styles import get_all_styles
ImportError: No module named pygments.styles
How do I go about solving this issue? I have downloaded pygments with pip install, but for whatever reason when I attempt to run the script the module pygments.style cannot be found.
Solved this issue by adding pythontex_install.py to my PATH using export PATH=$PATH:/usr/zachmaurus/Downloads/pythontex/pythontex_install.py in MacOS Terminal. To ensure that pythontex_install.py has been added to PATH environment run echo $PATH and it should appear as one of the variables. After having added pythontex_install.py to PATH, I now get the following result when I run pythontex pythontex.tex to compile my document:
This is PythonTeX 0.16
--------------------------------------------------
PythonTeX: pythontex - 0 error(s), 0 warning(s)
I solved this problem by explicitly call the interpreter I want, wenn I compile the document.
pdflatex -interaction=nonstopmode document.tex
python3 <path_to_pythontex.py>/pythontex.py document.tex
pdflatex -interaction=nonstopmode document.tex
or for any python interpreter you want:
pdflatex -interaction=nonstopmode document.tex
<path_to_python>/python3 <path_to_pythontex.py>/pythontex.py document.tex
pdflatex -interaction=nonstopmode document.tex
Of course Pygments module schuld be installed by this interpreter. This worked very good for me on macOS.
I got the same problem when I tried to use pythontex on my macbook (on macOS Catalina 10.15).
What I suggest below must be (if used) used with precaution :
I tried so many solutions and what actually worked for me was this :
I made sure that pygments was installed on my machine (I ran 'pip install pygments' on the terminal)
Then I changed the first line of the files pythontex, pythontex2 (because the problem seemed to come from there) and pythontex3
Theses files (if you are using macOS Catalina 10.15 like me) should be in
/usr/local/texlive/2021/texmf-dist/scripts/pythontex
(if you installed TexLive2021) -> you should be able to retrieve this path when you try to compile your tex file anyway...
The first line was replaced by #!/usr/bin/python3 (to ensure that python3 was used instead of python 2.7 which is used by default by macOS) inside the 3 files pythontex, pythontex2 and pythontex3 then :
I ALSO added this line (see the screenshot below) in pythontex3 :
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/')
to ensure that python found the module pygments
(because I installed python3.9 on my machine and the folder "pygments" and its subfolder "styles" was present in the folder "site-packages")
pythontex3 file
So now everything is working fine again and I can use pythontex =)

Pyinstaller Keyring Windows 32 bit

I have the following python test code:
import keyring
print(keyring.get_keyring())
keyring.set_password("a","b","c")
print(keyring.get_password("a","b"))
If I run this code using a 32 bit python or a 64 bit one I obtain the following output (as expected):
<keyring.backends.Windows.WinVaultKeyring object at 0x00000187B7DD6358>
c
My purpose is to build two standalone executable (32bit and 64bit): in order to achieve that I'm using pyinstaller and the following command (test.py is the name of the file containing the python code shown above)
pyinstaller --onefile test.py
If I run the 64 bit exe I obtain the following output (as expected):
<keyring.backends.Windows.WinVaultKeyring object at 0x00000187B7DD6358>
c
Instead, if I run the 32 bit exe I obtain the following output:
<keyring.backends.fail.Keyring object at 0x05463ED0>
Traceback (most recent call last):
File "test.py", line 3, in <module>
keyring.set_password("a","b","c")
File "site-packages\keyring\core.py", line 47, in set_password
File "site-packages\keyring\backends\fail.py", line 23, in get_password
RuntimeError: No recommended backend was available. Install the keyrings.alt package if you want to use the non-recommended backends. See README.rst for details.
[2732] Failed to execute script test
Does anyone know what is going on?
Thanks,
Daniele
Solved using an alternate keyring backend. If I change my code from:
import keyring
print(keyring.get_keyring())
keyring.set_password("a","b","c")
print(keyring.get_password("a","b"))
to:
import keyring
from keyrings.alt import Windows
keyring.set_keyring(Windows.RegistryKeyring())
print(keyring.get_keyring())
keyring.set_password("a","b","c")
print(keyring.get_password("a","b"))
it works.
Setting up the keyrings.alt file from the Keyring Github page does seem to work, and would explain why this was only an issue in Keyring>12, since it was included in the Module before that. I was also able to work around it by installing pip install pywin32 and running the following additions:
import keyring
import win32timezone
from keyring.backends import Windows
keyring.set_keyring(Windows.WinVaultKeyring())
print(keyring.get_keyring())
keyring.set_password("a","b","c")
print(keyring.get_password("a","b"))

Installing SIP for Python 2.7 on windows 10

I have seen many threads that have a high level of ambiguity and go off on tangents from the original question, often assuming much about the authors ability, so I am hoping that if I am direct and concise with my information, I will get an answer that is in line with the requirement. I know that the serious programmers will have seen this many times, in many formats, so please just bear with me as this is doing my head in. Please do not just post a link to some other answer as I rarely find that helps with my current issue.
I am not a hardcore programmer, I find the compiling, sourceball, tar, gz all nonsense to be honest and am looking for the easiest way to install sip for python on my machine. I have installed various versions of mingw32, mingw64 to the point that I don't know which one is best to use. I am assuming that the one here: C:\Program Files\mingw-w64 is the one, considering I am using 64 bit, but do the others I have installed impact on this?
I also installed versions of mysys:
C:\msys\1.0,
C:\msys64, but I still m unclear what and why etc, despite trying to read the docs that came with them.
I have windows 10, 64 bit professional edition.
I have python 2.7
I have installs of mingw, 32 bit and 64 bit in various locations, due mostly to not fully understanding what exactly it was or where it should go. I found zips of it and exes, so I got a bit confused.
I downloaded the sip package and unpacked it to here: C:\Python27\Lib\site-packages\sip-4.19.3 and it has the configure.py file in it. So far, so good.
I used a CMD window, changed directory to: C:\Python27\Lib\site-packages\sip-4.19.3 and then used the command: python configure.py to create the Makefile file which is what I believe is supposed to happen.
I then opened the mingw64 shell, changed directory to the above sip folder and typed: python configure.py again, just to be sure I would get a response and create the files again, probably should not have done so, but hey ho, at this point, I am quite frustrated with it and trying to do anything with what I have, which I know is poor practice. (see image 1.)
image 1: configure.py executed
From what I have read, I should use the make function that comes with Mingw64, but I tried the following, which also includes the configure.py code, but nothing seems to work when trying to use the Makefile file that was created via the configure.py process.
c:\Python27>cd ./Lib/site-packages/sip-4.19.3
c:\Python27\Lib\site-packages\sip-4.19.3> python configure.py
This is SIP 4.19.3 for Python 2.7.13 on win32.
The SIP code generator will be installed in C:\Python27.
The sip module will be installed in C:\Python27\Lib\site-packages.
The sip.pyi stub file will be installed in C:\Python27\Lib\site-packages.
The sip.h header file will be installed in C:\Python27\include.
The default directory to install .sip files in is C:\Python27\sip.
Creating siplib\sip.h...
Creating siplib\siplib.c...
Creating siplib\siplib.sbf...
Creating sipconfig.py...
Creating top level Makefile...
Creating sip code generator Makefile...
Creating sip module Makefile...
c:\Python27\Lib\site-packages\sip-4.19.3> Makefile
'Makefile' is not recognized as an internal or external command,
operable program or batch file.
c:\Python27\Lib\site-packages\sip-4.19.3> make Makefile
make: Nothing to be done for `Makefile'.
c:\Python27\Lib\site-packages\sip-4.19.3> Makefile Makefile
'Makefile' is not recognized as an internal or external command,
operable program or batch file.
c:\Python27\Lib\site-packages\sip-4.19.3>
So now I am at an impasse. I have the locations of my mingw versions and the msys in my path environment variable and I have done just about everything I have looked at on the web. I realise that its an order of things, but I really wish there were just executables for these modules and supporting tools as this compiling is a ball ache.I tried opening a python shell and importing sip.
>>> import os, sys
>>> import sip
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import sip
ImportError: No module named sip
>>> import sipconfig
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import sipconfig
ImportError: No module named sipconfig
>>> from sip import sip
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
from sip import sip
ImportError: No module named sip
>>> from sipconfig impport sip
SyntaxError: invalid syntax
>>> from sip import *
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
from sip import *
ImportError: No module named sip
So, if anyone has read through this and has a definitive answer as to what I am doing wrong, I would appreciate it.
make/nmake or make/nmake install cant be done from cmd and python path; instead using mingw or visual studio tools.
In windows search for prompt or Visual studio tools to open VS Command promt and from there cd--> sip file location (before this run configure.py present in sip folder)
Now run the commands nmake and then nmake install

Add a Python module to a Google App Engine project

I'm trying to use the feed-parser module for this project im working on. When I upload the files to App Engine and I run the script it comes back with the error that the there is no module named feed-parser.
So I'm wondering if and how can I install this module on App Engine, so that I can fix this error and use RSS.
Error:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/base/data/home/apps/s~vlis-mannipulus-bot/1.391465315184045822/main.py", line 7, in <module>
import feedparser
ImportError: No module named feed parser
Development 1:
So I've tried installing the module in the lib directory i created(in this fail example i forgot the /lib at the --prefix=..). And i get PYTHONERROR as is shown in the shell. Ive done some research on python paths and the solutions i tried didn't work for me.
kevins-MacBook-Pro-2:~ KevinH$ cd /Users/KevinH/Downloads/feedparser -5.2.1
kevins-MacBook-Pro-2:feedparser-5.2.1 KevinH$ sudo python setup.py install --prefix=/Users/KevinH/Documents/Thalia\ VMbot/Thalia-VMbot/
Password:
running install
Checking .pth file support in /Users/KevinH/Documents/Thalia VMbot/Thalia-VMbot//lib/python2.7/site-packages/
/usr/bin/python -E -c pass
TEST FAILED: /Users/KevinH/Documents/Thalia VMbot/Thalia- VMbot//lib/python2.7/site-packages/ does NOT support .pth files
error: bad install directory or PYTHONPATH
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/Users/KevinH/Documents/Thalia VMbot/Thalia-VMbot//lib/python2.7/site- packages/
and your PYTHONPATH environment variable currently contains:
''
Here are some of your options for correcting the problem:
* You can choose a different installation directory, i.e., one that is
on PYTHONPATH or supports .pth files
* You can add the installation directory to the PYTHONPATH environment
variable. (It must then also be on PYTHONPATH whenever you run
Python and want to use the package(s) you are installing.)
* You can set up the installation directory to support ".pth" files by
using one of the approaches described here:
https://pythonhosted.org/setuptools/easy_install.html#custom- installation-locations
Please make the appropriate changes for your system and try again.
Then i tried with the "pip" command but then i get this:
can't open file 'pip': [Errno 2] No such file or directory
According to what I have read "pip" should be a default program installed with python 2.7 and up. So to be sure i did install python3.5 and ran it with that and still get the same error. I typed this with both pythons:
kevins-MacBook-Pro-2:feedparser KevinH$ python3 pip -m install feedparse
--
Not sure if this would work, but via terminal i went to the default directory where feed parser has been installed on my system and copied it to the lib directory i made. Then I've created the config file with the following:
from google.appengine.ext import vendor
# Add any libraries installed in the "lib" folder.
vendor.add('lib')
Deployed it and im still getting the same error as above no module named feeedparser.
Apologies if im doing something stupidly wrong, im still in the learning process.
The App Engine documentation that explains how to add third party modules is here
In summary, you will need to add a folder, usually named 'lib', to the top level off your app, and then install feedparser into that folder using the commands described in the documentation. You will also need to create an appengine_config.py file as descibed in the documentation.
Note that not all third party packages can be uploaded to App Engine - those with C extensions are forbidden. Feedparser looks OK to me though.
EDIT: further comments based on edit "development1" to the question.
Your appengine_config.py looks good.
You "lib" folder should be your application folder, that is the same folder as your app.yaml and appengine_config.py files.
You need to install the feedparser package into the lib folder. The Google docs recommend that you do this by running the command
pip install -t lib feedparser
This command will install the feedparser package into your lib folder.
You need to install and run a version of pip that works with Python2.x - the Python3 version will create a version of feedparser that only runs under Python3.
If you can't install a pip for Python2 this question might provide the right solution, otherwise I'd suggest you ask a separate question about how to install feedparser into a custom install directory on a Mac.I don't have a Mac so I can't help with this.
Once you have feedparser installed in your lib folder, verify that your app works locally; in particular verify that it's using your lib/feedparser installation: try logging feedparser.__file__ after importing feedparser to check the location of the file being imported.
Once everything is working locally you should be able to upload to GAE.
So in summary, your appengine_config.py looks good, but you need to make sure that the right version of feedparser is installed into the lib folder in your app folder before uploading to App Engine.

Difficulties installing SOAPpy module

I am very new to python and have been figuring out how to install modules.I have been trying to install the SOAPpy module and have only had success with the required modules fpconst, wstools, and setuptools(not mentioned as required, but still ended up being required).
I am getting an error when I try and install the SOAPpy module.
Traceback <most recent call last>:
File "...\setup.py", line 43, in module <module>
__version__ = load_version()
File "...\setup.py", line 35 in load_version
execfile(filename, d.__dict__)
IOError: [Errno 2] Unable to load the version number (no such file or directory):
'...\\src\\SOAPpy\\version.py'
Not sure what i should do to fix this.
Any help is greatly appreciated!
Looking at SOAPpy's setup.py file, it tries to import SOAPpy.version. This doesn't work if you're calling it from somewhere else. Try running it directly in the SOAPpy directory because Python adds your current working directory to sys.path. For example:
cd C:\Users\eclaird\Download\SOAPpy\ # The folder with setup.py
python.exe setup.py install
Thanks for the help.
I eventually found what the issue was. I had to get some other required modules for the install to work, also the tar file I got was missing directories, so I found the completed version on Github.

Categories

Resources