0

I have looked at I think 5 different answers to this problem, yet none of them have worked for me yet. For reference, I've looked through all of these posts:

Relative imports for the billionth time

Attempted relative import with no known parent package

"Attempted relative import with no known parent package"

From what I've gathered, there are two solutions to this problem:

  1. Move the .py file you're trying to import functions from into the same directory as the script you're trying to run (this works, but it is not a good solution, I should be able to import from a parent directory without this error)

  2. Create a __init__.py file in the directory of the .py file you're trying to import from, and use import package_name to it. (I have tried this, but same issue)

Here is my project's structure:

structure

I'm trying to run the test.py script, which (attempts) to import the function add_technical_indicators from the add_technical_indicators.py file. My import statement looks like this:

from ..utils.add_technical_indicators import add_technical_indicators

Looking at the folder structure again, I have to go UP one directory, then into the utils folder to bring in the add_technical_indicators .py file, and finally the function add_technical_indicators.

Here's what I have tried so far:

from ..utils.add_technical_indicators import add_technical_indicators

from .utils.add_technical_indicators import add_technical_indicators

from utils.add_technical_indicators import add_technical_indicators (this doesn't work of course because add_technical_indicators is not in the same folder as the script being run)

Created an __init__.py file in the utils folder that reads import add_technical_indicators

Created an __init__.py file in the misc folder that reads import test

None of it works. I need a concise and actionable answer as to why this is still not working. I'm running Python 3.7.9, Windows 10, and VS code in case that matters.

I have looked through previous, repeat answers but none of them have worked for me, so although this IS a duplicate question, please do not close it until I have a solution because linking to the already "answered" questions didn't help me.

wildcat89
  • 1,159
  • 16
  • 47

2 Answers2

1

short solution

In test.py import as from utils.add_technical_indicators import add_technical_indicators and run as python -m misc.test (not test.py) in parent directory(in STOCK_PEAKS_ADN_THROUGHS_2)

explained

python cannot import files from upper directory of your working directory unless you add them to PATH, so from ..utils.add_technical_indicators import add_technical_indicators won't work.

python finds files starting from your working directory, so from utils.add_technical_indicators import add_technical_indicators in test.py can find utils/add_technical_indicators.py if you run script at parent directory.

python -m misc.test will run misc/test.py in parent directory. Note that python misc/test.py will run script in misc directory and will give you same error.

minolee
  • 404
  • 2
  • 8
  • Thank you! I see what I was doing wrong, can't look up a directory if I'm running the project from inside a sub dir. Thanks! – wildcat89 Jul 06 '22 at 07:05
0

You did the right thing here: from ..utils.add_technical_indicators import add_technical_indicators

The main issue must be in the imports of utils/add_technical_indicators.py file

Did you try importing stuffs inside add_technical_indicators.py relative to itself (if so, those imports only work when add_technical_indicators.py is run instead of test.py)

Rajesh Kanna
  • 113
  • 6