Python 导入错误:没有名为“硒”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31147660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:33:15  来源:igfitidea点击:

ImportError: No module named 'selenium'

pythonmacosseleniummodulewebdriver

提问by Giulio Colleluori

I'm trying to write a script to check a website. It's the first time I'm using selenium. I'm trying to run the script on a OSX system. Although I checked in /Library/Python/2.7/site-packages and selenium-2.46.0-py2.7.egg is present, when I run the script it keeps telling me that there is no selenium module to import.

我正在尝试编写脚本来检查网站。这是我第一次使用硒。我正在尝试在 OSX 系统上运行脚本。尽管我检查了 /Library/Python/2.7/site-packages 并且 selenium-2.46.0-py2.7.egg 存在,但当我运行脚本时,它一直告诉我没有要导入的 selenium 模块。

This is the log that I get when I run my code:

这是我运行代码时得到的日志:

Traceback (most recent call last):
  File "/Users/GiulioColleluori/Desktop/Class_Checker.py", line 10, in <module>
    from selenium import webdriver
ImportError: No module named 'selenium'
Traceback (most recent call last):
  File "/Users/GiulioColleluori/Desktop/Class_Checker.py", line 10, in <module>
    from selenium import webdriver
ImportError: No module named 'selenium'

采纳答案by gffbss

If you have pip installed you can install selenium like so.

如果你安装了 pip,你可以像这样安装 selenium。

pip install selenium

pip install selenium

or depending on your permissions:

或根据您的权限:

sudo pip install selenium

sudo pip install selenium

As you can see from this question pip vs easy_installpip is a more reliable package installer as it was built to improve easy_install.

从这个问题中可以看出pip vs easy_installpip 是一个更可靠的包安装程序,因为它是为了改进 easy_install 而构建的。

I would also suggest that when creating new projects you do so in virtual environments, even a simple selenium project. You can read more about virtual environments here. In fact pip is included out of the box with virtualenv!

我还建议在创建新项目时在虚拟环境中进行,即使是一个简单的 selenium 项目。您可以在此处阅读有关虚拟环境的更多信息。事实上,pip 已经包含在 virtualenv 中!

回答by Steven Correia

Even though the egg file may be present, that does not necessarily mean that it is installed. Check out this previous answer for some hint:

即使可能存在 egg 文件,也不一定意味着它已安装。查看之前的答案以获得一些提示:

How to install Selenium WebDriver on Mac OS

如何在 Mac OS 上安装 Selenium WebDriver

回答by as1992

make easy install again by downloading selenium webdriver from its website it is not installed properly.

通过从其网站下载 selenium webdriver 再次轻松安装它没有正确安装。

Edit 1: extract the .tar.gz folder go inside the directory and run python setup.py install from terminal.make sure you have installed setuptools.

编辑 1:将 .tar.gz 文件夹解压到目录中并从终端运行 python setup.py install。确保您已安装 setuptools。

回答by Rohit sai

first you should be sure that selenium is installed in your system.

首先,您应该确保系统中安装了 selenium。

then install pycharm https://itsfoss.com/install-pycharm-ubuntu/

然后安装 pycharm https://itsfoss.com/install-pycharm-ubuntu/

now if an of packages are not installed it will show red underlines. click on it and install from pycharm.

现在如果没有安装软件包,它将显示红色下划线。单击它并从 pycharm 安装。

like for this case click on selenium option in import statement, there you would getting some options. click on install selenium. it will install and automatically run the code successfully if all your drivers are placed in proper directories.

就像在这种情况下单击导入语句中的 selenium 选项一样,您将获得一些选项。点击安装硒。如果您的所有驱动程序都放在正确的目录中,它将成功安装并自动运行代码。

回答by mindmischief

Your IDE might be pointing to a different installation of Python than where Selenium is installed.

您的 IDE 可能指向与安装 Selenium 的位置不同的 Python 安装。

I'm using Eclipse and when I ran 'quick auto-configure' under:

我正在使用 Eclipse,当我在下面运行“快速自动配置”时:

Preferences > PyDev > Interpreters > Python Interpreter

首选项 > PyDev > 解释器 > Python 解释器

it pointed to a different version of Python than where pip or easy_install actually installed it.

它指向的 Python 版本与 pip 或 easy_install 实际安装它的位置不同。

Selenium worked from the Terminal so I determined which version of python my Terminal was using by running this:

Selenium 在终端上工作,所以我通过运行以下命令来确定我的终端使用的是哪个版本的 python:

python -c "import sys; print(sys.path)"

then had Eclipse point to that same location, which for me on my 10.11 Mac was here:

然后让 Eclipse 指向同一个位置,对我来说,在我的 10.11 Mac 上是这样的:

/Library/Frameworks/Python.framework/Versions/Current/bin/python2.7/

/Library/Frameworks/Python.framework/Versions/Current/bin/python2.7/

You can run "Advanced Auto-Config" as well to see all of the installed versions of python and select the one you want to use. When I selected that same location using "Advanced Auto-Config" it finally showed me the Selenium folder as it went through the configuration steps.

您也可以运行“高级自动配置”来查看所有已安装的 Python 版本并选择您要使用的版本。当我使用“高级自动配置”选择相同的位置时,它最终向我展示了 Selenium 文件夹,因为它完成了配置步骤。

回答by Rashid

Navigate to your scripts folder in Python directory (C:\Python27\Scripts) and open command line there (Hold shift and right click then select open command window here). Run pip install -U selenium
If you don't have pip installed, go ahead and install pip first

导航到 Python 目录 (C:\Python27\Scripts) 中的脚本文件夹并在那里打开命令行(按住 shift 并右键单击,然后选择在此处打开命令窗口)。运行pip install -U selenium
如果您没有安装 pip,请先安装 pip

回答by Brian

For python3, on a Mac you must use pip3 to install selenium.

对于 python3,在 Mac 上你必须使用 pip3 来安装 selenium。

sudo pip3 install selenium

回答by Wimukthi Rajapaksha

pip3 install selenium

Try this if you have python3.

如果你有 python3,试试这个。

回答by hamed baziyad

If you are using Anacondaor Spyderin windows, install seleniumby this code in cmd:

如果您正在使用AnacondaSpyderwindows,安装selenium该代码cmd

conda install selenium

If you are using Pycharm IDEin windows, install seleniumby this code in cmd:

如果您正在使用Pycharm IDEin windows,请selenium通过以下代码安装cmd

pip install selenium

回答by Gunjan Paul

Windows:

视窗:

pip install selenium

Unix:

Unix:

sudo pip install selenium