Skip to content Skip to sidebar Skip to footer

Import Main File (not Another Module) From A Subfolder

I want to import 'main' from a subfolder. Therefore every subfolder contains a __init__.py (but not 'source', and its actual name is 'Tracks Comparer'). My folders structure is li

Solution 1:

Assuming you are currently in Integration Tests folder? What about this:

from os import path
import sys
currentDirectory = path.dirname(__file__)
sys.path.append(path.join(currentDirectory, '../../')) # This will get you to sourceimport main

Solution 2:

Relative imports don't work at the file system level, so what you want to do can't be done with relative imports as you described.

It sounds like you are having issues with implicit relative imports (in mainTest when you import GPX it implicitly imports .GPX instead of GPX as a root module). You can turn off this behavior by adding this line at the top of all your test files:

from __future__ import absolute_import

This will make python import GPX from sys.path instead of .GPX.

Post a Comment for "Import Main File (not Another Module) From A Subfolder"