简体   繁体   English

Python:没有名为 selenium 的模块

[英]Python : no module named selenium

After searching the web for hours i didnt yet find an answer to my problem.在 web 上搜索了几个小时后,我还没有找到问题的答案。 I am using Python 3.6 and i cant import selenium. I always get the message "No module named 'selenium'' I tried everything, i first downloaded selenium from this website https://pypi.python.org/pypi/selenium/3.6.0 .我正在使用 Python 3.6,我无法导入 selenium。我总是收到消息“没有名为‘selenium’的模块”我尝试了一切,我首先从这个网站https://pypi.python.org/pypi/6selenium/3. .0

Then I tried python -m pip install -U selenium and didnt work either.然后我尝试了 python -m pip install -U selenium 也没有用。 I tried some other things that people said but they didnt work either.我尝试了人们所说的其他一些方法,但它们也没有用。 Im using windows 10. Any help?我正在使用 windows 10. 有帮助吗?

As you mentioned you are using Python 3.6 following the steps :正如您所提到的,您正在按照以下步骤using Python 3.6

  • Open Command Line Interface ( CLI ) and issue the command python to check if Python is properly installed :打开Command Line Interface ( CLI ) 并发出命令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. >>>
  • Ensure pip is working properly :确保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.
  • Install latest selenium through pip :通过pip安装最新的selenium

     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
  • Confirm that Selenium is installed :确认已安装Selenium

     C:\\Users\\username>pip freeze selenium==3.8.1
  • Open an IDE (eg Eclipse , PyCharm ) and write a simple program as follows :打开一个IDE (例如EclipsePyCharm )并编写一个简单的程序如下:

     from selenium import webdriver driver = webdriver.Firefox(executable_path="C:\\\\path\\\\to\\\\geckodriver.exe") driver.get('https://stackoverflow.com')
  • Execute the program on which Firefox Quantum Browser will be initiated and the url https://stackoverflow.com will be accessed.执行启动Firefox Quantum Browser 的程序,访问url https://stackoverflow.com


Python Download Location (Windows) : Python 下载位置(Windows):

Python (for Windows) can be download from the following location : Python(适用于 Windows)可以从以下位置下载:

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

I'm on VS Code in Windows 10, and here's how I solved it.我在 Windows 10 中使用 VS Code,这是我解决它的方法。

You need to pay attention to where the Python is located (in my case),您需要注意Python所在的位置(就我而言),

1) C:\Users\_Me_\AppData\Local\Programs\Python\Python38\ 

and where the Python looks for libraries/packages , including the ones installed using pip (again, in my case),以及Python 查找库/包的位置,包括使用pip安装的库/包(同样,在我的情况下),

2) C:\Users\_Me_\AppData\Roaming\Python\Python38\

I don't know why these two locations are different (gotta fix it at some point).我不知道为什么这两个位置不同(必须在某个时候修复它)。 It seemed that Python was running from the first location, but it was looking for libraries in the second!:/ Python 似乎从第一个位置运行,但它正在第二个位置寻找库!:/

Anyway, since I have limited experience in Python , I just copied the \\Lib\\site-packages from the first location (including selenium folders) to \\site-packages in second one in hopes of solving the issue, which worked out for me!无论如何,由于我在 Python 方面的经验有限,我只是\\Lib\\site-packages从第一个位置(包括 selenium 文件夹)复制到第二个位置的\\site-packages以希望解决这个问题,这对我有用!

How to check for there locations如何检查那里的位置

1) Open Python CLI, typed the following command: 1)打开Python CLI,输入以下命令:

which python

2) Open Python CLI, typed the following commands (From this answer ): 2)打开Python CLI,输入以下命令(来自这个答案):

>>> import site
>>> site.USER_SITE

EDIT编辑

Since this seems a temporary solution, I uninstalled Python and reinstalled it again in a proper directory (other than the default install directory), and now which python and which pip point to the same folder!由于这似乎是一个临时解决方案,我卸载了 Python 并在适当的目录(默认安装目录除外)中重新安装了它,现在which pythonwhich pip指向同一个文件夹! Problem solved!问题解决了!

If these solutions did not work for you, try looking into which Python interpreter your IDE is using (in my case I was using VSCode - you can find the interpreter in the bottom bar).如果这些解决方案对您不起作用,请尝试查看您的 IDE 使用的 Python 解释器(在我的情况下,我使用的是 VSCode - 您可以在底部栏中找到解释器)。 Worked for me.为我工作。

Easiest way is to copy all files of " venv " Lib, Scripts, Selenium and other folder into your main project folder.最简单的方法是将“ venv ”库、脚本、Selenium 和其他文件夹的所有文件复制到您的主项目文件夹中。 This issue occur as pycharm directly take from virtual environment venv.此问题发生在 pycharm 直接从虚拟环境 venv 中获取。

Hope this will resolve your problem:)希望这会解决您的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM