解释器可以找到第三方模块,但WIN-R和命令行不能

时间:2016-10-17 12:17:17

标签: python module

我已经使用pyperclip在我的\python31\Lib\site-packages文件夹中安装了一个名为pip install --target的第三方模块,因为我在计算机上也有python 2.7,并且看起来模块放在2.7中作为默认值

我可以通过IDLE运行下面的脚本就好了。

import pyperclip
import shelve
import sys
import os

curdir = os.getcwd()
mc_shelf = shelve.open(os.path.join(curdir, "mcb2"))

# save clipboard content
if len(sys.argv) == 3 and sys.argv[1].lower() == "save":
    mc_shelf[sys.argv[2]] = pyperclip.paste()
    print("Saved contents of clipboard to keyword \"{}\".".format(sys.argv[2]))
elif len(sys.argv) == 2:
    keyword = sys.argv[1].lower()
    if keyword == "list":
        pyperclip.copy(", ".join(keyword for keyword in mc_shelf))
        print("Copied list to clipboard.")
    elif keyword in mc_shelf:
        pyperclip.copy(mc_shelf[keyword])
        print("Pasted text to clipboard.")
mc_shelf.close()

然而,使用WIN-R /命令行,我得到一个ImportError,例如:

PS C:\users\dave\Desktop\2016Coding\AutomateBoring\8-RWFiles> py.exe mcb2.pyw save dave
py.exe : Traceback (most recent call last):
At line:1 char:1
+ py.exe mcb2.pyw save dave
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Traceback (most recent call last)::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

  File "mcb2.pyw", line 7, in <module>
    import pyperclip
ImportError: No module named 'pyperclip'

这里究竟会发生什么?它与每个应用程序使用的路径有关吗?我已经设置了环境变量,并在系统变量中设置了pythonpath(这尤其导致了一个不同的错误),但说实话,我很困惑。

尝试使用pip3进行安装:

PS C:\> pip install pyperclip
Requirement already satisfied (use --upgrade to upgrade): pyperclip in c:\python27\lib\site-packages

PS C:\> pip3 install pyperclip
pip3 : The term 'pip3' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At line:1 char:1
+ pip3 install pyperclip
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (pip3:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

2 个答案:

答案 0 :(得分:1)

如果要在Python3环境中安装软件包,则需要使用pip3安装软件包。 IE

pip3 install pyperclip
  

我已使用pip install --target

将名为pyperclip的第三方模块安装到我的\ python31 \ Lib \ site-packages文件夹中

您在这里有效地做的是使用Python的Python版本来尝试安装Python3包。您的Python3环境有自己的pip版本 - 如果您的Python3环境也在您的PATH中,您可以通过pip3

为Python3调用pip

答案 1 :(得分:0)

看来问题可能是由我的python版本(3.1.1)引起的。我确实遇到过关于pip和python 3.1.1的bug的讨论,但是没有我害怕的链接。

升级到python 3.5.2后,所有问题现在都已解决,我可以从Windows和命令行运行脚本,根据需要导入第三方模块,没有问题。

P.S我现在也可以访问“pip3”。