192

mechanizeをインストールした後、インポートできないようです。

私はpip、easy_installから、そしてpython setup.py installこのリポジトリから経由してインストールしようとしました:https ://github.com/abielr/mechanize 。Pythonインタラクティブに入るたびに、次のようになります。

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
>>> 

以前に実行したインストールでは、正常に完了したことが報告されているため、インポートが機能することを期待しています。このエラーの原因は何ですか?

4

34 に答える 34

123

私の場合、それは許可の問題です。パッケージはどういうわけかrootrw権限のみでインストールされ、他のユーザーはそれにrwできません!

于 2013-05-04T17:55:33.507 に答える
108

私は同じ問題を抱えていました:スクリプトimport coloramaがスローされ、ImportErrorが発生しましたが、sudo pip install colorama「パッケージはすでにインストールされています」と表示されていました。

私の修正:sudoなしでpipを実行します:pip install colorama。次に、pipは、インストールしてインストールする必要があることに同意し、スクリプトを実行しました。

私の環境はUbuntu14.0432ビットです。virtualenvをアクティブ化する前後にこれを見たと思います。

更新:さらに良いのは、を使用することpython -m pip install <package>です。これの利点は、パッケージを入れたい特定のバージョンのpythonを実行しているため、pipがパッケージを「正しい」pythonに明確にインストールすることです。繰り返しになりますが、この場合はsudoを使用しないでください...そうすれば、適切な場所でパッケージを取得できますが、おそらく(不要な)root権限が付与されます。

于 2016-01-14T19:12:47.353 に答える
27

これはPythonパスの問題です。

私の場合、Pythonは次の場所にインストールされています。

/Library/Frameworks/Python.framework/Versions/2.6/bin/python,

python2.6内にsite-packagesディレクトリはありません。

pipでインストールしたパッケージ(SOAPpy)は

/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/

そして、site-packageはpythonパスにありません。私がしたのは、site-packagesをPYTHONPATHに永続的に追加することだけです。

  1. ターミナルを開く

  2. open.bash_profileと入力します

  3. ポップアップ表示されるテキストファイルで、最後に次の行を追加します。

    export PYTHONPATH=$PYTHONPATH:/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
    
  4. ファイルを保存し、ターミナルを再起動すると、完了です

于 2014-04-22T17:45:22.257 に答える
19

Pythonのインポートメカニズムは、実際には次のいずれかで機能します。

  1. あなたのPYTHONPATHは間違っています、
  2. ライブラリが思った場所にインストールされていません
  3. これをマスクしている同じ名前の別のライブラリがあります
于 2013-01-12T17:19:35.650 に答える
17

組み合わせたアプローチでこの問題を修正することができました。まず、Chrisのアドバイスに従い、コマンドラインを開いて「pipshow packagename」と入力しました。これにより、インストールされたパッケージの場所がわかりました。

次に、Pythonを開き、「import sys」と入力してから、「sys.path」と入力して、Pythonがインポートしたパッケージを検索する場所を示します。残念ながら、最初のステップで示された場所はリストにありませんでした。

最後のステップで、「sys.path.append('package_location_seen_in_step_1')」と入力しました。オプションで、手順2を繰り返して、場所がリストに含まれていることを確認できます。

ステップをテストし、パッケージをもう一度インポートしてみてください...動作します。

欠点は?これは一時的なものであり、毎回リストに追加する必要があります。

于 2018-04-01T20:19:26.200 に答える
13

若いヒップなインターンがモジュールディレクトリ内の「pythonsetup.pyinstall」に秘密があると私に言うまで、私はこれで私のモニターに頭をぶつけてきました。

何らかの理由で、そこからセットアップを実行すると、正常に機能します。

明確にするために、モジュールの名前が「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
于 2016-06-21T22:32:23.230 に答える
7

を介してインストールしたキーリングを使用しようとしたときに、これに遭遇しましたsudo pip install keyring。他の回答で述べたように、私の場合は権限の問題です。

私のために働いたこと:

  1. アンインストールされたキーリング:
  • sudo pip uninstall keyring
  1. sudoの-Hオプションを使用して、キーリングを再インストールしました。
  • sudo -H pip install keyring
于 2018-09-04T05:56:08.280 に答える
6
于 2019-02-27T18:40:46.257 に答える
3

PYTHONPATHを正しく動作させることができませんでした。export追加すると問題が修正されたことに気づきました。

(仕事をしました)

