0

I have one project directory and three sub-folders as such:

Project
--V1
--V2
--Testing

V1 and V2 contain versions of the same code, and Testing contains scripts that run code from V1 and V2.

Lets say V1 and V2 each contain helper functions in a file called helper.py that take a value and does something to it as such:

Project
--V1
----helper.py
--V2
----helper.py
--Testing

helper.py

def someFunc(value):
    # do something w/ value

How can I import these functions into a python file inside Testing? I am trying to achieve something like the following, which does not work:

from ../V2.helper import someFunc

Thanks for the help!

2
  • 1
    Why do you say it doesn't work? What result are you getting? Commented Jun 22, 2022 at 0:49
  • It tells me it is an invalid import library Commented Jun 22, 2022 at 17:41

1 Answer 1

1

First, you have to make each folder a python module, by adding an __init__.py file to each one, like this:

Project
__init__.py
--V1
----helper.py
----__init__.py
--V2
----helper.py
----__init__.py
--Testing
--__init__.py

With this, you can make absolute imports in your scripts:

from Project.V2.helper import someFunc
Sign up to request clarification or add additional context in comments.

1 Comment

I will try this, thanks! Does the init function register the dir with the python interpreter or something to that effect? Can you explain how this works?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.