Convert python file into exe using auto-py-to-exe - python

I have just finished a project where I have made a connect 4 game and am trying to convert it to an exe file using auto-py-to-exe.
I want to use the one-file option, however every time it finishes and I run it, it would come up with an error:
Failed to execute script 'main' due to unhandled exception: No file 'Assets/icon.png' found in working directory '...'
Then in the box it says:
Traceback (most recent call last):
File "main.py", line 32, in <module>
FileNotFoundError: No file 'Assets/window-icon.png' found in working directory '...'.
I've tried quite a few alterations, e.g. not using the image, but then it would come up with the same error but for a different added file.
How can I fix this?
EDIT: I've tried it again by using the os module and giving the full directories to all the files in main.py, but that hasn't changed anything.

I was also facing this problem then I got the solutions from analysing other threads.
You have to update your script by adding this function
import sys
import os
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path=getattr(sys,'_MEIPASS',os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
and changing address of every additional files used in your script
for example
icon = resource_path("game.icon")
You just have to use auto-py-to-exe just like shown below
Showing the options you can just use
Add all the files used in the Additional File section of it and make sure that the period(.) is there in the destination
That's it !

Related

Python on RPi: Errno 2 No such file or directory: 'config.json' but config.json is in the same folder as the main.py script

Hey all
I am trying to run a python script from GitHub, https://github.com/dudisgit/gmod_toolgun_prop for a project with a functioning screen and I put a command at the end of the .bashrc file
python3 /home/pi/gmod_toolgun_prop-main/main.py
so that the code executes as soon as the RPi powers up. When running the script in Thonny's Python IDE on my RPi 2B it executes no problem and the screen works. However when I open terminal I get an error message from the code running in the .bashrc file:
Traceback (most recent call last):
File "/home/pi/gmod_toolgun_prop-main/main.py", line 381, in <module>
main()
File "/home/pi/gmod_toolgun_prop-main/main.py", line 357, in main
with open(args.config) as config_file:
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
However, the config.json file is in the same folder as the main.py file as shown below:
Screenshot of file explorer showing config.json is in the same folder as main.py
And here's a screenshot of the error message
And here's the code that is refenced in the error message as line 357:
with open(args.config) as config_file:
config = json.load(config_file)
The entirety of the main.py script is in the Github link as attached in the first paragraph as well as the config.json file and other relevant files.
I am fairly new to the Python programming space so I don't understand what could be causing this error nor how this script handles opening the config.json file.
I have tried creating a custom service but it spits out the same error. Crontab and the local.bashrc file just doesn't work straight up for this. This is the furthest I have got with it attempting to execute on boot.
Maybe it helps to use the absolute path of config.json, i.e.(according to your description)
/home/pi/gmod_toolgun_prop-main/config.json
instead of the simple filename config.json.
If Python says the file is not there, then the file is not there. The only question is, where is there?
The filename in this case is config.json. Since there's no / (no directory name), the name is taken to be relative to the current working directory. That might or might not be the same as the directory of the main Python module, here /home/pi/gmod_toolgun_prop-main/main.py.
You can verify that by printing the current working directory just before opening the file. You can use os.getcwd to do that. Or, use strace(1) to show the interpreter's attempts to open config.json.

Playsound python module can't find file even in same folder

I have imported the playsound python module and put a mp3 file in the same folder as the python file, and yet when I run the file all I get is an error message saying
open Sound.mp3
Cannot find the specified file. Make sure the path and filename are correct.
Error 263 for command:
close Sound.mp3
The specified device is not open or is not recognized by MCI.
Failed to close the file: Sound.mp3
Traceback (most recent call last):
File lookup is usually relative to the current working directory and not the Python source file itself. You could either run the script from the directory with the sound file (cd /path/to/sound_files, python3 /path_to_script), or pass and absolute path to the sound file when calling playsound.
If you're using an IDE, the working directory can usually be set in the run configuration.

Accessing Sub Directories and Files Within System32 Using Python 3

I have the following code:
import os
print (os.listdir("C:/Windows/System32/config"))
print (os.listdir("C:/Windows/System32/winevt"))
I am running this Python script as an administrator and confirmed those directories in fact exist.
The first line outputs with some files and folders, but not all files. For example, there is a registry hive contained within called "SOFTWARE" which does not appear in the output.
The second line says the path wasn't identified. Here is the full output:
['Journal', 'RegBack', 'systemprofile', 'TxR']
Traceback (most recent call last):
File "test.py", line 3, in <module>
print (os.listdir("C:/Windows/System32/winevt"))
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:/Windows/System32/winevt'
This module (os.listdir), works pretty much everywhere else on the system, but not within System32. I suspect this may be permission related, but am not too sure on how to fix it as I am already running the script as an administrator.
Help would be appreciated. Thanks!

Pygame breaks outside of Pydev.

Me and a team of friends created this game which I am now trying to run in linux,
We developed it using python 2.7 and Pygame in windows using Aptana studio and the code fully work when running through there.
When downloading it to linux It wouldn't load saying that it could not find files. I then tried running it via CMD in windows and there is the same error.
the error so far is
Traceback (most recent call last):
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 144, in <module>
showMenu()
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 107, in showMenu
menu.init(['Start','About','Quit'], surface)
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 52, in init
self.create_strukture()
File "/home/user/Desktop/Raspberroids/mainmenu.py", line 73, in create_strukture
self.font = pygame.font.Font(self.font_path, self.fontsize)
IOError: unable to read font filename
And the source is at:
https://github.com/ryanteck/RasPiThon/tree/master/Raspberroids/Source%20Code
Happens on both 2.7 and 2.6
Can anyone help?
Your font path data/coders_crux/coders_crux.ttf is relative.
When you start your game from another directory than your source directory, pygame can't find the font.
A simple fix is to add the following lines to the top of your script (mainmenu.py):
import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))
os.path.realpath(\__file__) will get the path to your script, and with os.chdir and os.path.dirname you change the current working directory to the directory of your script.
This way, the relative paths you use will work.
PyDev sets working directory and PYTHONPATH variable for your programs. It can also set console encoding to something different than OS defaults.
Add a print self.font_path statement before creating your Font object and see if path is OK. If it's relative path, you could also use os.path.abspath (see os.path docs for details) to get better understanding of what's going on.

