简体   繁体   中英

How to import modules into Jupyter Notebook kernel

I am having problems installing modules and then importing them into specific Jupyter Notebook kernels. I want to install them directly into the kernel as opposed to throughout anaconda to separate dependencies in projects. Here is how the problem goes:

  • I firstly want a package, for example, nltk
  • I navigate to and activate the conda environment (called python3) and run 'conda install nltk'
  • I then load that environment into Jupyter using ipykernel with the command 'python -m ipykernel install --user --name python3'
  • When trying to import the package into the notebook it tells me that it cannot be found

I have been struggling with this for a while. Where am I going wrong? I greatly appreciate all the help.

NOTE: I have somehow managed to install and import many packages into notebooks using the aforementioned process. I'd really like a method to do this in a foolproof manner.

Not entirely clear where things go wrong, but perhaps clarifying some of the terminology could help:

  • " navigate to...the conda environment " - navigating has zero effect on anything. Most end-users should never enter or directly write to any environment directories.

  • " ...and activate the conda environment " - activation is unnecessary - a more robust installation command is always to use a -n,--name argument:

     conda install -n python3 nltk

    This is more robust because it is not context-sensitive, ie, it doesn't matter what (if any) environment is currently activated.

  • " load that environment into Jupyter using ipykernel " - that command registers the environment as a kernel at a user-level. That only ever needs to be run once per kernel - not after each new package installation. Loading the kernel happens when you are creating (or changing the settings of) a notebook. That is, you choose the kernel in the Jupyter GUI.

    Even better, keep jupyter in a dedicated environment with an installation of nb_conda_kernels and Jupyter (launched from that dedicated environment) will auto-discover all Conda environments that have valid kernels installed (eg, ipykernel , r-irkernel ).

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