我在 python 脚本中从 conda 导入包时出错了吗?

Am I importing a package from conda wrong in my python script?

嘿哟! 我知道这可能看起来像 的重复问题,但有一点不同。我想导入整个包,而不仅仅是一个或一组函数。当我从 Anaconda 导入包并尝试将它们导入 python 脚本时,我得到 ErrorModuleNotFound。我可能输入错误,所以这里是信息:

我的 Python 脚本包含的内容:

import wikipedia
print (wikipedia.summary("Windows OS"))
win = wikipedia.page("New York")
win.content

我得到的错误:

================== RESTART: C:\Users\aglin\Desktop\test.py ==================
Traceback (most recent call last):
  File "C:\Users\aglin\Desktop\test.py", line 1, in <module>
    import wikipedia
ModuleNotFoundError: No module named 'wikipedia'

我的问题: 如何导入包 wikipedia 而不会出现此错误?如果有帮助,可以找到"Wikipedia API for Python's Documentation"here。另外,我是 Python 的新手,所以如果我需要做任何具体的事情或查看文档,请告诉我。谢谢! :D

以下是在 conda 中创建环境的常规步骤,将 Python 版本放入其中,然后将库导入其中。

步骤是:

  • conda create --name Py36 python=3.6
  • 激活 Py36
  • pip 安装维基百科

您会注意到,在显示的最后,可以打开 Python 解释器并导入维基百科。


Microsoft Windows [Version 10.0.16299.125]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\scratch>conda create --name Py36 python=3.6
Fetching package metadata .............
Solving package specifications: .

Package plan for installation in environment C:\ProgramData\Miniconda3\envs\Py36:

The following NEW packages will be INSTALLED:

    certifi:        2017.11.5-py36hb8ac631_0
    pip:            9.0.1-py36h226ae91_4
    python:         3.6.3-h3b118a2_4
    setuptools:     36.5.0-py36h65f9e6e_0
    vc:             14-h2379b0c_2
    vs2015_runtime: 14.0.25123-hd4c4e62_2
    wheel:          0.30.0-py36h6c3ec14_1
    wincertstore:   0.2-py36h7fe50ca_0

Proceed ([y]/n)? y

#
# To activate this environment, use:
# > activate Py36
#
# To deactivate an active environment, use:
# > deactivate
#
# * for power-users using bash, you must source
#


C:\scratch>activate Py36

(Py36) C:\scratch>pip install wikipedia
Collecting wikipedia
Collecting beautifulsoup4 (from wikipedia)
  Using cached beautifulsoup4-4.6.0-py3-none-any.whl
Collecting requests<3.0.0,>=2.0.0 (from wikipedia)
  Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests<3.0.0,>=2.0.0->wikipedia)
  Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests<3.0.0,>=2.0.0->wikipedia)
  Using cached idna-2.6-py2.py3-none-any.whl
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\miniconda3\envs\py36\lib\site-packages (from requests<3.0.0,>=2.0.0->wikipedia)
Collecting chardet<3.1.0,>=3.0.2 (from requests<3.0.0,>=2.0.0->wikipedia)
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Installing collected packages: beautifulsoup4, urllib3, idna, chardet, requests, wikipedia
Successfully installed beautifulsoup4-4.6.0 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 wikipedia-1.4.0

(Py36) C:\scratch>python
Python 3.6.3 |Anaconda, Inc.| (default, Nov  8 2017, 15:10:56) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wikipedia
>>> ^Z


(Py36) C:\scratch>