확실히 설치된 모듈을 가져올 수 없습니다
mechanize를 설치 한 후 가져올 수없는 것 같습니다.
pip, easy_install 및 https://github.com/abielr/mechanizepython setup.py install
에서 설치를 시도했습니다 . 파이썬 인터랙티브에 들어갈 때마다 다음과 같이 얻을 수 있습니다.
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>>
이전에 실행 한 설치에서 성공적으로 완료된 것으로보고되었으므로 가져 오기가 작동 할 것으로 예상합니다. 이 오류의 원인은 무엇입니까?
제 경우에는 권한 문제입니다. 패키지는 루트 rw 권한으로 만 설치되었으며 다른 사용자는 rw를 사용할 수 없습니다!
같은 문제가 발생했습니다. with with 스크립트가 발생 import colorama
하고 ImportError가 발생했지만 sudo pip install colorama
"package already installed"라는 메시지가 표시되었습니다.
내 수정 : sudo없이 pip 를 실행 하십시오 pip install colorama
. 그런 다음 pip는 설치, 설치 및 스크립트 실행이 필요하다는 데 동의했습니다.
내 환경은 Ubuntu 14.04 32 비트입니다. 나는 virtualenv를 활성화하기 전후에 이것을 보았다고 생각합니다.
업데이트 : 더 좋습니다 python -m pip install <package>
. 이것의 장점은 패키지를 원하는 특정 버전의 파이썬을 실행하기 때문에 pip는 패키지를 "올바른"파이썬에 분명히 설치한다는 것입니다. 다시 하지 않는 당신은 (원치 않는) 루트 권한으로 가능성이 올바른 위치에서 패키지를 얻을,하지만 ...이 경우는 sudo를 사용합니다.
파이썬 경로 문제입니다.
제 경우에는 다음에 파이썬이 설치되어 있습니다.
/Library/Frameworks/Python.framework/Versions/2.6/bin/python,
python2.6에는 사이트 패키지 디렉토리가 없습니다.
pip로 설치 한 패키지 (SOAPpy)가 있습니다
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
그리고 사이트 패키지는 파이썬 경로에 있지 않습니다. 내가 한 것은 사이트 패키지를 PYTHONPATH에 영구적으로 추가하는 것입니다.
- 터미널을여십시오
- open .bash_profile을 입력하십시오.
나타나는 텍스트 파일에서 끝에 다음 줄을 추가하십시오.
내보내기 PYTHONPATH = $ PYTHONPATH : /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
- 파일을 저장하고 터미널을 다시 시작하면 완료
파이썬 가져 오기 메커니즘은 실제로 작동합니다.
- 귀하의 PYTHONPATH가 잘못되었습니다.
- 라이브러리가 생각한 곳에 설치되지 않았습니다
- 이 이름을 마스킹하는 동일한 이름의 다른 라이브러리가 있습니다.
젊은 힙턴 인턴 이 모듈 디렉토리 내부의 "python setup.py install"에 대한 비밀을 밝힐 때까지 모니터에 대해 머리를 두 드렸습니다 .
어떤 이유로 든 거기에서 설치 프로그램을 실행하면 작동합니다.
명확하게 말하면 모듈 이름이 "foo"인 경우 :
[burnc7 (2016-06-21 15:28:49) git]# ls -l
total 1
drwxr-xr-x 7 root root 118 Jun 21 15:22 foo
[burnc7 (2016-06-21 15:28:51) git]# cd foo
[burnc7 (2016-06-21 15:28:53) foo]# ls -l
total 2
drwxr-xr-x 2 root root 93 Jun 21 15:23 foo
-rw-r--r-- 1 root root 416 May 31 12:26 setup.py
[burnc7 (2016-06-21 15:28:54) foo]# python setup.py install
<--snip-->
경로를 호출하여 다른 디렉토리에서 setup.py를 실행하려고하면 설치가 중단됩니다.
작동하지 않습니다 :
python /root/foo/setup.py install
작동합니다 :
cd /root/foo
python setup.py install
결합 된 접근 방식 으로이 문제를 해결할 수있었습니다. 먼저 Chris의 조언에 따라 명령 줄을 열고 'pip show packagename'을 입력했습니다. 설치된 패키지의 위치가 제공되었습니다.
다음으로 파이썬을 열고 'import sys'를 입력 한 다음 'sys.path'를 입력하여 파이썬이 가져온 패키지를 검색하는 위치를 보여줍니다. 아아, 첫 번째 단계에서 표시된 위치가 목록에 없었습니다.
마지막 단계에서는 'sys.path.append ('package_location_seen_in_step_1 ')을 입력했습니다. 선택적으로 2 단계를 반복하여 현재 위치가 목록에 있는지 확인할 수 있습니다.
Test step, try to import the package again... it works.
The downside? It is temporary, and you need to add it to the list each time.
I encountered this while trying to use keyring which I installed via sudo pip install keyring
. As mentioned in the other answers, it's a permissions issue in my case.
What worked for me:
- Uninstalled keyring:
sudo pip uninstall keyring
- I used sudo's
-H
option and reinstalled keyring:sudo -H pip install keyring
Hope this helps.
I couldn't get my PYTHONPATH to work properly. I realized adding export
fixed the issue:
(did work)
export PYTHONPATH=$PYTHONPATH:~/test/site-packages
vs.
(did not work)
PYTHONPATH=$PYTHONPATH:~/test/site-packages
In my case I had run pip install Django==1.11
and it would not import from the python
interpreter.
Browsing through pip's commands I found pip show
which looked like this:
> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...
Notice the location says '3.4'. I found that the python
-command was linked to python2.7
/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7
Right next to that I found a link called python3
so I used that. You could also change the link to python3.4
. That would fix it, too.
I am new to python. I fixed this issue by changing the project interpreter path.
File -> Settings -> Project -> Project Interpreter
In my case it was a problem with a missing init.py file in the module, that I wanted to import in a Python 2.7 environment.
Python 3.3+ has Implicit Namespace Packages that allow it to create a packages without an init.py file.
I had this exact problem, but none of the answers above worked. It drove me crazy until I noticed that sys.path was different after I had imported from the parent project. It turned out that I had used importlib to write a little function in order to import a file not in the project hierarchy. Bad idea: I forgot that I had done this. Even worse, the import process mucked with the sys.path--and left it that way. Very bad idea.
The solution was to stop that, and simply put the file I needed to import into the project. Another approach would have been to put the file into its own project, as it needs to be rebuilt from time to time, and the rebuild may or may not coincide with the rebuild of the main project.
I had this problem with 2.7 and 3.5 installed on my system trying to test a telegram bot with Python-Telegram-Bot.
I couldn't get it to work after installing with pip and pip3, with sudo or without. I always got:
Traceback (most recent call last):
File "telegram.py", line 2, in <module>
from telegram.ext import Updater
File "$USER/telegram.py", line 2, in <module>
from telegram.ext import Updater
ImportError: No module named 'telegram.ext'; 'telegram' is not a package
Reading the error message correctly tells me that python is looking in the current directory for a telegram.py
. And right, I had a script lying there called telegram.py and this was loaded by python when I called import
.
Conclusion, make sure you don't have any package.py
in your current working dir when trying to import. (And read error message thoroughly).
I had similar problem (on Windows) and the root cause in my case was ANTIVIRUS software! It has "Auto-Containment" feature, that wraps running process with some kind of a virtual machine. Symptoms are: pip install somemodule
works fine in one cmd-line window and import somemodule
fails when executed from another process with the error
ModuleNotFoundError: No module named 'somemodule'
I hope it will save some time to somebody :)
Maybe a bit off-topic, but i had issues to import PyYAML
. Points out that you need to import yaml
. (guess it's a classical rtfm...)
I had a similar problem using Django. In my case, I could import the module from the Django shell, but not from a .py which imported the module.
The problem was that I was running the Django server (therefore, executing the .py) from a different virtualenv from which the module had been installed.
Instead, the shell instance was being run in the correct virtualenv. Hence, why it worked.
This Works!!!
This often happens when module is installed to an older version of python or another directory, no worries as solution is simple. - import module from directory in which module is installed. You can do this by first importing the python sys
module then importing from the path in which the module is installed
import sys
sys.path.append("directory in which module is installed")
import <module_name>
Most of the possible cases have been already covered in solutions, just sharing my case, it happened to me that I installed a package in one environment (e.g. X
) and I was importing the package in another environment (e.g. Y
). So, always make sure that you're importing the package from the environment in which you installed the package.
If the other answers mentioned do not work for you, try deleting your pip cache and reinstalling the package. My machine runs Ubuntu14.04 and it was located under ~/.cache/pip
. Deleting this folder did the trick for me.
Something that worked for me was:
python -m pip install -user {package name}
The command does not require sudo. This was tested on OSX Mojave.
In my case I had to also install the module(s) for the superuser, too.
sudo su
pip install <module>
Apparently the superuse cannot access the normal users files under certain circumstances.
For me it was ensuring the version of the module aligned with the version of Python I was using.. I built the image on a box with Python 3.6 and then injected into a Docker image that happened to have 3.7 installed, and then banging my head when Python was telling me the module wasn't installed...
36m
for Python 3.6 bsonnumpy.cpython-36m-x86_64-linux-gnu.so
37m
for Python 3.7 bsonnumpy.cpython-37m-x86_64-linux-gnu.so
I know this is a super old post but for me, I had an issue with a 32 bit python and 64 bit python installed. Once I uninstalled the 32 bit python, everything worked as it should.
I have solved my issue that same libraries were working fine in one project(A) but importing those same libraries in another project(B) caused error. I am using Pycharm as IDE at Windows OS. So, after trying many potential solutions and failing to solve the issue, I did these two things (deleted "Venv" folder, and reconfigured interpreter):
1-In project(B), there was a folder named("venv"), located in External Libraries/. I deleted that folder.
2-Step 1 (deleting "venv" folder) causes error in Python Interpreter Configuration, and there is a message shown at top of screen saying "Invalid python interpreter selected for the project" and "configure python interpreter", select that link and it opens a new window. There in "Project Interpreter" drop-down list, there is a Red colored line showing previous invalid interpreter. Now, Open this list and select the Python Interpreter(in my case, it is Python 3.7). Press "Apply" and "OK" at the bottom and you are good to go.
Note: It was potentially the issue where Virtual Environment of my Project(B) was not recognizing the already installed and working libraries.
If you are using a virtual environment use pipenv install <module name>
instead of pip install <module name>
Worked for me.
When you install via easy_install
or pip
, is it completing successfully? What is the full output? Which python installation are you using? You may need to use sudo
before your installation command, if you are installing modules to a system directory (if you are using the system python installation, perhaps). There's not a lot of useful information in your question to go off of, but some tools that will probably help include:
echo $PYTHONPATH
and/orecho $PATH
: when importing modules, Python searches one of these environment variables (lists of directories,:
delimited) for the module you want. Importing problems are often due to the right directory being absent from these listswhich python
,which pip
, orwhich easy_install
: these will tell you the location of each executable. It may help to know.Use virtualenv, like @JesseBriggs suggests. It works very well with
pip
to help you isolate and manage the modules and environment for separate Python projects.
If you learn how to use virtualenv (which is pretty dead-simple), you will have less of these issues. You'll just source the virtualenv and then you will be using local (to the project) packages.
It solves a lot of headache for me with paths, versions, etc.
참고URL : https://stackoverflow.com/questions/14295680/unable-to-import-a-module-that-is-definitely-installed
'Programing' 카테고리의 다른 글
System.gc ()는 언제 작동합니까? (0) | 2020.07.29 |
---|---|
Subversion을 사용하여 현재 체크 아웃 된 분기와 다른 분기로 변경 사항 커밋 (0) | 2020.07.29 |
일반적인 방법을 사용하는시기와 와일드 카드를 사용하는시기는? (0) | 2020.07.29 |
C ++로 짧은 리터럴을 작성하는 방법 (0) | 2020.07.29 |
주어진 수의 요소로 목록 자르기 (0) | 2020.07.29 |