0

I have an issue where I dont get the solution. I also look here in stackoverflow.com, tried different solutions but nothing helps.

My project struct looks like this:

-MyFolder
----MySubFolder
------MySubFolder.py
------__init__.py
----MyFolder.py
----__init__.py

To be clear. MyFolder.py and his init.py are below MyFolder and above MySubFolder.

In a different project, I import MyFolder, but PyCharm doesn´t recognized MyFolder as a Package. MyFolder is part of PYTHONPATH, so it should be found.

In the init.py inside MyFolder, it looks like:

import os
import sys
fpath = os.path.join(os.path.dirname(__file__))
sys.path.append(fpath)

from MyFolder import *

and in MySubFolder

import os
import sys
fpath = os.path.join(os.path.dirname(__file__))
sys.path.append(fpath)

from MySubFolder import *

But when I do

from MyFolder.MyFolder import *

then it working. But my goal is only to import MyFolder

3
  • 6
    sys.path modifications are very often not necessary, rather a red flag. I would remove all of them if I were you. There is very likely a much simpler way to achieve what you want to do. -- It would be better if you used clearer names for your example. Having a MyFolder directory and a MyFolder.py file make things very hard to follow. Commented Sep 30, 2024 at 16:50
  • 2
    Have you tried installing your package into your virtual environment, as a pop editable install? Commented Sep 30, 2024 at 17:05
  • 2
    The are two ways to layout a Python project, often referred to as "flat" and "src". packaging.python.org/en/latest/discussions/… . You have gone for the src layout which requires installation of the project to be able to run its code, rather than the flat layout which does not. This means that the src layout involves an additional step in the development workflow of a project (typically, an editable installation is used for development and a regular installation is used for testing). Commented Oct 1, 2024 at 4:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.