티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
How do I fix PyDev "Undefined variable from import" errors?
PyDev의 "import에서 정의되지 않은 변수" 오류를 어떻게 해결하나요?
문제 내용
I've got a Python project using PyDev in Eclipse, and PyDev keeps generating false errors for my code. I have a module settings
that defines a settings
object. I import that in module b
and assign an attribute with:
제가 PyDev를 사용하는 Eclipse에서 Python 프로젝트를 작업 중인데, PyDev가 내 코드에 대해 잘못된 오류를 계속 생성합니다. settings 모듈에는 설정 객체를 정의하고 있으며, 모듈 b에서 이를 가져와 다음과 같이 속성을 할당합니다.
from settings import settings settings.main = object()
In some of my code--but not all of it, statements like:
전부는 아니지만 일부 코드에서 다음과 같은 문장이 있습니다.
from settings import settings print settings.main
... generate "Undefined variable from import: main" messages in the Eclipse code error pane, even though the code runs without a problem. How can I correct these?
... 코드가 문제 없이 실행되는 경우에도 Eclipse 코드 오류 창에서 "Undefined variable from import: main" 메시지를 생성합니다. 이것들을 어떻게 고칠 수 있을까요?
높은 점수를 받은 Solution
For code in your project, the only way is adding a declaration saying that you expected that -- possibly protected by an if False
so that it doesn't execute (the static code-analysis only sees what you see, not runtime info -- if you opened that module yourself, you'd have no indication that main was expected).
프로젝트 내 코드에서는, 유일한 방법은 선언을 추가해서 그렇게 의도했다고 알리는 것입니다. 가능하면 if False로 보호하여 실행되지 않게 해야합니다. (정적 코드 분석기는 런타임 정보가 아닌 당신이 보는 것만 볼 수 있습니다. 모듈을 직접 열었을 때 main이 예상될 것이라는 표시가 없습니다.)
To overcome this there are some choices:
이 문제를 해결하는 방법은 다음과 같습니다:
- If it is some external module, it's possible to add it to the
forced builtins
so that PyDev spawns a shell for it to obtain runtime information (see http://pydev.org/manual_101_interpreter.html for details) -- i.e.: mostly, PyDev will import the module in a shell and do adir(module)
anddir
on the classes found in the module to present completions and make code analysis. - You can use Ctrl+1 (Cmd+1 for Mac) in a line with an error and PyDev will present you an option to add a comment to ignore that error.
- It's possible to create a
stub
module and add it to thepredefined
completions (http://pydev.org/manual_101_interpreter.html also has details on that).
1. 만약 이 문제가 외부 모듈에서 발생하는 것이라면, PyDev가 런타임 정보를 얻기 위해 쉘을 생성하도록 강제로 내장 모듈로 추가하는 것이 가능합니다. (자세한 내용은 참조) 즉, 대부분 PyDev는 모듈을 가져와서 dir (module)과 클래스의 dir에 대해 완성 및 코드 분석을 제공하기 위해 쉘에서 모듈을 가져와 임포트합니다.
2. Ctrl + 1 (Mac의 경우 Cmd + 1)을 사용하여 오류가 있는 줄에서 PyDev가 해당 오류를 무시할 주석을 추가하는 옵션을 제공합니다.
3. 스텁 모듈을 만들고 미리 정의된 완성 목록에 추가할 수도 있습니다 ([http://pydev.org/manual\_101\_interpreter.html에](http://pydev.org/manual_101_interpreter.html%EC%97%90) 자세한 내용이 있습니다).
가장 최근 달린 Solution
This worked for me:
이게 나한테 도움이 됐어:
step 1) Removing the interpreter, auto configuring it again
단계 1) 인터프리터 제거 및 자동 구성 다시 설정하기
step 2) Window - Preferences - PyDev - Interpreters - Python Interpreter Go to the Forced builtins tab Click on New... Type the name of the module (curses in my case) and click OK
2단계) Window - Preferences - PyDev - Interpreters - Python Interpreter로 이동합니다. 강제 내장 탭으로 이동합니다. New...를 클릭합니다. 모듈 이름(curses in my case)을 입력하고 OK를 클릭합니다.
step 3) Right click in the project explorer on whichever module is giving errors. Go to PyDev->Code analysis.
단계 3) 오류가 발생하는 모듈을 마우스 오른쪽 버튼으로 클릭하고 PyDev->Code analysis로 이동합니다.
출처 : https://stackoverflow.com/questions/2112715/how-do-i-fix-pydev-undefined-variable-from-import-errors
'개발 > 파이썬' 카테고리의 다른 글
'알려진 상위 패키지가 없는 상대 가져오기를 시도했습니다' 오류 수정하기 (0) | 2023.01.29 |
---|---|
map() 함수를 사용하여 리스트를 반환하는 방법 (0) | 2023.01.28 |
Python에서 알파벳 리스트 만들기 (0) | 2023.01.28 |
여러 개의 서브플롯을 가지고 있을 때 서브플롯 크기/간격 조절하기 (0) | 2023.01.28 |
Python 모듈 os.chmod 제대로 사용하기 (0) | 2023.01.27 |