export PYTHONPATH=$PYTHONPATH:~/test/site-packages

対。

(動作しませんでした)

PYTHONPATH=$PYTHONPATH:~/test/site-packages
于 2016-12-06T16:32:31.883 に答える
3

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.

于 2019-08-19T21:35:37.837 に答える
2

私の場合、実行したのですが、インタプリタpip install Django==1.11からインポートされませんでした。python

私が見つけたpipのコマンドを閲覧すると、次のpip showようになります。

> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...

場所が「3.4」と表示されていることに注意してください。python-コマンドがにリンクされていることがわかりましたpython2.7

/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7

そのすぐ隣にというリンクがpython3あったので、それを使用しました。リンクをに変更することもできますpython3.4。それもそれを修正するでしょう。

于 2017-04-06T14:05:07.317 に答える
2

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.

于 2019-04-26T09:26:18.177 に答える
1

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.

于 2019-08-07T01:29:08.033 に答える
1

Also, make sure that you do not confuse pip3 with pip. What I found was that package installed with pip was not working with python3 and vice-versa.

于 2019-11-14T14:14:03.283 に答える
1

Had this problem too.. the package was installed on Python 3.8.0 but VS Code was running my script using an older version (3.4)

fix in terminal:

py .py

Make sure you're installing the package on the right Python Version

于 2019-11-21T20:39:49.913 に答える
1

I had colorama installed via pip and I was getting "ImportError: No module named colorama"

So I searched with "find", found the absolute path and added it in the script like this:

import sys
sys.path.append("/usr/local/lib/python3.8/dist-packages/")
import colorama 

And it worked.

于 2020-08-25T13:01:32.363 に答える
1

This solution doesn't apply for everyone. Only for a specific set of newbies (LIKE ME) who did a specific set of actions. None of the above answers were working for me.

So what caused the problem: Previously I had a project with venv setup inside root. Later I created a new user and decided to move the project to this user. And instead of moving only the source files and installing the dependencies freshly, I moved the entire project along with the venv folder to the new user (Because I was not aware of these issues). After that the dependencies that I installed were getting added to the global site-packages instead of the one inside venv folder. So the code running inside this env was not able to access those dependencies.

Solution: Just removed the venv folder and recreated it again.

    $ deactivate
    $ rm -rf venv
    $ python3 -m venv venv
    $ source venv/bin/activate
    $ pip install -r requirements.txt
于 2020-11-24T05:34:35.403 に答える
0

easy_installまたはを介してインストールするとpip、正常に完了しますか?フル出力とは何ですか?どのPythonインストールを使用していますか?sudoモジュールをシステムディレクトリにインストールする場合(おそらく、システムpythonインストールを使用している場合)、インストールコマンドの前に使用する必要がある場合があります。あなたの質問には多くの有用な情報はありませんが、おそらく役立ついくつかのツールは次のとおりです。

  • echo $PYTHONPATHおよび/またはecho $PATH:モジュールをインポートするとき、Pythonはこれらの環境変数(ディレクトリのリスト、:区切られている)の1つで必要なモジュールを検索します。インポートの問題は、多くの場合、これらのリストに適切なディレクトリがないことが原因です。

  • which python、、、which pipまたはwhich easy_install:これらは各実行可能ファイルの場所を示します。知るのに役立つかもしれません。

  • @JesseBriggsが提案するように、virtualenvを使用します。pipこれは、個別のPythonプロジェクトのモジュールと環境を分離して管理するのに非常に役立ちます。

于 2013-01-12T17:26:56.890 に答える
0

私はこの正確な問題を抱えていましたが、上記の答えはどれもうまくいきませんでした。親プロジェクトからインポートした後、sys.pathが異なることに気付くまで、私は夢中になりました。プロジェクト階層にないファイルをインポートするために、importlibを使用して小さな関数を記述したことがわかりました。悪い考え:私はこれをしたことを忘れました。さらに悪いことに、インポートプロセスはsys.pathをいじくりまわし、そのままにしておきました。非常に悪い考えです。

解決策は、それを停止し、プロジェクトにインポートするために必要なファイルを配置することでした。別のアプローチは、ファイルを独自のプロジェクトに配置することでした。ファイルは時々再構築する必要があり、再構築はメインプロジェクトの再構築と一致する場合と一致しない場合があります。

于 2016-04-17T19:08:39.353 に答える
0

Python-Telegram-Botを使用してテレグラムボットをテストしようとして、システムにインストールされた2.7および3.5でこの問題が発生しました。

