Python 帮助中列出的模块与 pip 找到的不同

Python Modules listed in help differ from what pip is finding

我在让我的代码找到我的 beautifulsoup4 模块时遇到了一些麻烦。 当列出我安装的模块时,python 给出了我在下面提供的列表(没有提到 beatifulsoup4 或 bs4)

但是,当我 运行 为模块安装 pip 时,我得到了如下所示的输出

如果有人能解释这个差异and/or提供我的文件找到这个模块所必需的代码,那就太好了。

(如果有帮助:我的代码在Home/username/Desktop/mycode,我的模块安装在Home/username/Desktop/python2.7/dist-packages/bs4

pip install beautifulsoup4
Requirement already satisfied (use --upgrade to upgrade): beautifulsoup4 in /usr/lib/python2.7/dist-packages
Cleaning up...

模块:

>>> help('modules')

Please wait a moment while I gather a list of all available modules...

BaseHTTPServer      asynchat            imaplib             sha
Bastion             asyncore            imghdr              shelve
CDROM               atexit              imp                 shlex
CGIHTTPServer       audiodev            importlib           shutil
Canvas              audioop             imputil             signal
ConfigParser        base64              inspect             site
Cookie              bdb                 io                  smtpd
DLFCN               binascii            itertools           smtplib
Dialog              binhex              json                sndhdr
DocXMLRPCServer     bisect              keyword             socket
FileDialog          bsddb               lib2to3             spwd
FixTk               bz2                 linecache           sqlite3
HTMLParser          cPickle             linuxaudiodev       sre
IN                  cProfile            locale              sre_compile
MimeWriter          cStringIO           logging             sre_constants
Queue               calendar            macpath             sre_parse
ScrolledText        cgi                 macurl2path         ssl
SimpleDialog        cgitb               mailbox             stat
SimpleHTTPServer    chunk               mailcap             statvfs
SimpleXMLRPCServer  cmath               markupbase          string
SocketServer        cmd                 marshal             stringold
StringIO            code                math                stringprep
TYPES               codecs              md5                 strop
Tix                 codeop              mhlib               struct
Tkconstants         collections         mimetools           subprocess
Tkdnd               colorsys            mimetypes           sunau
Tkinter             commands            mimify              sunaudio
UserDict            compileall          mmap                symbol
UserList            compiler            modulefinder        symtable
UserString          contextlib          multifile           sys
_LWPCookieJar       cookielib           multiprocessing     sysconfig
_MozillaCookieJar   copy                mutex               syslog
__builtin__         copy_reg            netrc               tabnanny
__future__          crypt               new                 tarfile
_abcoll             csv                 nis                 telnetlib
_ast                ctypes              nntplib             tempfile
_bisect             curses              ntpath              termios
_codecs             datetime            nturl2path          test
_codecs_cn          dbhash              numbers             textwrap
_codecs_hk          dbm                 opcode              this
_codecs_iso2022     decimal             operator            thread
_codecs_jp          difflib             optparse            threading
_codecs_kr          dircache            os                  time
_codecs_tw          dis                 os2emxpath          timeit
_collections        distutils           ossaudiodev         tkColorChooser
_csv                dl                  parser              tkCommonDialog
_ctypes             doctest             pdb                 tkFileDialog
_ctypes_test        dumbdbm             pickle              tkFont
_elementtree        dummy_thread        pickletools         tkMessageBox
_functools          dummy_threading     pipes               tkSimpleDialog
_hashlib            email               pkgutil             toaiff
_heapq              encodings           platform            token
_hotshot            errno               plistlib            tokenize
_io                 exceptions          popen2              trace
_json               fcntl               poplib              traceback
_locale             filecmp             posix               ttk
_lsprof             fileinput           posixfile           tty
_multibytecodec     fnmatch             posixpath           turtle
_multiprocessing    formatter           pprint              types
_osx_support        fpformat            profile             unicodedata
_pyio               fractions           pstats              unittest
_random             ftplib              pty                 urllib
_socket             functools           pwd                 urllib2
_sqlite3            future_builtins     py_compile          urlparse
_sre                gc                  pyclbr              user
_ssl                gdbm                pydoc               uu
_strptime           genericpath         pydoc_data          uuid
_struct             getopt              pyexpat             warnings
_symtable           getpass             quopri              wave
_sysconfigdata      gettext             random              weakref
_testcapi           glob                re                  webbrowser
_threading_local    grp                 readline            whichdb
_tkinter            gzip                repr                wsgiref
_warnings           hashlib             resource            xdrlib
_weakref            heapq               rexec               xml
_weakrefset         hmac                rfc822              xmllib
abc                 hotshot             rlcompleter         xmlrpclib
aifc                htmlentitydefs      robotparser         xxsubtype
antigravity         htmllib             runpy               zipfile
anydbm              httplib             sched               zipimport
argparse            idlelib             select              zlib
array               ihooks              sets                
ast                 imageop             sgmllib             

如果这有帮助,我的 PYTHONPATH 是:

['/home/username/Desktop/mycode', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/plat-linux2', '/usr/local/lib/python2.7/lib-tk', '/usr/local/lib/python2.7/lib-old', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/site-packages']

这是通过 运行 宁这个程序发现的:

import sys
print sys.path

似乎 beautifulsoup 安装到不在您的 PYTHONPATH 上的目录中。试试这个临时解决方案:

sys.path.append('/usr/lib/python2.7/dist-packages')
help('modules')

您现在应该会看到 bs4 已列出并且可用。请记住,这不是永久性的解决方案——您需要将该目录添加到您的 PYTHONPATH 环境变量中,或者将 beautifulsoup 安装在您的 PYTHONPATH 中已有的目录中。