콘솔에서 모듈을 실행하려고합니다. 내 디렉토리의 구조는 다음과 같습니다.
다음을 사용 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
여기서 __main__
현재 모듈을 나타냅니다 p_03_using_bisection_search.py
.
간단히, 인터프리터는 디렉토리 구조를 모른다.
인터프리터가 들어갈 때 p_03.py
스크립트는 다음과 같습니다.
from p_03_using_bisection_search.p_02_paying_debt_off_in_a_year import compute_balance_after
와 p_03_using_bisection_search
이라 어떤 모듈 또는 인스턴스를 포함하지 않습니다 p_02_paying_debt_off_in_a_year
.
그래서 파이썬 환경 귀중품을 변경하지 않고 깨끗한 솔루션을 생각해 냈습니다 ( 상대 가져 오기에서 요청이 수행되는 방식을 찾은 후 ).
디렉토리의 주요 아키텍처는 다음과 같습니다.
main.py
setup.py
—problem_set_02/
——__init__.py
——p01.py
——p02.py
——p03.py
그런 다음에 작성하십시오 __init__.py
.
from .p_02_paying_debt_off_in_a_year import compute_balance_after
여기 __main__
이고 __init__
정확히 모듈을 의미problem_set_02
.
그런 다음 main.py
:
import problem_set_02
setup.py
환경에 특정 모듈을 추가 하기 위해를 작성할 수도 있습니다.
답변
다음과 같이 실행하십시오.
python3 -m p_03_using_bisection_search
답변
디렉토리와 서브 디렉토리를 작성한 경우 아래 단계를 따르고 모든 디렉토리가 __init__.py
디렉토리로 인식되도록 해야합니다 .
-
스크립트에 포함
import sys
하고sys.path
, 파이썬 사용할 수있는 모든 경로를 볼 수 있습니다. 현재 작업 디렉토리를 볼 수 있어야합니다. -
다음을 사용하여 사용하려는 서브 디렉토리 및 해당 모듈을 가져 오십시오. 이제 해당 모듈
import subdir.subdir.modulename as abc
의 메소드를 사용할 수 있습니다.
예를 들어,이 스크린 샷에서 하나의 상위 디렉토리와 두 개의 하위 디렉토리가 있고 두 번째 하위 디렉토리 아래에 모듈이 CommonFunction
있습니다. 오른쪽 콘솔에는을 실행 한 후 sys.path
작업 디렉토리를 볼 수 있음이 표시됩니다.
답변
점을 제거하고 파일 시작 부분에서 absolute_import를 가져옵니다.
from __future__ import absolute_import
from p_02_paying_debt_off_in_a_year import compute_balance_after
답변
.py 파일이있는 기본 폴더의 이름 만 사용하십시오.
from problem_set_02.p_02_paying_debt_off_in_a_year import compute_balance_after