Problem making python program into an exe - python

I have been working on a program that shows the weather using python and thought I would try setting it up as an exe for easy installation on other devices. However every time I put in a location to search for using the exe GUI, I get this in the terminal window that opens with the program window:
Exception in Tkinter callback
Traceback (most recent call last):
File "tkinter\__init__.py", line 1892, in __call__
File "getWeather.py", line 132, in <lambda>
File "getWeather.py", line 45, in open_weather
File "requests\api.py", line 75, in get
File "requests\api.py", line 61, in request
File "requests\sessions.py", line 529, in request
File "requests\sessions.py", line 645, in send
File "requests\adapters.py", line 417, in send
File "requests\adapters.py", line 228, in cert_verify
OSError: Could not find a suitable TLS CA certificate bundle, invalid path: C:\Users\pcusername\AppData\Local\Temp\_MEI118802\certifi\cacert.pem
I have been trying to figure out what the problem is, but haven't been able to find anything to get it working. If anyone can help figure this out I would appreciate it. Also, let me know if any more context/code is needed or if I did anything wrong regarding the post, as it is my first time having to post on stack overflow.
Note, this is using:
windows 11,
python 3.9,
pycharm 2021.2.2,
pyinstaller for exe creation

Ok ended up figuring it out:
This method does work: (The top answer) python requests can't find a folder with a certificate when converted to .exe
I was however missing the cacert.pem file still, which gave the error when I tried this fix initially. What ended up working is finding the cacert.pem in the mypycharmproject\venv\Lib\site-packages\certifi folder and then copying it to the dist folder where the exe is located.
Thank you for your help everyone!

Related

Writing a network Scanner with python using scapy

I am writing a simple network scanner with python using scapy following is my code :
import scapy.all as scapy
def scan(ip):
scapy.arping(ip)
scan("192.168.1.1/24")
Error I am getting :
Traceback (most recent call last):
File "ipScanner.py", line 10, in <module>
scan("192.168.1.1/24")
File "ipScanner.py", line 8, in scan
scapy.arping(ip)
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/layers/l2.py", line 648, in arping
filter="arp and arp[7] = 2", timeout=timeout, iface_hint=net, **kargs) # noqa: E501
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/sendrecv.py", line 553, in srp
filter=filter, nofilter=nofilter, type=type)
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/arch/bpf/supersocket.py", line 242, in __init__
super(L2bpfListenSocket, self).__init__(*args, **kwargs)
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/arch/bpf/supersocket.py", line 62, in __init__
(self.ins, self.dev_bpf) = get_dev_bpf()
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/arch/bpf/core.py", line 114, in get_dev_bpf
raise Scapy_Exception("No /dev/bpf handle is available !")
scapy.error.Scapy_Exception: No /dev/bpf handle is available !
Exception ignored in: <function _L2bpfSocket.__del__ at 0x105984c20>
Traceback (most recent call last):
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/arch/bpf/supersocket.py", line 139, in __del__
self.close()
File "/Users/omairkhan/opt/anaconda3/lib/python3.7/site-packages/scapy/arch/bpf/supersocket.py", line 211, in close
if not self.closed and self.ins is not None:
AttributeError: 'L2bpfSocket' object has no attribute 'ins'
Can anyone please help understand it.
NOTE: I am running it on mac OS.
I wrote this exact program when I first started programming with matching syntax, and it ran correctly on my systems when run as administrator. I develop on Linux and Windows rather than Mac, but I will offer what I can.
Are you running this script through your IDE or calling it from the shell?
I recommend only running it from the shell. This simply gives you more control over the files like specifying which version of python the script is, and if you need administrative privileges for a script, you can elevate the script permissions in the shell.
Also, in my OS, I was taught to always use, and have experienced the mistakes of forgetting this, always add:
#!/usr/bin/env python
as the first line of every script. At least in Linux, it tells the PC how to treat the file (it tells it to treat the file as a python file--yes I acknowledge that its already running it as python). I would check to see if that is valid for MacOS file system.
Most of what I have recommended so far comes down to no /dev/bpf handle is available, only ever being an issue for me when I'm not running script as an administrator (although Linux states permission denied). And I shouldn't leave out that using Anaconda on Windows in the past (before I understood the structure of my file systems) prevented me from using common modules like pygame and scapy. I could only guess in that case Anaconda prevented the PC from knowing where to find every piece of that module by making the computer think it had its own one of that module under Anaconda directory when it was in a different PATH.

