-2
  1. How do I import from b1.py into b2.py
  2. How do I import from sa1 into b2?
test/                      # root folder
    packA/                 
        subA/              
            __init__.py
            sa1.py
            sa2.py
        __init__.py
        a1.py
        a2.py
    packB/                 
        b1.py
        b2.py
    math.py
    random.py
    other.py
    start.py

EDIT solved it by putting:

import os
import sys
sys.path.append(os.getcwd())

where os.getcwd() is the project's root directory.

  • 2
    Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – dpwr Dec 12 '20 at 20:19

1 Answers1

1
  1. from packB import b1
  2. from packA.subA import sa1
David M.
  • 4,198
  • 2
  • 16
  • 21
  • that's what I tried, David, but I get "no module defined: packB" for the first case. – Egor Isakson Dec 12 '20 at 22:26
  • `packB` must be in your PYTHONPATH (e.g. export PYTHONPATH="$PYTHONPATH:/test/packB"). Is that the case? – David M. Dec 12 '20 at 22:40
  • I haven't dealt with PYTHONPATH perhaps that is the problem. I will look it up. – Egor Isakson Dec 12 '20 at 22:49
  • It looks like i solved the issue by appending sys.path. However, either way PYTHONPATH or sys.path are both kind of clunky, especially given that they are in one project. Do you know if that's the regular approach? – Egor Isakson Dec 12 '20 at 23:11
  • There are alternatives; you can find more information [here](https://stackoverflow.com/a/55656738) and [here](https://stackoverflow.com/a/1893622). – David M. Dec 13 '20 at 00:21