0

I'm on Windows 10, using Python 3.8.3. Here is my simple folder structure:

root_project_folder\
    __init__.py # Empty file

    project_1_folder\
        __init__.py # Empty file
        foo.py

    project_2_folder\
        __init__.py # Empty file
        bar.py

Within foo.py I have the following code:

from ..project_2_folder.bar import Test

Test.test_method()

Within bar.py I have the following code:

class Test:

    @staticmethod
    def test_method():
        print("Test successful")

When I run foo.py, I receive the following error:

ImportError: attempted relative import with no known parent package

I've read a few threads about importing using relative paths. Here are the threads for reference:

Python3 relative imports failing in package

Importing files from different folder

What is init.py for?

I did not append any directories to sys.path. I also did not modify PYTHONPATH either, such as adding directories as an environment variable. From what I have read, I don't need to do any of that. Would I have to add something to one of the __init__.py files?

Any help would be appreciated.

howdoicode
  • 779
  • 8
  • 16
  • you are calling ```foo.py```? I'm not sure if you are allowed to go up a level from the script you called... if you call a script in your root that calls ```foo.py``` will it work? – antonioplacerda Oct 01 '20 at 00:31
  • Yeah I figured that. I continued doing more research and it seems it won't work. I even added the appropriate paths using `sys.path` and `PYTHONPATH` environment variable. I couldn't get it to work. – howdoicode Oct 01 '20 at 14:28

0 Answers0