Python:没有名为 selenium 的模块

新手上路,请多包涵

在网上搜索了几个小时后,我还没有找到问题的答案。我正在使用 Python 3.6,但无法导入硒。我总是收到消息“没有名为‘selenium’的模块”我尝试了一切,我首先从这个网站 https://pypi.python.org/pypi/selenium/3.6.0 下载了 selenium。

然后我尝试了 python -m pip install -U selenium 也没有用。我尝试了人们所说的其他一些方法,但它们也没有用。我正在使用 Windows 10。有什么帮助吗?

原文由 Jordan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

正如您提到的,您是 using Python 3.6 遵循以下步骤:

  • 打开 Command Line InterfaceCLI )并发出命令 python 检查Python是否正确安装:
   C:\Users\username>python
  Python 3.6.1 (v3.6.1:69c0db5, Jan 16 2018, 17:54:52) [MSC v.1900 32 bit (Intel)]
   on win32
  Type "help", "copyright", "credits" or "license" for more information.
  >>>

  • 确保 pip 正常工作:
   C:\Users\username>pip

  Usage:
    pip <command> [options]

  Commands:
    install                     Install packages.
    download                    Download packages.
    uninstall                   Uninstall packages.
    freeze                      Output installed packages in requirements format.
    list                        List installed packages.
    show                        Show information about installed packages.
    check                       Verify installed packages have compatible dependencies.
    search                      Search PyPI for packages.
    wheel                       Build wheels from your requirements.
    hash                        Compute hashes of package archives.
    completion                  A helper command used for command completion.
    help                        Show help for commands.

  General Options:
    -h, --help                  Show help.
    --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
    -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
    -V, --version               Show version and exit.
    -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
    --log <path>                Path to a verbose appending log.
    --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
    --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
    --timeout <sec>             Set the socket timeout (default 15 seconds).
    --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
    --trusted-host <hostname>   Mark this host as trusted, even though it does not have valid or any HTTPS.
    --cert <path>               Path to alternate CA bundle.
    --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
    --cache-dir <dir>           Store the cache data in <dir>.
    --no-cache-dir              Disable the cache.
    --disable-pip-version-check
                                Don't periodically check PyPI to determine
                                whether a new version of pip is available for
                        download. Implied with --no-index.

  • 安装最新 selenium 通过 pip
   C:\Users\username>pip install -U selenium
  Collecting selenium
    Downloading selenium-3.8.1-py2.py3-none-any.whl (931kB)
      100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 942kB 322kB/s
  Installing collected packages: selenium
  Successfully installed selenium-3.8.1

  • 确认 Selenium 已安装:
   C:\Users\username>pip freeze
  selenium==3.8.1

  • 打开一个 IDE (例如 EclipsePyCharm )并编写一个简单的程序如下:
   from selenium import webdriver

  driver = webdriver.Firefox(executable_path="C:\\path\\to\\geckodriver.exe")
  driver.get('https://stackoverflow.com')

  • 执行 Firefox Quantum 将启动浏览器并访问 url https://stackoverflow.com 的程序。

Python 下载位置(Windows):

可以从以下位置下载 Python(适用于 Windows):

 https://www.python.org/downloads/

原文由 undetected Selenium 发布,翻译遵循 CC BY-SA 3.0 许可协议

我在 Windows 10 中使用 VS Code,这就是我解决它的方法。

你需要注意 Python 的位置(在我的例子中),

 1) C:\Users_Me_\AppData\Local\Programs\Python\Python38\

以及 Python 查找 libraries/packages 的地方,包括使用 pip 安装的库/包(同样,在我的例子中),

 2) C:\Users_Me_\AppData\Roaming\Python\Python38\

我不知道为什么这两个位置不同(必须在某个时候修复它)。 Python 似乎是从第一个位置运行的,但它在第二个位置寻找库!:/

无论如何,由于我在 Python 方面的经验有限,我只是 \Lib\site-packages 从第一个位置(包括 selenium 文件夹)复制 \site-packages 在第二个 位置希望解决问题,这有效出来找我!

如何检查那里的位置

  1. 打开 Python CLI,键入以下命令:
 which python

  1. 打开 Python CLI,输入以下命令(来自 这个答案):
 >>> import site
>>> site.USER_SITE

编辑

由于这似乎是一个临时解决方案,我卸载了 Python 并在适当的目录(默认安装目录除外)中再次重新安装它,现在 which pythonwhich pip 指向同一个文件夹!问题解决了!

原文由 khalifmahdi 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题