1

I have David Eppstein's PADS library of Python Algorithms and Data Structures in a subdirectory next to my Python script:

- my-script.py
- PADS
    - __init__.py
    - Automata.py
    - Util.py
    - ...

How can I use the RegExp class in my script? It's defined in PADS/Automata.py but when I use import PADS.Automata, I get an error from a line inside that file:

  File ".../PADS/Automata.py", line 9, in <module>
    from Util import arbitrary_item
ModuleNotFoundError: No module named 'Util'

Edit: Do I need to change the line inside Automata.py to from .Util import arbitrary_item? That seems to work, but then I can't run python Automata.py from the command line. Also, I'd rather avoid modifying the files within PADS if possible.

1 Answer 1

1

When you import something in Python, it automatically searches the module on a variable called as PATH. More exactly in python we use the PYTHONPATH enviroment variable.

You can add the directory you are having problems with easily by following one of these 3 methods:

  • 1 (Fastest): Append the directory to the PYTHONPATH before importing the module.
import sys
import os

sys.path.insert(0, os.path.abspath("./PADS"))

import PADS.Automata

Note that this method only works if the path is modified before any imports to the module are done. (Not so scalable for other use cases)

  • 2 (My preferred): Add the directory to the PYTHONPATH before the execution of the program.

For doing this build a sh script (or ps1 if you are in windows) that updates the PYTHONPATH and run it once for each terminal. The script would be something like the following:

#!/bin/sh
export PYTHONPATH=$PYTHONPATH:'path_to_the_package'

# Example: $PYTHONPATH:'/home/user/PADS'

From now on python will search automatically in the PADS directory and you won't have to update any python file. Even better if you want to automatize this method only append this script to the /home/user/.bashrc file (that will be executed on all terminals).

Pros: Easy, Non-invasive and automatizable method for your packages. Do it once and always work.

  • 3 (Scalable): Convert the code of your friend into a package.

This method is the scalable one. Just update all the imports of the package of your friend as follows:

from Util import arbitrary_item  # bad
from PADS.Util import arbitrary_item  # good :D

If you want to feel even more professional then add some __init__.py files to link all the dependencies and generate a setup.py file so you can install the package of your friend with pip install and never depend on its dependencies again :D.

Although this method is slow, it is better for production packages/libraries. There is a tutorial in this link

  • 4 (Happy Solution): If you only want to make it work once then move your python file to the same directory. But some bugs may occur if you try to link dependencies with each other. The resultant tree would be something like the following:
- PADS
    - my-script.py
    - __init__.py
    - Automata.py
    - Util.py
    - ...
Sign up to request clarification or add additional context in comments.

Comments

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.