Why isn't my python turned exe, not working? - python

So I have this piece of code here:
import os
import sys
onlyfiles = [f for f in os.listdir(sys.path[0]) if os.path.isfile(os.path.join(sys.path[0], f))]
while True:
print(onlyfiles)
x = input()
if x == "y":
continue
else:
break
I converted the script into a .exe file with pyinstaller. But when I try to run it, I assume it crashed?
Can anyone help me with this?

your exe is crashing due to 'NotADirectory' error being thrown by the program.
As you are on windows, you can see this error.
Navigate to the path where you have created you exe and launch cmd.exe, and then execute your exe by issuing 'filename.exe' .
The 'NotADirectory' error is coming as now exe is frozen and system paths are changed as well, which include 'base_library.zip' . Certainly this is not a directory and hence exception is expected.

Related

pyinstaller exe fails - fusionscript.dll problem

[Thanks to Charles Duffy for the gentle nudging toward a clearer question! I am new to Stack Overflow and appreciate the help. After more research, it appears that the dependent code is actually within Blackmagic's fusionscript.dll code (If the .dll is missing, my exe is able to continue past the line of my code which tries to import) , so I don't see any way to get around it until they update.]
Original question:
I'm stuck trying to get a pyinstaller executable to work on a Windows 10 test system. My exe works fine on a system that has Python 3.6.8 installed.
Complete code is below. The print command that starts with "trying:" succeeds and shows the correct path for the import. The print command immediately after the import command print("Made it past import") is not executed, and the exe quits without showing any errors.
I've determined that there's just one file that is necessary for the exe to run on the test system, and that's the os.py file in the Lib folder of the Python install. Without that file, the exe just quits (without any error message) when it hits the line of code: script_module = imp.load_dynamic("fusionscript", path + "fusionscript" + ext)
For anyone trying to reproduce: the exe runs when a Python 3.6.8 install is present, or more specifically, when os.py is. I made the installer with a simple pyinstaller importTest.py command. I place the fusionscipt.dll in the folder with the resulting executable. If os.py is present, the exe runs normally and prints all of it's print commands. If you remove os.py from the Python Lib directory, this is no longer the case, and the exe quits when it hits the imp command.
NOTE: I know that imp is deprecated, but the API that my code engages with (fusionscript.dll for Resolve) only works with Python 3.6, so I can't use anything newer.
Here's the code:
from PyQt5.QtCore import QObject
import sys
import imp
import os
CURR_DIR = os.getcwd()
sys.path.append(CURR_DIR)
script_module = None
ext = ".dll"
path = CURR_DIR+"\\"
try:
print("trying: " + path + "fusionscript" + ext)
script_module = imp.load_dynamic("fusionscript", path + "fusionscript" + ext)
print("Made it past import")
except ImportError:
pass
print("script module = ",script_module)

Why python exe is not working for listdir functionality

I created below simple program. It works if i run normally on sublime but when i create a exe using pyinstaller it does not work. It does not give any error it just shows program name and then terminates.
import os
from time import sleep
files = os.listdir('C:')
for file in files:
print(file)
sleep(5)

How do I open exe file with python that is dependent on AVI files?

I'm trying to open Adv5KTCP.exe (path shown in code) but this exe actually opens three more avi.files as shown in image below. This causes the error. I've tried os, subprocess & pywinauto to call but to no avail. I've also tried adding the files path to environmental variable but I think that makes no sense. Opening the exe from command prompt does not work too. However, the exe can be opened manually by double clicking the exe file (like the usual). I just need it to be automated.
AVI Files:
The Error:
Double clicking the exe would open this window:
Here is my code:
import os
import sys, logging
import subprocess
import ctypes
from pywinauto import Application
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
exe = "Adv5KTCP.exe" #path set in environment variable: "C:\Program Files (x86)\Advantech\ADAM-5000TCP-6000 Utility\Program"
os.startfile(exe)
# subprocess.Popen([exe])
# application = Application(backend="uia").start(exe)
else:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
Does anybody have any idea on how I could go about this? Thank you in advance for your response.
New Findings:
I tried to cut the three AVI files to desktop & set desktop path to system variable & the exe gives the same error. However when i cut the exe file to desktop with the avi files as shown below, it works! Even when the other required files are not in desktop but path's set to system variable. Which means the exe is somehow registering the paths of the avi upon click, location/position or some sort which I'm not sure of.

Pyinstaller: Executable file opened with built in function in python closes after 1 sec when i make .exe file with pyinstaller

I make program in python to open some executable file given by path. I use:
os.startfile(path)
to start program. When i run script.py in IDLE it works fine, but when i make .exe file with pyinstaller when i run it that program i want to open starts to open and closes almost immediately. I tried with different functions like subprocess.Popen(path) and it does the same thing, open and close program after 1 second. Can someone help me? Is there a problem in python functions or in pyinstaller or even windows 10?
The problem is that your code most likely just runs and then the window closes.
You can add import time to your imports and time.sleep(x) or something to keep the window open for x seconds after you run
Read all the way through before doing anything!
Ok so try this:
Put this in a python file called start.py:
import os
import subprocess
#py_command - whatever opens python in cmd
#path - must be full path of file you want to call
def StartPythonScript(path, py_command):
command = 'start cmd /k ' + py_command + " " + path
subprocess.run(command, shell=True)
Now, take the code from file you are calling from the exe, let's say its name is run.py, and put it in a different file, say code.py.
Make sure start.py is in the same folder as run.py
In run.py, put this:
import start
cmd = "py" #change this to whatever opens python in cmd
path = "code.py" #change to the full path of code.py
start.StartPythonScript(path, cmd)
So when you click on the exe, it opens run.py which tells start.py to open code.py, which contains your program.
You can actually merge start.py and run.py, but you can reuse start.py if they are seperate.
OR...
Add import os to your program that is called by the exe.
Add os.system("pause") to the end of your program.
I'm not sure if this will work on an infinitely running program...but try this first to save time.
More info:
How to stop Python closing immediately when executed in Microsoft Windows
Good luck!

Python command for setting working directory to source file location in Spyder

I want a python line of code that sets the working directory to folder the code is part of. I am using spyder IDE for writing and running python codes.
Side note: This question is very similar to R command for setting working directory to source file location in Rstudio
This is a common problem I run into when developing in Jupyter for the command line.
You can try this to find where your script is executing from:
import os
from pathlib import Path
def myPath():
'''
return the current working directory in both interpeters and when exectued on the commandline
'''
try:
# path of this file when executed
wd = os.path.dirname(os.path.abspath(__file__))
except NameError as e:
print('this script is running in an interpreter')
# if not found
wd = Path().resolve()
return(wd)

Categories