How Do I Permanently Append To Python's sys.path

3.8k views Asked by At

I'm trying to import some of my own modules on my mac and I can't figure out how to add the path to my modules. I have tried editing the PYTHONPATH environment variable, (export PYTHONPATH="${PYTHONPATH}:/path/to/modules) appending the path to sys.path, etc. Nothing seems to work for me. I want the path to be in sys.path permanently so that I don't need to keep putting import sys then sys.path.append("/path/to/modules") at the top of every document that I make. Is there a file somewhere that contains the path that I could edit? Or do I need to enter a command into terminal or something? Any help is appreciated.

1

There are 1 answers

3
pbuck On

If you want to "install" modules, yes, create a site package. If you just doing something locally, for fun / education on you own computer, modify PYTHONPATH.

On a Mac, you're most likely using bash as your shell.

PYTHONPATH=/path/to/modules:${PYTHONPATH}
export PYTHONPATH

...will do what you want in your current shell. To make it permanent (for you), add both lines to your local .bashrc file, under your home directory.

If you want to make it permanent for all users of this computer.. don't edit the systemwide /etc/profile -- that's when you really should install it correctly using packages.