IT

ModuleNotFoundError : __main__이 패키지가 아니라는 것은 무엇을 의미합니까?

lottoking 2020. 6. 15. 08:07
반응형

ModuleNotFoundError : __main__이 패키지가 아니라는 것은 무엇을 의미합니까?


콘솔에서 모듈을 실행하려고합니다. 내 디렉토리의 구조는 다음과 같습니다.

여기에 이미지 설명을 입력하십시오

다음을 사용 p_03_using_bisection_search.py하여 problem_set_02디렉토리 에서 모듈을 실행하려고합니다 .

$ python3 p_03_using_bisection_search.py

내부 코드 p_03_using_bisection_search.py는 다음과 같습니다.

__author__ = 'm'


from .p_02_paying_debt_off_in_a_year import compute_balance_after


def compute_bounds(balance: float,
                   annual_interest_rate: float) -> (float, float):

    # there is code here, but I have omitted it to save space
    pass


def compute_lowest_payment(balance: float,
                           annual_interest_rate: float) -> float:

    # there is code here, but I have omitted it to save space
    pass    

def main():
    balance = eval(input('Enter the initial balance: '))
    annual_interest_rate = eval(input('Enter the annual interest rate: '))

    lowest_payment = compute_lowest_payment(balance, annual_interest_rate)
    print('Lowest Payment: ' + str(lowest_payment))


if __name__ == '__main__':
    main()

p_02_paying_debt_off_in_a_year.py코드가 있는 함수를 가져오고 있습니다 .

__author__ = 'm'


def compute_balance(balance: float,
                    fixed_payment: float,
                    annual_interest_rate: float) -> float:

    # this is code that has been omitted
    pass


def compute_balance_after(balance: float,
                          fixed_payment: float,
                          annual_interest_rate: float,
                          months: int=12) -> float:

    # Omitted code
    pass


def compute_fixed_monthly_payment(balance: float,
                                  annual_interest_rate: float) -> float:

    # omitted code
    pass


def main():
    balance = eval(input('Enter the initial balance: '))
    annual_interest_rate = eval(
        input('Enter the annual interest rate as a decimal: '))
    lowest_payment = compute_fixed_monthly_payment(balance,
                                                   annual_interest_rate)
    print('Lowest Payment: ' + str(lowest_payment))


if __name__ == '__main__':
    main()

다음과 같은 오류가 발생합니다.

ModuleNotFoundError: No module named '__main__.p_02_paying_debt_off_in_a_year'; '__main__' is not a package

이 문제를 해결하는 방법을 모르겠습니다. __init__.py파일 추가를 시도했지만 여전히 작동하지 않습니다.


상대 가져 오기의 점을 간단히 제거하고 다음을 수행하십시오.

from p_02_paying_debt_off_in_a_year import compute_balance_after

나는 당신과 같은 문제가 있습니다. 문제는에 상대 수입을 사용했다는 것입니다 in-package import. __init__.py디렉토리에 없습니다 . 따라서 모세가 위에서 대답 한대로 수입하십시오.

내가 생각하는 핵심 문제는 점으로 가져올 때입니다.

from .p_02_paying_debt_off_in_a_year import compute_balance_after

다음과 같습니다.

from __main__.p_02_paying_debt_off_in_a_year import compute_balance_after

where __main__ refers to your current module p_03_using_bisection_search.py.


Briefly, the interpreter does not know your directory architecture.

When the interpreter get in p_03.py, the script equals:

from p_03_using_bisection_search.p_02_paying_debt_off_in_a_year import compute_balance_after

and p_03_using_bisection_search does not contain any modules or instances called p_02_paying_debt_off_in_a_year.


So I came up with a cleaner solution without changing python environment valuables (after looking up how requests do in relative import):

The main architecture of the directory is:

main.py

setup.py

---problem_set_02/

------__init__.py

------p01.py

------p02.py

------p03.py

Then write in __init__.py:

from .p_02_paying_debt_off_in_a_year import compute_balance_after

Here __main__ is __init__ , it exactly refers to the module problem_set_02.

Then go to main.py:

import problem_set_02

You can also write a setup.py to add specific module to the environment.


Try to run it as:

python3 -m p_03_using_bisection_search


Hi Please follow below step, you will resolve this problem. If you have created directory and sub-directory then follow below steps and please keep in mind all directory must have "init.py" to get it recognized as a directory.

  1. "import sys" and run "sys.path" , you will be able to see all path that is being search by python.You must be able to see your current working directory.

  2. Now import sub-directory and respective module that you want to use using import follow this command: "import subdir.subdir.modulename as abc" and now you can use the methods in that module. ScreenShotforSameIssue

이 스크린 샷에서 볼 수 있듯이 하나의 상위 디렉토리와 두 개의 하위 디렉토리가 있고 두 번째 하위 디렉토리 아래에 module == CommonFunction이 있으며 sys.path 실행 후 오른쪽을 볼 수 있습니다. 작업 디렉토리를 볼 수 있습니다


점을 제거하고 파일 시작 부분에서 absolute_import를 가져옵니다.

from __future__ import absolute_import

from p_02_paying_debt_off_in_a_year import compute_balance_after

참고 URL : https://stackoverflow.com/questions/41816973/modulenotfounderror-what-does-it-mean-main-is-not-a-package

반응형