0

Let's say I have a script I've written:

~/workspace/myscript/script.py

If I have, for example, a ~/bin which I have added to my $PATH, then I could create a symbolic link

~/bin/script -> ~/workspace/myscript/script.py

And everything works fine, I can call my script from anywhere.

Then, say my script starts to grow, and I separate it out

~/workspace/myscript/
    script.py
    mylib.py

I now run into a problem, as described here, that if I am calling my python script directly (as opposed to importing it as a module) then I cannot do a relative import.

The only solution I have seen is to package up the whole program into a fully fledged python package with a setup.py and installing it system-wide (or managing a home directory python library folder).

This seems like a lot of extra work for the sake of breaking my code into multiple python files.

Is there some way I can:

  1. Call the script from anywhere (have it callable on path),
  2. Have the code sparated into multiple files,
  3. Not have to manage a full python package and installation.

All at once?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Alex
  • 1,306
  • 3
  • 15
  • 25

1 Answers1

1

You can add the root directory of your module to the Python path:

export PYTHONPATH="$PYTHONPATH:~/workspace/myscript/"
Cory Nezin
  • 1,551
  • 10
  • 22