Simple Pong game will not run as an .exe

I am new to this forum and have limited python experience. Since this quarantine I decided to get back into learning python. I have over the past few days made some working games using both curses and pygame. These games function properly on my PC however I would like to get them into an .exe format. I have used pyinstaller in order to create the .exe however my pong game (which uses pygame) crashes once you try to open the .exe file. I am assuming it may have something to do with the import of pygame. Any help would be much appreciated.
Here is a link to my code on GitHub:
https://github.com/nick-cheshire/PythonGames-/blob/master/Pong.py
Thanks to #TheBigKahuna I was able to create a .bat file and run it and here are the errors I recieved :
``Traceback (most recent call last):
File "Pong.py", line 137, in <module>
pong()
File "Pong.py", line 90, in pong
game_font = pygame.font.Font("freesansbold.ttf", 32)
File "site-packages\pygame\pkgdata.py", line 50, in getResource
File "site-packages\pkg_resources\__init__.py", line 1134, in resource_exists
File "site-packages\pkg_resources\__init__.py", line 1404, in has_resource
File "site-packages\pkg_resources\__init__.py", line 1472, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[18912] Failed to execute script Pong``
I have managed to make it work. The problem was with font. Change your line
game_font = pygame.font.Font("freesansbold.ttf", 32)
with
game_font = pygame.font.SysFont("Arial", 20)
I assume that the problem is with locating the file. Please correct if I am wrong.
Now when making .exe with pyinstaller, make sure your file is named main.py and since you are building a GUI you will want -w (without a console) parameter. So, I suggest running powershell as administrator, navigate to your main.py and:
pyinstaller --onefile -w main.py
Hope this works.

Problem with Pyinstaller when using ITKPython

I am using ITKPython,
My codes are working well in PyCharm with *.py format but after making a standalone *.exe file via PyInstaller some errors occure as follow:
Traceback (most recent call last):
File “RSG_V_0.py”, line 27, in <module>
File “site-packages\itkExtras.py”, line 449, in imread
File “site-packages\itkLazy.py”, line 40, in getattribute
AttributeError: ‘LazyITKModule’ object has no attribute ‘ImageFileReader’
[29316] Failed to execute script RSG_V_0
Would you please show me the way to solve these kind of problems.
By the way, already I test my PyInstaller with a simple print.py, In fact PyInstaller is working correctly without ITK filters.
Thanks
Sina
That is most likely a problem in PyInstaller. Report it on their issue tracker.

Trying to make PLY work for the first time