pipとpip3を使用して、sudoを使用して、または使用せずにインストールした後、動作させることができませんでした。私はいつも得ました:

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

エラーメッセージを正しく読むと、Pythonが現在のディレクトリで。を探していることがわかりますtelegram.py。そうです、telegram.pyというスクリプトがあり、これはPythonによって読み込まれましたimport

package.py結論として、インポートしようとするときは、現在の作業ディレクトリに何もないことを確認してください。(そしてエラーメッセージをよく読んでください)。

于 2017-01-10T07:09:29.103 に答える
0

私は(Windowsで)同様の問題を抱えていましたが、私の場合の根本的な原因はANTIVIRUSソフトウェアでした!実行中のプロセスをある種の仮想マシンでラップする「自動封じ込め」機能があります。症状は次のとおりですpip install somemodule。1つのコマンドラインウィンドウで正常に動作import somemoduleし、別のプロセスから実行するとエラーが発生して失敗する

ModuleNotFoundError: No module named 'somemodule'
于 2018-05-07T06:42:49.000 に答える
0

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.

于 2019-05-09T17:41:29.407 に答える
0

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>
于 2019-07-04T01:56:18.810 に答える
0

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.

于 2019-08-02T16:45:14.707 に答える
0

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

于 2019-09-18T15:43:36.993 に答える
0

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.

于 2019-09-24T13:18:50.393 に答える
0

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.

于 2019-09-29T12:06:29.760 に答える
0

If you are using a virtual environment use pipenv install <module name> instead of pip install <module name>

Worked for me.

于 2019-10-04T10:08:53.400 に答える
0

As a friend did for me today, here is what helped me (I am using Windows):

Press 'Setting' -> 'Project' -> 'Project Interpreter'. Here in the window on the right, there is a line with the title 'Project Interpreter' on it's left. Click this line and it will open several additional lines.

Now press the 'Show All' line. A window will open. In this window press the small '+' sign in the upper right corner.

A new window will open. On the left there are 4 tabs, press the most upper one, which says 'Virtualenv Environment'. Now, in the window on the right, mark the 'Existing Environment' option. The 'Interpreter' line will become well visible. Press the '...' button on the right of the line.

Now, a browsing window will open. Browse to the directory that you installed Python itself in. Not the one with PyCharm. When you get there, choose the 'python.exe' file and press OK (the window will disappear).

Press OK again (this window will disappear too).

Now in this window make sure the new line you created is marked, and press OK again.

Now, all the installed packages should be visible in the project interpreter, and are read by your program.

于 2019-12-22T13:02:24.833 に答える
0

In my case (an Ubuntu 20.04 VM on WIN10 Host), I have a disordered situation with many version of Python installed and variuos point of Shared Library (installed with pip in many points of the File System). I'm referring to 3.8.10 Python version. After many tests, I've found a suggestion searching with google (but' I'm sorry, I haven't the link). This is what I've done to resolve the problem :

  1. From shell session on Ubuntu 20.04 VM, (inside the Home, in my case /home/hduser), I've started a Jupyter Notebook session with the command "jupyter notebook".

  2. Then, when jupyter was running I've opened a .ipynb file to give commands.

  3. First : pip list --> give me the list of packages installed, and, sympy wasn't present (although I had installed it with "sudo pip install sympy" command.

  4. Last with the command !pip3 install sympy (inside jupyter notebook session) I've solved the problem, here the screen-shot : enter image description here

  5. Now, with !pip list the package "sympy" is present, and working : enter image description here

于 2022-01-09T11:43:33.163 に答える
-1

少し話題から外れているかもしれませんが、問題がありましたimport PyYAML。あなたがする必要があることを指摘しimport yamlます。(それは古典的なrtfmだと思います...)

于 2019-01-04T09:32:42.953 に答える
-1

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.

于 2019-09-14T18:50:36.513 に答える
-1

Simplest solution that worked for me that I don't see mentioned in this thread:

I have multiple versions of Python installed but was trying to use Python3.7 -- so I had to use:

sudo pip3.7 install <package>

于 2020-02-03T21:38:05.893 に答える
-16

virtualenv(非常に単純です)の使用方法を学ぶと、これらの問題は少なくなります。virtualenvをソースするだけで、ローカル(プロジェクトへの)パッケージを使用します。

パスやバージョンなどで、私にとって多くの頭痛の種を解決します。

于 2013-01-12T17:18:56.597 に答える