0

So I have this simple program, job_admin_cli.py:

import pyhocon

import sys
print(sys.path)

When I execute it, I get the following result

myapptest-staging-dark-3 33.5.0-jscherman /opt/myapptest/staging/myapptest $ sudo python3 job_admin_cli.py
['/opt/myapptest/staging/myapptest', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
Traceback (most recent call last):
  File "/opt/myapptest/staging/myapptest/job_admin_cli.py", line 1, in <module>
    import pyhocon
ModuleNotFoundError: No module named 'pyhocon'

On the other side, when doing exactly the same from the console,

myapptest-staging-dark-3 33.5.0-jscherman /opt/myapptest/staging/myapptest $ python3
Python 3.6.10 |Anaconda, Inc.| (default, Jan  7 2020, 21:14:29)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyhocon
>>>
>>> import sys
>>> print(sys.path)
['', '/opt/myapptest/staging', '/opt/myapptest/staging/miniconda3/envs/myapptest/lib/python36.zip', '/opt/myapptest/staging/miniconda3/envs/myapptest/lib/python3.6', '/opt/myapptest/staging/miniconda3/envs/myapptest/lib/python3.6/lib-dynload', '/opt/myapptest/staging/miniconda3/envs/myapptest/lib/python3.6/site-packages']

Note that, not only it can import now the pyhocon module, but it also prints different paths in sys.path.

Why is this happening?

jscherman
  • 5,389
  • 12
  • 42
  • 84
  • Are you using a virtual environment? – NotNik. Mar 11 '20 at 01:38
  • 4
    You ran `python` under two different user accounts between the two examples - using `sudo` switches the first example to the `root` user. Try running `sudo python3` instead for the interactive console and redo your example. – metatoaster Mar 11 '20 at 01:43
  • @metatoaster you are right! why so? why running with sudo does not use the same paths? – jscherman Mar 11 '20 at 01:58
  • It may be that the `python` binary being resolved are different (compare `which python` vs `sudo which python`), or that certain [environmental variables not being passed from your user account via `sudo`](https://stackoverflow.com/questions/27466862/why-is-the-pythonpath-different-when-running-python-with-and-without-sudo). – metatoaster Mar 11 '20 at 02:04
  • Alternatively, since you are using Anaconda on your user account, you may wish to look at [How to run Anaconda Python on sudo](https://stackoverflow.com/questions/36659448/how-to-run-anaconda-python-on-sudo). – metatoaster Mar 11 '20 at 02:05

0 Answers0