SoFunction
Updated on 2024-11-13

Solve the problem of python import reporting errors in the same directory

Description of the problem

Running an attention mechanism project these days, where the main function for train is under the big folder, and the rest of the files like dataloader and loss are under another sub-file.

Then there was an import error.

This is the operation of the other files in the folder src that I imported in train.

So the files in this src folder are these

Here you can see that my model file and attention file are indeed under the same folder, so it stands to reason that if I need to call a class in attention in the file model, I can just import attention.

But after I do that I get an error saying that the mod attentions could not be found.

prescription

The problem here is that the two files in the src folder can't call each other, so we right-click on the src folder and follow the diagram to mark the folder as Sources root.

Cause of the problem

I asked my labmates about it later, and it seems that this problem often occurs when running projects in pycharm.

It should be a problem with the default settings of the file directory, which can easily happen by directly copying a stream of someone else's project.

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.