0

I need to add a directory to my sys.path/PYTHONPATH which contains my custom Python modules. There are a couple of other questions on here on the same topic with answers that have resolved the issue for other users.

I have manually appended to the sys.path object the new file path, and that has successfully added the module directory, but only for the current Python shell, and after a reboot the module path is lost.

Another common answer has been to alter the PYTHONPATH environment variable with a call like:

PYTHONPATH="$PYTHONPATH:/path/to/modules"

I am having no luck with this approach and I cannot even identify the PYTHONPATH variable or understand where it lives. The call sudo echo $PYTHONPATH does not return anything and PYTHONPATH does not show up when I call env. There is no reference to PYTHONPATH in the .bashrc or .profile files in my ~ directory.

I am new to Linux and getting the hang of using the terminal. Any help to point me in the right direction to getting the path to my modules permanently added will be greatly appreciated!

UPDATE: I have tried adding the line PYTHONPATH=/path/to/modules to my .bashrc file in my user directory, but this does not resolve the issues and furthermore the PYTHONPATH variable is still not showing up in terminal when I call env.

UPDATE 2: Adding the following lines to .bashrc has succeded in getting PYTHONPATH to show up in terminal as an environment variable:

PYTHONPATH=path/to/modules
export PYTHONPATH

and now in Python sys.path shows the new path/to/modules and the modules are loading as expected.

However, while this works to solve my specific problem, I'm still not sure quite how all the pieces are working, such as why the new PYTHONPATH variable is not showing the remaining default paths that show up in sys.path.

Tom
  • 126
  • 1
  • 6
  • Add `PYTHONPATH=/path/to/modules` to your `.bashrc`. (You don't need `$PYTHONPATH:` inside the string, because that is intended to preserve a pre-existing value, which you don't have.) – John Gordon Jan 04 '21 at 16:03
  • I have tried this but it is not working. The $PYTHONPATH variable is now showing up in terminal with the one path, but the sys.path directories are unchanged, and I still cannot import my modules. It feels like the python is referring to a differint PYTHONPATH somewhere? – Tom Jan 04 '21 at 16:25
  • How do you run python? Do you type `python myscript.py` from the shell, or do you use an IDE, or some other way? – John Gordon Jan 04 '21 at 16:33
  • I am running from the shell - using python to run the script as you said, and also running the python interactive interpreter, both have the same results regarding sys.path. – Tom Jan 04 '21 at 18:46
  • Then I don't see how `sys.path` wouldn't have the changes you made in bashrc. Are you running python from a shell that was started _after_ you made the changes to bashrc? – John Gordon Jan 04 '21 at 19:32
  • Update the question to include the PYTHONPATH variable that you added to bashrc, and also the output of `print(sys.path)` from inside the python interpreter. – John Gordon Jan 04 '21 at 19:33
  • Yes I am rebooting the system after editing the .bashrc file but still the PYTHONPATH variable is not showing up. Could there be another .bashrc file somewhere that is superseding the one in my user folder? – Tom Jan 04 '21 at 19:54
  • Are you sure bash is the shell you're using? – John Gordon Jan 04 '21 at 19:59
  • There are quite a few different ways to invoke a **"terminal"**, not all of which will use `~/.bashrc`. If you manually (within the terminal) `source ~/.bashrc`, does it work? Within `~/.bashrc`, do you `export PYTHONPATH="$PYTHONPATH:/path/to/modules"` or are you just doing `PYTHONPATH="$PYTHONPATH:/path/to/modules"`? – tink Jan 04 '21 at 20:01
  • @tink your suggestion to use the export command fixed it, but I'm still not sure of the relationship between PYTHONPATH and sys.path. – Tom Jan 04 '21 at 20:09
  • Does [this](https://stackoverflow.com/questions/4271494/what-sets-up-sys-path-with-python-and-when) shed some light? – tink Jan 04 '21 at 20:12
  • I don't understand why people insist on hacking `PYTHONPATH` and/or `sys.path`. There is almost never a good reason to do so. -- Maybe read this answer for some better ideas: https://stackoverflow.com/a/50193944/11138259 – sinoroc Jan 04 '21 at 23:19

0 Answers0