0

Given the following structure in my python3 project:

.
├── utils
│   └── someutil
│       └── Stuff.py
└── tools
    └── sometool
        └── Main.py

Where the content of tools.sometool.Main.py looks like this:

import XXX utils.someutil.Stuff XXX

Where XXX means "I am clueless please help me with this import statement". Further In the structure above I want to enter the commandline:

python3 tools/sometool/Main.py

and have it execute without failing on the import. What is the best and cleanest way to do this in Python 3?

I have experimented with putting __ini__.py files with lots of statements like

sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), '../../')))

And the like. Is there really no better way?

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95
  • `sys.path.append()` is what I follow and it has worked for me till now. I'm not sure why you're facing an issue. – Irfanuddin Sep 01 '19 at 16:23
  • Where is the `__init__.py` file? – Sraw Sep 01 '19 at 16:33
  • @Sraw: I have tried many combinations, but really in utills, someutl tools and sometools. Also I ajusted the ../.. to ../ depending on the level. – Mr. Developerdude Sep 01 '19 at 16:45
  • @LennartRolland I don't think any of them will work. As when you use `python3 tools/sometool/Main.py` to execute, the only loaded file before importing is `Main.py` itself. You will need to add that into `Main.py` or create something like `bin/helper.py` to config the path. And surely, run `python3 bin/helper.py`. – Sraw Sep 01 '19 at 16:47

0 Answers0