I'm new to Python and I'm having some problems trying to make PLY works. For now, all I want is to successfully run the example from the PLY homepage.
At first I tried to just download PLY-3.8, put the ply folder in the same directory I saved the example (calc.py) and ran it. The calc.py file is at the C:\Users\...\Python directory and the ply folder is the C:\Users\...\Python\ply, just to make it clearer. But I got an ImportError: No module named 'ply'.
Then I searched for a while, tried to update something called distutils and install the modules through the Windows PowerShell and so on and so forth, but none of that worked and I just reset the whole thing (reinstalling Python and all of that). But then I finally got it to work by simply inserting into the sys.path the directory path where the script I was running (edit: in interactive mode) was, by doing this:
import sys
sys.path.insert(0,'C:\\Users\\ ... \\Python')
This fixed the ImportError but, and this is where I am now, there are a bunch of other errors:
Traceback (most recent call last):
File "C:\Users\...\Python\calc.py", line 48, in <module>
lexer = lex.lex()
File "C:\Users\...\Python\ply\lex.py", line 906, in lex
if linfo.validate_all():
File "C:\Users\...\Python\ply\lex.py", line 580, in validate_all
self.validate_rules()
File "C:\Users\...\Python\ply\lex.py", line 822, in validate_rules
self.validate_module(module)
File "C:\Users\...\Python\ply\lex.py", line 833, in validate_module
lines, linen = inspect.getsourcelines(module)
File "c:\users\...\python\python35\lib\inspect.py", line 930, in getsourcelines
lines, lnum = findsource(object)
File "c:\users\...\python\python35\lib\inspect.py", line 743, in findsource
file = getsourcefile(object)
File "c:\users\...\python\python35\lib\inspect.py", line 659, in getsourcefile
filename = getfile(object)
File "c:\users\...\python\python35\lib\inspect.py", line 606, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module
Now I have absolutely no idea what to do. I tried to search for a solution but had no luck. I appreciate if anyone can help me out.
I'm on Windows 10, using Python 3.5.0 and iep as my IDE (www.iep-project.org) if these informations are of any importance.
In short: I just want to successfully run the example from the PLY homepage and then I think I can figure out the rest.
EDIT: I found out that if I do:
import inspect
inspect.getfile(__main__)
I get the exact same (last) error from before:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "c:\users\...\python\python35\lib\inspect.py", line 606, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module
I think this is the culprit, but I still don't know how to fix it.
EDIT 2: I got it to work and answered the question explaining how, but if someone have a more complete answer, I would love to hear it.
To anyone having this problem, I found what was the issue. I still don't know why exactly is like that, so if anyone have a more complete answer to provide I would appreciate (I'm still a newbie at Python).
Anyway, it seems this code can't be executed in Interactive mode, it needs to be executed as a script. To do that on IEP it's Run > Run file as script or Ctrl+Shift+E. On IDLE you need to Open... the file (Ctrl+O) and then Run Module (F5).
As to why it can't be executed in Interactive mode, here's a little bit about the difference between interactive mode and running as script from the IEP wizard:
Interactive mode vs running as script
You can run the current file or the main file normally, or as a script. When run as script, the shell is restared (sic) to provide a clean environment. The shell is also initialized differently so that it closely resembles a normal script execution.
In interactive mode, sys.path[0] is an empty string (i.e. the current dir), and sys.argv is set to [''].
In script mode, __file__ and sys.argv[0] are set to the scripts filename, sys.path[0] and the working dir are set to the directory containing the script.
That explains a bit about why the inspect.getfile(__main__) was throwing an error: the __main__ had no attribute __file__. And also why I had to insert the current directory into sys.path: sys.path didn't had the current directory in interactive mode.
I hope this helps someone.

Enthought Canopy crashes upon opening mac

So yesterday I updated to Enthought version 1.1 and now it refuses to open. I've rebooted my computer as well as did a re-install of enthought canopy. I keep getting the following error
Traceback (most recent call last):
File "build/bdist.macosx-10.5-i386/egg/canopy/app/bootstrap.py", line 1989, in main
File "build/bdist.macosx-10.5-i386/egg/canopy/app/bootstrap.py", line 1021, in main
File "build/bdist.macosx-10.5-i386/egg/canopy/app/bootstrap.py", line 1012, in _ kill_leftover_procs
File "build/bdist.macosx-10.5-i386/egg/canopy/app/running_process_manager.py", line 116, in kill_leftover_procs
File "/Applications/Canopy.app/appdata/canopy-1.1.0.1371.macosx-x86_64/Canopy.app/Contents/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "build/bdist.macosx-10.5-i386/egg/canopy/app/running_process_manager.py", line 59, in lock
LockError: Lock could not be acquired
I have no idea what's going on here. I've sent the error report to enthought but does anyone have any ideas?
I think I did it. Try searching for these files in your Terminal. They're inside your .canopy folder. Make sure that you're working on your root directory. They're not searchable via Finder. My Canopy's finally working now. Hope this helps.
proc_manager.lock
process.lck
running_procs.pkl
Somehow it seems like a lock file didn't get cleaned in the process. Look into the ~/.canopy folder and remove the process.lck file. You may also start your Activity Monitor and make sure there is no stray canopy or python process, and kill it if there is (or log out of OSX and log back in, which will do the same thing). Canopy will run as normally after that.

Categories