Closed valentinitnelav closed 1 year ago
I have tried this suggestion: https://stackoverflow.com/a/44592299/5193830, but still doesn't work with Windows:
filename = inspect.getframeinfo(inspect.currentframe()).filename
print('filename: ' + filename)
# filename: start_project.py
path = os.path.dirname(os.path.abspath(filename))
print('path: ' + path)
# path: C:\Users\vs66tavy\Downloads
This suggestion also doesn't work as expected (https://stackoverflow.com/a/18489147/5193830):
print('lambda trial ' + os.path.abspath(inspect.getsourcefile(lambda:0)))
# lambda trial C:\Users\vs66tavy\Downloads\start_project.py
Also this suggestion didn't work (https://stackoverflow.com/a/2632297/5193830):
os.path.dirname(str(__file__, encoding))
os.path.dirname(str(sys.executable, encoding))
I used str
instead of unicode
as suggested here https://stackoverflow.com/questions/2632199/how-do-i-get-the-path-of-the-current-executed-file-in-python#comment116879531_2632297
unicode
didn't work as well and it gave this error:
Traceback (most recent call last):
File "start_project.py", line 101, in <module>
print('Test 1: ' + os.path.dirname(str(__file__, encoding)))
TypeError: decoding str is not supported
os.path.dirname(sys.executable) # returns
# C:\Python38
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
# C:\Users\vs66tavy\Downloads
os.getcwd()
# C:\Users\vs66tavy\Downloads
Using sys.path[0]
might work both for Linux & Windows.
sys.path[0]
returns C:\Users\vs66tavy\Documents\img-with-box-from-excel\src
, so the path to the src
folder where boxcel\start_project.py
is located (the calling script).
I can work with that.
Thanks to @RRemelgado for pointing out that sys.path[0]
can do the trick :)
If this becomes again an issue (possibly when addressing #9 and using argparse ), the check also this option https://stackoverflow.com/a/51724506/5193830:
display_images_py_file = pkgutil.get_data(__package__, "src/display_images.py")
It looks like
__file__
&os.path.realpath(__file__)
returns different things depending on the OS (Windows vs Linux).On Windows:
On Linux:
On Windows,
os.path.realpath(__file__)
returns the path to the Excel file that is used as the argument forstart_project.py
and not the path to the executed script. On Linux, this behaves as expected - it returns the path to the executed script and not to the Excel file. No error is therefore given under Linux and the tool works as expected.Python versions:
Need to fix this.