在安装mechanize后,我似乎无法导入它。

我已经尝试从pip、easy_install和通过python setup.py从这个repo安装: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
>>> 

我之前运行的安装报告已经成功完成,因此我希望导入能够正常工作。是什么导致了这个错误?


当前回答

当您通过easy_install或pip安装时,它是否成功完成?全输出是多少?您正在使用哪个python安装?如果您正在将模块安装到系统目录(如果您正在使用系统python安装),则可能需要在使用安装命令之前使用sudo。你的问题中没有太多有用的信息,但是一些工具可能会有帮助,包括:

echo $PYTHONPATH and/or echo $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 lists which python, which pip, or which 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.

其他回答

当您通过easy_install或pip安装时,它是否成功完成?全输出是多少?您正在使用哪个python安装?如果您正在将模块安装到系统目录(如果您正在使用系统python安装),则可能需要在使用安装命令之前使用sudo。你的问题中没有太多有用的信息,但是一些工具可能会有帮助,包括:

echo $PYTHONPATH and/or echo $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 lists which python, which pip, or which 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.

对我有用的是:

python -m pip install -user {package name}

该命令不需要sudo。这是在OSX Mojave上测试的。

就像一个朋友今天为我做的那样,下面是帮助我的方法(我正在使用Windows):

按“设置”->“项目”->“项目解释器”。在右边的窗口中,左边有一行标题为“Project Interpreter”。单击该行,它将打开其他几行。

现在按下“显示全部”行。一扇窗会打开。 在这个窗口中,按下右上角的小“+”符号。

将打开一个新窗口。在左边有4个标签,按最上面的一个,上面写着“Virtualenv环境”。 现在,在右边的窗口中,标记‘Existing Environment’选项。'Interpreter'行将变得清晰可见。按“…”’按钮在行右边。

现在,一个浏览窗口将打开。浏览到安装Python本身的目录。不是有PyCharm的那个。当你到达那里,选择'python.exe'文件并按OK(窗口将消失)。

再次按OK(此窗口也将消失)。

现在在这个窗口中,确保您创建的新行被标记,并再次按OK。

现在,所有安装的包都应该在项目解释器中可见,并由程序读取。

在我的情况下,这是模块中缺少init.py文件的问题,我想在Python 2.7环境中导入该文件。

Python 3.3+具有隐式命名空间包,允许它在没有init.py文件的情况下创建包。

我一直在用脑袋撞显示器,直到一个年轻的实习生告诉我,秘诀是在模块目录中“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,那么您最终会得到一个borked安装。

不工作:

python /root/foo/setup.py install

做的工作:

cd /root/foo
python setup.py install