Methodicle
Methodicle

Reputation: 59

Folder structure and imports

So I have a small project i'm working on currently but im struggling with imports. originally I had a struct like so:

root -> python
     -> tests  -> testscripts

Im running my tests from the python folder and I had a class in that folder called pertest_resources that contained paths to other folders like the root and the script folder.

python_folder = os.path.realpath(os.curdir)
perf_root_folder = os.path.realpath(python_folder + "/../")
test_root_folder = os.path.realpath(python_folder + "/../tests")
script_folder = os.path.realpath(test_root_folder + "/scripts")
network_folder = os.path.realpath(test_root_folder + "/networks")
output_folder = os.path.realpath(test_root_folder + "/output")
input_folder = os.path.realpath(test_root_folder + "/input")
kit_folder = os.path.realpath(perf_root_folder + "/kit")
sys.path.append(python_folder)
sys.path.append(script_folder)
sys.path.append(network_folder)

Now this worked fine when all the core files were in the python folder, but now its getting a little messy i've been asked to branch off some of the python scripts to a new folder called 'networks' with a structure like:

root -> python -> perftest_resources.py
     -> tests  -> testscripts
               -> networks -> standard_network.py

Now I want to run a file from the python folder called test that uses the standard_network that in turn imports perftest_resources from the python folder.

I've tried:

sys.path.append('..' + os.sep + '..') 
from python.perftest_resources import PerfTestResources as pr

but that doesn't work when its all originally run from test.py in the python folder. Do I need to add init to both folders?

Upvotes: 1

Views: 121

Answers (1)

grapes
grapes

Reputation: 8646

Is it a package? If true, you should use intra-package references, like

from . import echo
from .. import formats
from ..filters import equalizer

If not, turn it into package )

Read more: https://docs.python.org/3/tutorial/modules.html

Upvotes: 1

Related Questions