Make pyinstaller executable find its own name? - python

How do I make a executable compiled with pyinstaller find its own name? I need this to make it copy itself to startup. Or is there another way to achieve this?
file only finds the original name of the script, not the executable.

Hope this solves your problem
import os
import sys
# uncompiled file.py can be found by
file_name = os.path.basename(__file__)
# compiled file.exe can find itself by
file_name = os.path.basename(sys.executable)
# and path to py or exe
current_path = os.path.realpath(sys.executable)
current_path = os.path.realpath(__file__)

Related

python change default directory

so I recently tried using playsound and the .mp3 is in the same folder as the .py, and it says it could not find the file.
I printed out the current directory and it says it's at C:\Users\me
Shouldn't this be at the same directory as where the .py script is at? This has been happening with my other python scripts where I have to explicitly give it the directory, whereas before I didn't have to and it was just where the python script was at.
Is there a setting for this?
The current directory is defined by where you execute the python file not the location of it. If you want to change the directory you can use the os module:
import os
os.chdir(PATH)
If you're looking for a file in the same directory as the executing python program:
basename = os.path.basename(__file__)
my_file_name = basename + "/myfile"
with open(my_file_name) as my_file:
...
The variable __file__ contains the full pathname of the currently executing Python program.

os.getcwd() can't find current working directory

Whenever I try to get current working directory in python on windows with os.getcwd() it shows only the root directory.
for example this directory structure:
root\
ㄴbase\
ㄴㄴfile\
ㄴㄴㄴfile.py\
I use
# files.py
os.getcwd()
output will be:
\---> c:/root\
I want to get c:/root/file
any idea?
thanks in advance
+
actually os.getcwd() finds the 'root:\base\' at some point. but in other cases it doesn't find. so I wanted to know the difference.
Current working directory isn't the path of the script, it's where you are running the script from.
And apparently, you're running your script from c:/root/ directory. If you want the directory of the script use this to get the script path
from os.path import dirname, abspath
script_path = abspath(dirname(__file__))
or run the script from c:/root/base/file to get the path with getcwd().

OS .CWD Skips One DIR how to fix

I am trying to get directory from where script is ran, but os.cwd() skips one dir
Script Path - D:\Mainfolder\Testfolder\testfoldr2\check1.py
But
Output is - D:\Mainfolder\Testfolder
Why no testfolder2 is coming though the script is stored there kindly suggest how to get testfolder2 as working dir
I am Using for checking
import os
print(os.getcwd())
getcwd() will give you your working directory from where you are running the script,
if you want folder of the file then you can use below code:
import os
path = os.path.dirname(__file__)
print(path)

How do I tell Pyinstaller to use an .EXE thats in the Scripts folder?

for example if I
pip install ffmpeg-python
and run
os.system('ffmpeg -version')
it will print ffmpeg's version.
but if I save this line to main.py and pyinstaller --onefile main.py and run it, Windows can't find ffmpeg.
How do I tell pyinstaller to use the ffmpeg.exe thats in the Scripts folder?
Edit: I figured it out, answered below
I had a similar problem. I solved it by defining full path of local directory. After that I added this path to my EXE files in the current directory and run them. So try this:
from os import path
this_script_dir = path.dirname(path.realpath(__file__))
ffmpeg_path = this_script_dir + '\\ffmpeg.exe'
Ok this is how i did it:
#test.py
import sys
import os
if getattr(sys, 'frozen', False):
basedir = sys._MEIPASS
else:
basedir = os.path.dirname(os.path.abspath(file))
basedir = str(basedir)
os.system(basedir+'/ffmpeg.exe -version')
#works without the .exe also
and for pyinstaller you write
pyinstaller test.py --add-data "ffmpeg.exe;." --onefile
ffmpeg.exe is in the same folder as the test.py

How do I find directory of the Python running script from inside the script?

How do I find the directory of the running Python script from inside Python [3.3]?
I have tried what was suggested at: How can I find script's directory? , but I get "Invalid syntax" and directed to "os" (And I did import os).
The closest I have got to the answer is: sys.argv[0], but that still includes the file name, so I cannot use it. Is there any other way?
The part where it says rundir = sys.argv[0] is where the suggested code will go:
import os, sys
rundir = sys.argv[0]
print("Running from" + rundir)
To get the directory that contains the module you are running:
import os
path = os.path.dirname(os.path.realpath(__file__))
Or if you want the directory from which the script was invoked:
import os
path = os.getcwd()
From the docs:
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file.
Depending on how the script is called, this may be a relative path from os.getcwd(), so os.path.realpath(__file__) will convert this to an absolute path (or do nothing is the __file__ is already an absolute path). os.path.dirname() will then return the full directory by stripping off the filename.
Try this:
import os
os.path.dirname(__file__)
__file__ gets the name of the file you are in. The dirname function gets the directory that the file is in.
The syntax error probably had to do with the print statement. In python 3.x
print "hi"
is invalid. Print is now a function.
print("hi")
works. You need the parentheses.
This should work:
import os,sys
print(os.path.dirname(os.path.realpath(sys.argv[0])))

Categories