Python executable - creating directories and file access - python

I have created a simple Python script test.py that is supposed to check if a directory exists, and, if it does not, to create it.
The code is as follows:
import os
def crete_dir(dir_name):
if not os.path.exists(dir_name):
os.makedirs(dir_name)
crete_dir("test_dir")
I am on Mac and use PyCharm to write and test my code.
The script works as expected so long as I run it in PyCharm, but as soon as I turn it into an executable (using pyinstaller: pyinstaller --onefile --clean --windowed test.py) and run it, it does not work (does not create the directory, even though it does not exist). The terminal window, that opens alongside the executable, does not return any file-access-permission or other error.
What should I please do to make the executable work normally?
Many thanks.

Related

Pyinstaller exectuable using data from other folders?

I've written a program that generates a random name and displays a random image along with it using Python and tkinter. However, I want the user to be able to add and remove pictures as well as edit the names of students. That being said, I don't want to package these inside the executable where it can't be changed by the user.
To use PyInstaller, I go into the command prompt and navigate to the working directory and type:
pyi-makespec --windowed --onefile --icon=Assets\\icon.ico random_student.py
Then, in the spec file I change datas to:
datas=[('Assets\\icon.ico', 'Assets')],
Then, I run
pyinstaller random_student.spec
The program runs just fine using PyCharm. And I've done this exact same method on a couple of other .py files an it works. However, they don't need to pull images/text into their programs. This will create an executable, but I can't run it. It gives me a Fatal Error "Failed to execute script random_student". I've tried placing the executable in the working directory and in the pictures folder, but neither work.
I'm currently using Windows 10 64-bit and Python 3.6.6
I'd appreciate any kind of help I can get with this!
SOLUTION: I removed the --windowed option so I could actually read the error. Then realized I didn't have Pillow installed so it was unable to be packaged. Thank you for the help.

Getting cmd window with python exe from py2exe

I have generated python exe using py2exe but getting cmd window when I run my program.
I have changed my files from .py to .pyw and again generated .exe file but I am still getting cmd window.
How to generate exe that doesnot show cmd.
Note I am using Tkinter in my code.
Thanks
There is a simple way of doing it in Python:
Change your directory with cd into this one where your desired .py file is.
Pip install Pyinstaller.
when you succeeded typing following line: pyinstaller.exe --onefile -w NAME.py and hit enter. NAME is the name of your specific py file.
Now have a look in your chosen directory where are new files created. Look for the exe file which should be in the Dist folder.
Just copy paste it where ever you want and delete the rest.
Still
questions?
Just watch this video.

Pyinstaller-compiled exe can't find file with absolute path

I compiled my Python GUI with Pyinstaller on Windows 10 but it seems like it cannot find my other script even though I provided the hard-coded absolute path to it (with r'"C:\Program Files...script path..."'). I even tried os.isfile (script path) but it returns False. The python script was compiled with pyinstaller --onefile --windowed --icon=iconimage.ico myscript.py from the command prompt. I use this same command on Ubuntu and the binary works just fine. I read something about Pyinstaller creating a temporary directory which I found, but I don't think it matters where it's running from as long as I give it the full path, so I'm thinking maybe I need more options when compiling? The GUI opens just fine. It's when it needs to call the script that it doesn't do anything. There are no errors when I run it from the command prompt. Please help!
Solved by adding --onedir which will put everything needed to run the program in one directory in the dist folder.

How to make a Python script run without it opening the console window?

I want to double click on my Python script (which uses a Tkinter GUI) and I just want it to open the Tkinter window, not the console window.
To do this, I changed the extension from .py to .pyw and this seems to work fine on Windows but when I double click my .pyw file on a Linux machine, it doesn't work. It simply froze and I had to restart my system.
Please suggest a platform-independent solution that would help me to run the Python script without opening the terminal/command prompt.
it's been a while since i tried on linux, but i believe it should be fairly simple, firstly you need to put a shebang at the top of the script so your shell knows which executable to use:
#!/usr/bin/python
or if you want a specific version you can expand this to:
#!/usr/bin/python3.2
using whichever version you want (only works for first 2 digits)
then you need to mark it as executable:
chmod 711 myfile.py
for more info on this see this page
then when you double click it, on the rpi (last i used linux) it asks if you want to execute it, or execute it in the terminal.
if you choose to execute it without the terminal, you should only see the tkinter GUI
You can use pyinstaller to create the executables for the different platforms you want.
For example,
The syntax of the pyinstaller command is:
pyinstaller [options] script [script ...] | specfile
In the most simple case, set the current directory to the location of your program myscript.py and execute:
pyinstaller myscript.py
PyInstaller analyzes myscript.py and:
Writes myscript.spec in the same folder as the script.
Creates a folder build in the same folder as the script if it does not exist.
Writes some log files and working files in the build folder.
Creates a folder dist in the same folder as the script if it does not exist.
Writes the myscript executable folder in the dist folder.
In the dist folder you find the bundled app you distribute to your users.
Normally you name one script on the command line. If you name more, all are analyzed and included in the output. However, the first script named supplies the name for the spec file and for the executable folder or file. Its code is the first to execute at run-time.
For certain uses you may edit the contents of myscript.spec (described under Using Spec Files). After you do this, you name the spec file to PyInstaller instead of the script:
pyinstaller myscript.spec
You may give a path to the script or spec file, for example
pyinstaller options... ~/myproject/source/myscript.py
or, on Windows,
pyinstaller "C:\Documents and Settings\project\myscript.spec"
pyinstaller

PyInstaller Issue with Application File Python 2.7

I am trying to use PyInstaller for generating an .Exe from a Python 2.7 file. In the CMD window, I run pyinstaller myfile.py.
It creates a build and dist folder both of which have a number of files, including an Application file. When I click both application files, a CMD box pops up and very quickly disappears, despite my file requiring inputs from the user.
What I am missing here? Which file can I distribute to be a usable copy?
By default, PyInstaller generates a one-folder bundle containing an executable, it also creates this executable with a console window for standard input/output. I'm just guessing, but your script don't have a GUI, right?
In any case, the better way to work is creating a one-file bundle:
pyinstaller -F myfile.py
In this way, you only have to execute one file.
if after executing the application, it behaves in the same way, I would say that adding the -d option will help you to find out what is going wrong with your generated executable. Also, running your application from a per-existing CMD window is recommendable since these windows do not close itself after running your application.

Categories