Why do i get this traceback error?

This is the error i get:
Traceback (most recent call last):
File "dwload.py", line 9, in <module>
File "Pmw\__init__.pyc", line 28, in <module>
WindowsError: [Error 3] The system cannot find the path specified: "C:\\Users\\P
'sao\\Desktop\\dist\\library.zip\\Pmw/*.*"
I get the error after compiling the python file into the executable with py2exe.
Does anybody know why i get this error?
Found the solution: http://billyjin.kodingen.com/punbb-1.3.4/viewtopic.php?id=310
Well, the error message says that the path was not found, and if you look at the path, you appear to have mixed up backslashes and slashes as path separators. How are you constructing the path you're using?
I had the same problem when using pyinstaller and tkinter table (py 2.7). The problem was solved by removing the Pwm dependency in the tkintertable lib metadata. Its will help you, if pwm dont used in program
Finally, I found the solution:
First, you need to run the bundlepmw.py in the C:\Python27\Lib\site-packages\Pmw\build\lib\Pmw\Pmw_1_3\bin folder following this command: python bundlepmw.py C:\Python27\Lib\site-packages\Pmw\Pmw_1_3\lib to create the Pmw.py.
Read this page: folk.uio.no/hpl/scripting/doc/python/Pmw/dynamicloader.html, after fixing the bundlepmw.py based on this link sourceforge.net/p/pmw/discussion/33675/thread/f0bd7f34.
You should be able to generate the Pmw.py file.
Then copy the Pmw.py plus PmwBlt.py and PmwColor.py into the main script directory and run your setup.py. Sweet

Categories