Python: Pyinstaller on mac current directory problem - python

I have created a little script that I want to run as an executable on Mac and Windows.
I created the executable as one file using --onefile and I want to work with files in the same directory as the executable.
In windows os.getcwd() works fine after using pyinstaller but on mac it reverts to the base path:
> /Users/User
Traceback (most recent call last):
File "test.py", line 93, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/user/Invoices/'
[62121] Failed to execute script test
logout
When I run it as a .py file however it gets the right directory on mac as well.
I have tried changing os.getcwd() to os.path.realpath(__file__) yet it still gives the wrong path when converted with pyinstaller.
I want to be able to move the executable around on mac and work with whatever directory it is in.

It turns out that the following works:
dir_path = slash.join(sys.argv[0].split(slash)[:-1])
this works only when using the executable on mac. On windows I still use os.getcwd and when running the python script as well.

Related

Python Executable, PermissionError: [WinError 5]

I'm trying to write a chatting app that when run the first time deletes itself from the current position. Now the code is:
import os
import sys
file=sys.argv[0]
os.remove(file)
if i run it as .py file, it works just fine. Anyway if compile it with pyinstaller, when run by terminal it raises:
Traceback (most recent call last):
File "tests.py", line 6, in <module>
PermissionError: [WinError 5] Denied access: 'tests.exe'
[14320] Failed to execute script 'tests' due to unhandled exception!
nothing changes if i run it as administrator or assigning the file permissions with os.chmod. I have python 3.10, i tried both with python 3.9 and 3.10 and it does not work. I even tried running the command del with the subprocess module with the same result.
This works as .py file because the actual executable is the compiled version of the source code - a separate file/memory space. While the compilation is running the source code .py file is closed and can be removed.
When the program is compiled and run as a .exe file it will not remove itself because it is still open and running and therefore has a permission access denied error.
It is trying to remove itself because file = sys.argv[0]
(If you want to remove your running exe maybe start another program that does the removing from the exe and exit exe so that it is closed before removal - kind of awkward.)

pyinstaller not using qtmodern data

When using the qtmoden library with python.
It works fine when running the code in VS Code.
But after using pyinstaller, it doens't anymore. When opening teh generated .exe file, it states that it does not have access to files located in - C:\Users\MyUsername\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\qtmodern\resources
When running the .exe aplication it errors stating:
Traceback (most recent call last):
File "my_filename.py", line 262, in (this number differs depending on my app/code)
File "qtmodern\styles.py", line 70, in dark
File "qtmodern\styles.py", line 23, in _apply_base_theme
FileNotFoundError: [Errno 2] No such file or directory: " 'C:\sers\MyName\AppData\Local\Temp_MEI166802\qtmodern\resources/styles.qss'
[36020] failed to execute script my_filename
Anybody any idea how to get this fixed?
How to make sure pyinstaller also takes these 2 stypes.py files into account?
I tried adding data with pyinstaller via the --add--data command and including paths with --paths command, But the error message stays the exact same.

Pyinstaller, errors on windows 10

I am beginner in Python. I was testing pyinstaller, installed it using the venv. I had some issues with my script, so I got to test this only with a basic print('Testing') main.py script.
Tried:
- pyinstaller main.py
Makes the dist folder with exe, but when I run the ext, it flahses and closes. Running the exe inside cmd gives me:
Error loading Python DLL 'path-to-project\dist\main\'.
LoadLibrary: The specified module could not be found.
pyinstaller --onefile main.py
This does not make the dist bundle, I get:
File "path-to-project\venv\lib\site-packages\PyInstaller\building\utils.py", line 365, in cacheDigest
with open(fnm, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: ''
Not sure what I am doing wrong.

How to run a Python script in VSCode using the same path of the script? (otherwise it can't find an input file located in the same folder)

I have this Python code: https://github.com/andreagrandi/aoc_2019/tree/master/aoc
which runs perfectly from the terminal (Python 3 required) if I do for example: python aoc_03.py
But if I try to run it from VSCode, taking advantage of the Python extension and integration, I get this error:
(aoc) ➜ advent_of_code_2019 git:(master) /Users/andrea/.virtualenvs/aoc/bin/python /Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py
Traceback (most recent call last):
File "/Users/andrea/Documents/advent_of_code_2019/aoc/aoc_03.py", line 70, in <module>
with open('aoc_03_input.txt', 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: 'aoc_03_input.txt'
My guess is that when invoked from VSCode, the script is run from a different path, so it cannot find the file aoc_03_input.txt which is located in the same folder of the script.
How do I tell VSCode to run my script from the /Users/andrea/Documents/advent_of_code_2019/aoc/ folder, so that it will be able to find my input file?
Thanks
Actually, I should have tried more before asking this question, because I just found the solution, but at this point I will write it here in case it can be useful to anyone:
If I change the path in this way:
with open('./aoc_03_input.txt', 'r') as file:
The file is being open correctly when I run the code in VSCode and when I run it from the terminal. Tested under OSX (but it should work under Linux too. Not sure about Windows).

python.exe don't see files in Windows

I write in CMD:
C:\ProjectStray\Model>python smth.py
But get:
python: can't open file 'smth.py': [Errno 2] No such file or directory
python C:\ProjectStray\Model\smth.py also not works
My files:
On another windows environments that works.
How can I fix that?

Categories

Resources