简体   繁体   中英

Python 3 cannot find a module

I am unable to install a module called 'jieba' in Python 3 which is running in Jupyter Notebook 6.0.0. I keep getting ModuleNotFoundError: No module named 'jieba' after trying these methods:

1. import jieba
2. pip3 install jieba

Can anyone help? Thank you.

pip3 in the terminal is almost certainly installing your package into a different Python installation.

Rather than have you hunt around for the right installation, you can use Python and Jupyter themselves to ensure you are using the correct Python binary. This relies on three tricks:

  • You can execute the pip command-line tool as a module by running python -m pip ... . This uses the pip module installed for the python command, so you don't have to verify what python installation the pip3 command is tied to.

  • You can get the path of the current Python interpreter with the sys.executable attribute .

  • You can execute shell commands from Jupyter notebooks by prefixing the shell command with ! , and you can insert values generated with Python code with {expression}

You can combine these to run pip (to install packages or run other commands) against the current Python installation , from Jupyter itself, by adding this into a cell:

import sys
!{sys.executable} -m pip <pip command line options>

To install your jieba package, that makes:

import sys
!{sys.executable} -m pip install jieba

If you are using Anaconda, then you could also install the conda package for jieba ; the package does not require any platform-specific dependencies or compilation, but it may be more convenient for you or necessary to install other packages that do have such requirements and have pre-compiled conda packages.

In that case, tell the conda command about your Python executable:

import sys
!conda install --yes --prefix {sys.prefix} <package name>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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