如果从 cmd 或 .py 文件运行,则包不起作用...... PYTHON [英] Package doesn't work if run from cmd or from the .py file.... PYTHON

查看:78
本文介绍了如果从 cmd 或 .py 文件运行,则包不起作用...... PYTHON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 python (windows) 有问题.如果我创建了一个程序(甚至是简单的)并导入了一个包,那么如果我从 python shell 运行它,它就可以工作但是如果我从 .py 文件或使用 cmd (C:\Python34\program.py) 运行它,它不会.

I noticed a problem with python (windows). If I create a program (even simple) and I import a package, it works if I run it from the python shell but if I run it from the .py file or with the cmd (C:\Python34\program.py) it doesn't.

说清楚:

程序 1

from selenium import webdriver
print("have a good day")

程序 2

import pyautogui
print("be happy")
pyautogui.moveT(300,300)

错误程序 1

Traceback (most recent calls)
 File"C:\Python34\program.py" line 1, in <module>
  from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'

错误程序 2

Traceback (most recent calls)
 File"C:\Python34\program.py" line 1, in <module>
  import pyautogui
ModuleNotFoundError: No module named 'pyautogui'

所以我不明白如果我不从 shell 运行它,它为什么不能找到/识别模块.

So I don't understand why it doesn't find/recognize the module if I doesn't run it from the shell.

推荐答案

ModuleNotFoundError 指出模块 'selenium' 和 'pyautogui' 不可用于 python 解释器(您调用的那个)来执行您的脚本).由于您尝试在 shell 解释器中执行相同的代码,问题可能是因为在您的 Windows 系统上安装了多个 python.

The ModuleNotFoundError states that the module 'selenium' and 'pyautogui' isn't available for the python interpreter (the one you invoked to execute your script with). Since you have tried executing the same code from within the shell interpreter, the problem might be because of having multiple python installations on your Windows system.

当您的系统上安装了多个版本的 python 时,您尝试使用 pip 命令安装的软件包可能不会最终驻留在python 版本你期待他们在.

When you have multiple versions of python installed on your system, then there is a possibility that the package(s) you tried installing using pip command doesn't end up residing inside the python version you were expecting them to be in.

在您执行任何操作之前,检查 pip 实用程序所指的 Python 版本:

Before you do anything, check which python version the pip utility is referring to:

$ pip --versionpip <ver>从路径/到/pip (python <版本>)

如果上面结果最后的python版本是2.x,那么说明你尝试使用pip install selenium安装的selenium包最终进入了python 2环境.

If the python version at the end of the above result is 2.x, then it means that the selenium package you tried installing using pip install selenium ended up inside python 2 environment.

但是,当您尝试执行 python 脚本时,正在调用 3.x 解释器.在这种情况下,模块将不可用.您可以执行以下操作:

However, when you try to execute the python script, the 3.x interpreter is being invoked. In which case, the module will not be available. Here's what you can do:

使用 pip3 命令安装包:

pip3 安装硒

这将安装 selenium 或任何其他包,仅在 python 3 安装中.现在您应该能够执行脚本而不会出现任何错误.pyautogui 也是如此.

This will install the selenium or any other package, inside python 3 installation only. Now you should be able to execute the script without any errors. Same goes for pyautogui.

另外,请注意在 Microsoft Windows 下,python 命令通常会调用 Python 2 shell 解释器.在这种情况下,您可以轻松地运行您的代码(需要 selenium)而不会出现任何错误.在 Windows 下还有另一个名为 py 的实用程序.您可以在此处了解更多相关信息

Also, note that under Microsoft Windows, the python command usually invokes the Python 2 shell interpreter. In which case, you were easily able to run your code (which required selenium) without any errors. There is another utility called py, under Windows. You can learn more about that here

selenium 不是预安装的软件包

selenium is not a pre-installed package

这篇关于如果从 cmd 或 .py 文件运行,则包不起作用...... PYTHON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