Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not work on Arch Linux subsystem in Termux: OSError: Cannot find path to android app folder #62

Closed
nukemiko opened this issue Feb 8, 2022 · 5 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@nukemiko
Copy link

nukemiko commented Feb 8, 2022

Description

Unable to use platformdirs in virtual environment created in Termux's Arch Linux subsystem. It will be raise: OSError: Cannot find path to android app folder.

The Arch Linux subsystem is deployed by install script from SDRausty/TermuxArch.

This problem also breaks the functionality of pip because it embeds this module: pypa/pip#10887

A reddit page reported a similar problem: https://www.reddit.com/r/termux/comments/p6l3yh/i_cant_use_python_virtualenv_on_termux_proot/

Python version

3.10.2

platformdirs version

2.4.1

OS

Android 11, Arch Linux subsystem in Termux

How to Reproduce

  1. Deploy a Arch Linux subsystem environment by install script from SDRausty/TermuxArch. Don't add any parameters. The specific link for install script: https://raw.githubusercontent.com/TermuxArch/TermuxArch/master/setupTermuxArch
  2. Install package python and python-platformdirs in the subsystem. (The latest version of python-platformdirs in the Arch Linux arm repositories: 2.4.1-1, python: 3.10.2-1)
  3. Execute python -m platformdirs or from platformdirs import * in python interpreter. You can see the full screen of error messages.

Output

Shell:

# (Skip the part about deploying the subsystem)

u0_a398@localhost:pts/0->/data/data/com.termux/files/home (0)
> startarch

Welcome to Arch Linux in Termux PRoot!
Install a package: pacman -S package
More  information: pacman -[D|F|Q|R|S|T|U]h
Search   packages: pacman -Ss query
Upgrade  packages: pacman -Syu

Chat:   wiki.termux.com/wiki/Community
GitHub: github.com/archlinuxarm
Help:   help man and info man
IRC:    archlinuxarm.org/about/contact

root09:27~$ pacman -Qi python-platformdirs
名字           : python-platformdirs
版本           : 2.4.1-1
描述           : A small Python module for
                 determining appropriate platform-specific dirs, e.g. a "user
                 data dir"
架构           : any
URL            : https://github.com/platformdirs/platformdirs
软件许可       : MIT
组             : 无
提供           : 无
依赖于         : python
可选依赖       : 无
依赖它         : python-virtualenv
被可选依赖     : 无
与它冲突       : 无
取代           : 无
安装后大小     : 121.81 KiB
打包者         : Arch Linux ARM Build System
                 <builder+seattle@archlinuxarm.org>
编译日期       : 2021年12月28日 星期二 00时30分09秒
安装日期       : 2022年02月07日 星期一 23时25分34秒
安装原因       : 作为其他软件包的依赖关系安装
安装脚本       : 否
验证者         : 数字签名

root09:27~$ python -m platformdirs
-- platformdirs 2.4.1 --
-- app dirs (with optional 'version')
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 85, in _android_folder
    from jnius import autoclass
ModuleNotFoundError: No module named 'jnius'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.10/site-packages/platformdirs/__main__.py", line 46, in <module>
    main()
  File "/usr/lib/python3.10/site-packages/platformdirs/__main__.py", line 27, in main
    print(f"{prop}: {getattr(dirs, prop)}")
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 21, in user_data_dir
    return self._append_app_name_and_version(_android_folder(), "files")
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 97, in _android_folder
    raise OSError("Cannot find path to android app folder")
OSError: Cannot find path to android app folder
root09:27~$ 

Python:

>>> from platformdirs import *
>>> user_data_dir()
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 85, in _android_folder
    from jnius import autoclass
ModuleNotFoundError: No module named 'jnius'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/platformdirs/__init__.py", line 50, in user_data_dir
    return PlatformDirs(appname=appname, appauthor=appauthor, version=version, roaming=roaming).user_data_dir
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 21, in user_data_dir
    return self._append_app_name_and_version(_android_folder(), "files")
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 97, in _android_folder
    raise OSError("Cannot find path to android app folder")
OSError: Cannot find path to android app folder
>>> site_data_
site_data_dir(   site_data_path(
>>> site_data_dir()
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 85, in _android_folder
    from jnius import autoclass
ModuleNotFoundError: No module named 'jnius'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.10/site-packages/platformdirs/__init__.py", line 66, in site_data_dir
    return PlatformDirs(appname=appname, appauthor=appauthor, version=version, multipath=multipath).site_data_dir
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 26, in site_data_dir
    return self.user_data_dir
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 21, in user_data_dir
    return self._append_app_name_and_version(_android_folder(), "files")
  File "/usr/lib/python3.10/site-packages/platformdirs/android.py", line 97, in _android_folder
    raise OSError("Cannot find path to android app folder")
OSError: Cannot find path to android app folder
>>>
@Julian Julian added the bug Something isn't working label Feb 8, 2022
@Julian
Copy link
Member

Julian commented Feb 8, 2022

Thanks. Do you happen to see what the fix should look like here? A PR would certainly be welcome given you've got a device you can try it on.

@gaborbernat gaborbernat added the help wanted Extra attention is needed label Feb 8, 2022
@nukemiko
Copy link
Author

nukemiko commented Feb 9, 2022

Thanks. Do you happen to see what the fix should look like here? A PR would certainly be welcome given you've got a device you can try it on.

I found codes for OS detection in src/platformdirs/__init__.py:

if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system":
    module, name = "platformdirs.android", "Android"

A temporary mitigation for this problem is unset environment valriable ANDROID_ROOT and ANDROID_DATA.

In order to fix this problem, the following judgment conditions may be more appropriate:

if os.getenv("ANDROID_DATA") == "/data" and os.getenv("ANDROID_ROOT") == "/system" and not (os.path.exists("/usr") or os.path.exists("/run")):
    module, name = "platformdirs.android", "Android"

@Julian
Copy link
Member

Julian commented Feb 9, 2022

That sounds odd to me (but I'll have to read the code more carefully).

Termux is Android, so I'd expect a fix to look like making the Android module know how to cope with it, rather than marking it as not-Android. But that's just an initial reaction.

@YariKartoshe4ka
Copy link
Contributor

I can clarify: these variables are written in the init.environ.rc file in the device's skin, so wherever the shell is launched (even from the subsystem), they will be present everywhere. I was counting only on applications, Python IDEs and Termux, but not subsystems.

I think the best solution would be, before choosing android, to check if it is possible to get a folder from _android_folder and, if possible, then choose android, otherwise other OS (windows, macos, linux). I will open PR as soon as possible

@gaborbernat
Copy link
Contributor

Fixed via #63.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants