Python os.path.exists returns False

Hi,

I’m trying to connect music21, a Python “toolkit for computer-aided musicology”, to Musescore. To do so, the music21.environment.UserSettings() function has a setting to define a path to Musescore, which checks if the file exists with os.path.exists(value). In Fedora, the rpm installs Musescore to /usr/bin/mscore, but os.path.exists("/usr/bin/mscore") returns False, even though the file exists.

In the documentation of os.path.exists, it is specified that

On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists. - os.path — Common pathname manipulations — Python 3.10.4 documentation

I’m guessing that Fedora doesn’t grant me the permission to execute os.stat() on files in /usr/bin.

Solution?

What are the ownership & permissions on /usr/bin/mscore?

By default, Fedora installed Musescore as

[essi@fedora ~]$ ls -l /usr/bin/mscore
-rwxr-xr-x. 1 root root 27206824 22 mar 04:01 /usr/bin/mscore

I just checked on my system with system installed files and python allows my regular user to use os.stats() and os.path.exists() with no problems.

It is possible that the selinux permissions are interfering as well so check that for the files of concern (ls -lZ)

I get this.

[essi@fedora ~]$ ls -lZ /usr/bin/mscore
-rwxr-xr-x. 1 root root system_u:object_r:bin_t:s0 27206824 22 mar 04:01 /usr/bin/mscore

That part looks right.
On my fedora 36 machine the python functions you gave work properly so more info, including exactly what is seen when starting the app, (full details) is needed.
To confirm it is the function returning false or something else try what I did.

[jvian@laptop ~]$ python -V
Python 3.10.4
[jvian@laptop ~]$ python
Python 3.10.4 (main, Mar 25 2022, 00:00:00) [GCC 12.0.1 20220308 (Red Hat 12.0.1-0)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print( os.path.exists("/usr/bin/head"))
True
>>> print( os.stat("/usr/bin/head"))
os.stat_result(st_mode=33261, st_ino=684414, st_dev=64768, st_nlink=1, st_uid=0, st_gid=0, st_size=45912, st_atime=1650894271, st_mtime=1647850427, st_ctime=1650894246)

This clearly shows that my regular user is able to read the stats and that python sees the file.

For trouble shooting you need more detail than has been provided about the messages you are receiving.

Interesting. I tried with the system python, the Anaconda python distribution, conda environments, python, ipython and Jupyter lab. They all… return True to os.path.exists("/usr/bin/mscore")! The only way I obtain False is with the Jupyter implementation of VSCode, so I guess ths is an issue on their side. Problem solved! Thanks a lot