error when compiling shitil with pyinstaller - python

Pyinstaller & shutil [function copy] problem !
hi I'm trying to convert a .py code into .exe
actually my code is similar to that :
dr = os.environ['WINDIR']
current = os.path.abspath(__file__)
shutil.copy(current, dr)
as we can see the code copy the current fine to windows file (this is just an example ) ; the code actually works fine but if we compile it into exe and run it we will see .
Traceback (most recent call last):
File "<string>", line 13, in <module>
File "C:\Documents and Settings\john\Desktop\pyinstaller\pyinstaller-2.0\test
\build\pyi.win32\test\out00-PYZ.pyz\shutil", line 119, in copy
File "C:\Documents and Settings\john\Desktop\pyinstaller\pyinstaller-2.0\test
\build\pyi.win32\test\out00-PYZ.pyz\shutil", line 82, in copyfile
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\john
\\Desktop\\pyinstaller\\pyinstaller-2.0\\test\\dist\\test.py'
the error is from function copy because it doesn't find test.py in \test\dist\test.py .
any solutions ?
i tried :
dr = os.environ['WINDIR']
current = os.path.abspath(__file__)
newfile = current.replace('py','exe')
shutil.copy(current, dr)
but it still say
IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\john
\\Desktop\\exeinstaller\\exeinstaller-2.0\\test\\dist\\test.exe'

It seems that when compiling into exe file os.path.abspath(__file__) copies the file location before compiling and is not treated as a variable.

Related

opening a .txt file in same folder as .py not working

My vscode is having problems using the open() function to open a file.
I am trying to open a .txt file. Both the py file and .txt file are in the same folder (named "practice"), but python terminal is erroring out with
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('ex_text.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'ex_text.txt'
PS C:\Users\liamt\OneDrive\Desktop\VSCode> python -
I tried using the relative path to the .txt file but a permission error came up.
Traceback (most recent call last):
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice'
PS C:\Users\liamt\OneDrive\Desktop\VSCode>
What am I doing wrong? Really appreciate any help.
When using vscode, open the entire folder/directory containing all the files by using file > open folder, and not just double clicking the python file alone. By opening the entire folder, it is included in the workspace and can be opened by the python file that exists along with it.

Pyinstaller Error Bundling Egg Files FileNotFoundError: [Errno 2] No such file or directory

I am attempting to convert my python file (.py) into a Windows executable (.exe). To do this, I am using Pyinstaller. My python includes the module PyBluez for Bluetooth capabilities. When I run the command pyinstaller <path> -F from the directory of the py file, it gives an error:
FileNotFoundError: [Errno 2] No such file or directory: 'c:\users\X\appdata\local\programs\python\python37-32\lib\site-packages\pybluez-0.22-py3.7-win32.egg\bluetooth\widcomm.py'
After quite a bit of research, I am unable to figure out what is wrong with my program. Do I need to specifically specify where to find the egg file? If so, How would I do that? Thank you! Also, I tried to use cx_Freeze and it also gave an error about the bluetooth module:
Traceback (most recent call last):
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts__startup__.py", line 14, in run
module.run()
File "C:\Users\X\AppData\Local\Programs\Python\Python37\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.dict)
File "BuzzerBeater.py", line 3, in
ModuleNotFoundError: No module named 'bluetooth'
I had the same problem, and managed to solve it by unzipping the .egg file to a folder with the same name (including .egg in the folder name), in my case C:\Users\X\AppData\Local\Programs\Python\Python37\Lib\site-packages\PyBluez-0.22-py3.7-win-amd64.egg\.

Trying to copy a file in Python script, but it doesn't work

I'm trying to copy a file (image.jpg) from the folder 'src' to the folder 'dst', but I've got an error:
Traceback (most recent call last):
File "exec.py", line 7, in
shutil.copyfile(file, destination)
File "C:\Users\mike\AppData\Local\Programs\Python\Python35-32\lib\s
hutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'image.jpg'
This is my code:
import shutil, os
source = os.listdir('C:/Users/mike/Pictures/src/')
destination = 'C:/Users/mike/Pictures/dst/'
for file in source:
shutil.copy(file, destination)
Python 3.5 / Windows 7
os.listdir returns the names, but they don't have the directory prefix on them, you need to add this when copying.
for file in source:
shutil.copy(os.path.join('C:/Users/mike/Pictures/src/', file), destination)

Python IOError: No such file or directory

I have a python program which inputs a .wav file name from the user, does some filtering and writes the output to another .wav file. The programs works perfectly well when I run it using the interpreter. But when I run the program using command prompt, it prompts for the input audio filename, but when I type the .wav filename and press enter, I get the following error:
Traceback (most recent call last):
File "C:\python_audio\stereo.py", line 15, in <module>
(fs, x) = read(audio_in,'rb')
File "C:\Python27\Lib\site-packages\scipy\io\wavfile.py", line 155, in read
fid = open(filename, 'rb')
IOError: [Errno 2] No such file or directory: 'chostakovitch.wav'
The python program stereo.py and the .wav file (chostakovitch.wav) are in the path C:\python_audio, and I have added this path as an environment variable.
But I can't figure out why I am still getting this error?

Py2exe can't seem to find my file in library.zip

I am trying to create an exe file from my program written in Python 2.7 (using GTK+ layouts). I have consulted this guide: http://www.py2exe.org/index.cgi/Py2exeAndPyGTK and tried to set up my files accordingly. I succesfully make an .exe file, but when trying to run it, i get this error:
Traceback (most recent call last):
File "cipherka.py", line 183, in <module>
File "cipherka.py", line 18, in __init__
GError: Nelze otevřít soubor "C:\Repa\cipherka\dist\library.zip\gui.xml": No such file or directory
While I cannot find the gui.xml file in the library.zip archive, it is available on the same level as the main .exe (cipherka.exe) file. If i copy it into the .zip file on my own, it doesn't solve the problem.
Here is my setup.py file:
from distutils.core import setup
import py2exe
import glob
setup(
name="cipherka",
windows = [
{
'script': 'cipherka.py',
}
],
options = {
'py2exe': {
'packages':'encodings',
'includes': 'cairo, pango, pangocairo, atk, gobject, gio',
}
},
data_files=['gui.xml',
'README',
("modules", glob.glob("modules/*.*")),
("media", glob.glob("media/*.png"))
]
)
Any help will be wildly appreciated! And I can give you any necessary info when needed. Thx
UPDATE:
Allright, I implemented the change. However, it does weird things. When i use:
path = os.path.dirname(__file__).replace('\\library.zip','')
it works when ran from the .py file, but fails when compiled with this error:
Traceback (most recent call last):
File "cipherka.py", line 217, in <module>
File "cipherka.py", line 18, in __init__
NameError: global name '__file__' is not defined
When i use
path = os.path.dirname('gui.xml').replace('\\library.zip','')
instead - the program stops running from python and the compiled version gives me the following:
C:\Repa\cipherka\dist\cipherka.exe:188: GtkWarning: gtk_tree_path_append_index: assertion `index >= 0' failed
C:\Repa\cipherka\dist\cipherka.exe:188: GtkWarning: gtk_tree_model_get_iter: assertion `path->depth > 0' failed
Traceback (most recent call last):
File "cipherka.py", line 217, in <module>
File "cipherka.py", line 55, in __init__
File "cipherka.py", line 188, in changed_cb
IndexError: could not find tree path
Any ideas?
"gui.xml" is in "C:\Repa\cipherka\dist\", not "C:\Repa\cipherka\dist\library.zip" as your code supposes.
This should work whether your program is compiled or not.
path = os.path.dirname(__file__).replace('\\library.zip','')
xml_file = open(os.path.join(path, 'gui.xml'))
the error reported is due to scripts running under py2exe do not have a __file__ global. Detect this and use sys.argv[0] instead in your .py file.

Categories

Resources