79314438

Date: 2024-12-28 19:47:18
Score: 1
Natty:
Report link

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:

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)

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.

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

- PADS
    - my-script.py
    - __init__.py
    - Automata.py
    - Util.py
    - ...
Reasons:
  • Blacklisted phrase (1): this link
  • Long answer (-1):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: Samthink