You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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'python3start_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/iDivDropbox/ValentinStefan/GitHub/img-with-box-from-excel/src/boxcel/start_project.pyprint('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/iDivDropbox/ValentinStefan/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.
The text was updated successfully, but these errors were encountered:
valentinitnelav
changed the title
Linux vs Windows current script file differes
Linux vs Windows differences for __file__ (path to current/calling script file)
Dec 13, 2022
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 :)
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.
The text was updated successfully, but these errors were encountered: