0

Problem

I've been trying to run a python script which imports from the Foundation package:

from Foundation import ...

Whenever I try to run this I get the following error: Terminal output

Things I've done:

I've installed the Foundation package and verified that it was installed in /usr/local/lib/python3.7/site-packages

I've added export PYTHONPATH='/usr/local/lib/python3.7/site-packages' to my .zshrc file.

When I go into interactive mode, sys.path includes /usr/local/lib/python3.7/site-packages and I can successfully import Foundation:

enter image description here

When I run the script using /usr/local/bin/python3.7, sys.path does not include /usr/local/lib/python3.7/site-packages and importing Foundation fails:

enter image description here

Does anyone know why this might be happening and/or how to fix this? Why does running this script have a different sys.path than running the same python executable in interactive mode?

(I know I could use sys.path.extend(desired path) or something like that but that's not an ideal solution)

2
  • 1
    You didn't mention that you're running it with sudo. That gets you a different .zshrc; you don't inherit the environment. Commented Nov 18, 2022 at 23:54
  • 1
    You're running the script using sudo. It strips out most environment variables. Commented Nov 18, 2022 at 23:54

1 Answer 1

1

First, you run Python as your user (say ~joe or whatever your UID is) but then you bring sudo to the table. And that's where things starts to differ, because it will not inherit your environment. Simple test for you to replay (substitute python3 by whatever path/version you want):

$ python3
>>> import sys
>>> sys.path
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', 
'/usr/lib/python3.10/lib-dynload', '/usr/lib/python3/dist-packages', 
'/usr/local/lib/python3.10/dist-packages', 
'/home/<USER>/.local/lib/python3.10/site-packages']

then the same but with sudo:

$ sudo python3
>>> import sys
>>> sys.path
['', '/usr/lib/python310.zip', '/usr/lib/python3.10', 
'/usr/lib/python3.10/lib-dynload', '/usr/lib/python3/dist-packages', 
'/usr/local/lib/python3.10/dist-packages']

To work this around you either need to ensure your superuser's environment fits your needs or you feed python interpreter with needed value of PYTHONPATH on the fly:

$ sudo PYTHONPATH=/FOO/BAR python3
>>> import sys
>>> sys.path
['', '/FOO/BAR', '/usr/lib/python310.zip', '/usr/lib/python3.10', 
'/usr/lib/python3.10/lib-dynload', '/usr/lib/python3/dist-packages', 
'/usr/local/lib/python3.10/dist-packages']
Sign up to request clarification or add additional context in comments.

1 Comment

This had me stuck for so long. Thanks for the help! I had no idea that sudo circumvented the .zshrc file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.