1

I’m trying to use a class from other folder.

I created an __init__.py file in all my folders. But I sill can’t import the class. I’m trying to import Vector class from ex02/vector,py to ex03/matrix.py

Here is the tree file

day01
.
├── README.md
├── __init__.py
├── ex00
│   ├── __init__.py
│   ├── book.py
│   ├── recipe.py
│   └── test.py
├── ex01
│   ├── __init__.py
│   └── game.py
├── ex02
│   ├── __init__.py
│   ├── pytest.py
│   └── vector.py
└── ex03
    ├── __init__.py
    └── matrix.py

I’m importing using this command:

from day01.ex02.vector import Vector

But I’m getting this error:

Traceback (most recent call last):
  File "matrix.py", line 1, in <module>
    from day01.ex02.vector import Vector
ModuleNotFoundError: No module named 'day01' 

The python code that is importing is this one:

https://pastebin.com/8AZAV6fv

And the one that I'm trying to import is this one

https://pastebin.com/BkYX9sc4

  • Does this answer your question? [Relative imports in Python 2.7](https://stackoverflow.com/questions/14132789/relative-imports-in-python-2-7) – dwb Jul 10 '20 at 00:05

1 Answers1

0

To access files between folders inside your project you just need to mention the folder name and them the filename.

for example:

ProjectFolder:

  • Main.py
  • folderA -> FileA.py
  • FolderB -> FileB.py

let's say that you are in right now in file FileA and you want to use FileB's content in it.

all you have to do is:

from FolderB import File 

Here's an actually example in VS code

So in your code in the matrix.py file just write:

from ex02 import Vector