valentinitnelav / img-with-box-from-excel

boxcel: Integrate Excel with Python for visualizing images with their corresponding bounding boxes for object detection annotation workflows
BSD 3-Clause "New" or "Revised" License
3 stars 0 forks source link

Linux vs Windows differences for __file__ (path to current/calling script file) #11

Closed valentinitnelav closed 1 year ago

valentinitnelav commented 1 year ago

It looks like __file__ & os.path.realpath(__file__) returns different things depending on the OS (Windows vs Linux).

On Windows:

cd  C:\Users\vs66tavy\Documents\img-with-box-from-excel\src\boxcel
python start_project.py C:\Users\vs66tavy\Downloads\hymenoptera_sample.xlsx 
# Gives an error like
Traceback (most recent call last):
  File "start_project.py", line 94, in <module>
    with open(display_images_py_file,'r') as firstfile, open(target_py_file,'w') as secondfile:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\vs66tavy\\Downloads\\display_images.py'

print('path to current file using __file__ is: ' + __file__)
# path to current file using __file__ is: 
start_project.py

print('path to current file using os.path.realpath(__file__) is: ' + os.path.realpath(__file__))
# path to current file using os.path.realpath(__file__) is: 
C:\Users\vs66tavy\Downloads\start_project.py

On Linux:

cd '/home/vs66tavy/iDiv Dropbox/Valentin Stefan/GitHub/img-with-box-from-excel/src/boxcel'
python3 start_project.py '/home/vs66tavy/iDiv Dropbox/Valentin Stefan/GitHub/img-with-box-from-excel/sandbox/hymenoptera_sample.xlsx'

print('path to current file using __file__ is: ' + __file__)
# path to current file using __file__ is: 
/home/vs66tavy/iDiv Dropbox/Valentin Stefan/GitHub/img-with-box-from-excel/src/boxcel/start_project.py

print('path to current file using os.path.realpath(__file__) is: ' + os.path.realpath(__file__))
# path to current file using os.path.realpath(__file__) is: 
/home/vs66tavy/iDiv Dropbox/Valentin Stefan/GitHub/img-with-box-from-excel/src/boxcel/start_project.py

On Windows, os.path.realpath(__file__) returns the path to the Excel file that is used as the argument for start_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:

python # On Windows it returns this:
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

python # On Ubuntu it returns this:
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3

python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Need to fix this.

valentinitnelav commented 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
valentinitnelav commented 1 year ago

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")