Pythonista-Issues icon indicating copy to clipboard operation
Pythonista-Issues copied to clipboard

How to setup PYTHONPATH globally by default?

Open o-murphy opened this issue 4 months ago • 5 comments

o-murphy avatar Aug 15 '25 07:08 o-murphy

sys.path is PYTHONPATH plus the documents directory.

sys.path.append() works as documented, but does not go back and modify the PYTHONPATH environment variable.

  • https://docs.python.org/3/tutorial/modules.html#standard-modules
  • https://docs.python.org/3/library/sys.html#sys.path
import os
import sys

python_path = set(os.getenv("PYTHONPATH").split(":"))
system_path = set(sys.path)

print(f"{len(python_path) = } but {len(system_path) = }")
print(f"{system_path ^ python_path = }")

sys.path.append("my/special/place")  # <-- this works but does not modify the `PYTHONPATH` environment variable.

python_path = set(os.getenv("PYTHONPATH").split(":"))
system_path = set(sys.path)

print(f"{len(python_path) = } but {len(system_path) = }")
print(f"{system_path ^ python_path = }")

cclauss avatar Aug 15 '25 09:08 cclauss

sys.path is PYTHONPATH plus the documents directory.

sys.path.append() works as documented, but does not go back and modify the PYTHONPATH environment variable.

  • https://docs.python.org/3/tutorial/modules.html#standard-modules
  • https://docs.python.org/3/library/sys.html#sys.path

import os import sys

python_path = set(os.getenv("PYTHONPATH").split(":")) system_path = set(sys.path)

print(f"{len(python_path) = } but {len(system_path) = }") print(f"{system_path ^ python_path = }")

sys.path.append("my/special/place") # <-- this works but does not modify the PYTHONPATH environment variable.

python_path = set(os.getenv("PYTHONPATH").split(":")) system_path = set(sys.path)

print(f"{len(python_path) = } but {len(system_path) = }") print(f"{system_path ^ python_path = }")

The problem it's can't be configured globally (forever) for pythonista and cause of it we have few problems with pip

  • using pip install -U --target ~/Documents/site-packages will overwrite ~/Documents/site-pachages/bin
  • using pip install -U --prefix ~/Documents/site-packages will create common tree like lib/pythonX.Y/site-packages which one is not in the PYTHONPATH, so we should manually move packages from ~/Documents/site-packages/lib/pythonX.Y/site-packages to ~/Documents/site-packages, and in this case the on uninstall the entry points would not be removed from ~/Documents/site-pachages/bin
  • There no clear way to use PEP-370 in Pythonista

o-murphy avatar Aug 15 '25 09:08 o-murphy

You can setup global modifications to sys.path in pythonista_startup.py in Pythonista 3 or using the "Startup" setting in Lab.

omz avatar Aug 15 '25 10:08 omz

You can setup global modifications to sys.path in pythonista_startup.py in Pythonista 3 or using the "Startup" setting in Lab.

Aha, but it is not works as expected. For example StaSh can't see path changes made by pythonista_startup.py

o-murphy avatar Aug 15 '25 10:08 o-murphy

I mean the scripts that runs via runpy can't see changes made by pythonista_startup.py

o-murphy avatar Aug 15 '25 10:08 o-murphy