如何让 Jupyter Notebook 在模块的正确位置显示?

How to make Jupyter Notebook look in the right place for a module?

使用 Jupyter Notebook 运行 python2 内核,我尝试导入 emcee 并收到一条错误消息:

File "/home/me/.local/lib/python2.7/site-packages/emcee/ensemble.py", line 84
    parameter_names: Optional[Union[Dict[str, int], List[str]]] = None,
                   ^
SyntaxError: invalid syntax

显然是因为我使用的是 python2 内核。

然后我安装了 ipykernal 使用:python3 -m pip install ipykernel

然后我使用 Python3 内核打开了一个 Jupyter Notebook。我再次尝试 import emcee 但我遇到了同样的问题。它仍在 python2 路径中寻找 emcee 模块。

然后我做了 pip3 install emcee,打开了一个 Jupyter Notebook(python3 内核),但我仍然遇到同样的问题。

我想我需要让 Jupyter Notebook 寻找 python3 版本的主持人,但我不确定如何去做。我刚试过:

export PYTHONPATH='/home/me/.local/lib/python3.6/site-packages/'

但同样,这并没有解决问题。我检查了我的 Jupyter Notebook 中的 sys.path,新的 python 路径似乎没有被添加。

有人可以告诉我我做错了什么吗?

这里的问题不是你这边的,这个问题主要是那边的。他们,那里指的是emcee.
解释:
他们基本上是在您遇到错误的地方使用 annotation here,并且您正在使用 python2 导入此库,它不支持 annotation.

这里有一些关于注解的信息(一些Whosebug的问题)。

primes: List[int] = [] and stats: Dict[str, int] = {}. Everything between : and the = is a type hint, so primes is indeed defined as List[int], and initially set to an empty list (and stats is an empty dictionary initially, defined as Dict[str, int]).

It's a function annotation; function arguments and the return value can be tagged with arbitrary Python expressions. Python itself ignores the annotation (other than saving it), but third-party tools can make use of them.

  • How can I use function annotation in python2.7

You can't use the new (3.5+) annotation syntax directly in 2.7, but if you have python 3.4+ you can install mypy and run it on your 2.7 code with a different syntax. See PEP 484.

现在如果 jupyter notebook 没有使用 python3 那么解决方案是 create virtual environment and try running there. You may add your issue in new issue they will help but if you know what you are doing then you may change that code too. If your edited code works for both python2 and 3 then you may pull request.

Jupyter Notebook 并没有真正使用 python3 内核,尽管它说它是:

我的 python3 内核位于:/home/damejia/.local/share/jupyter/kernels/python3

kernel.json 文件是:

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

但是“python”只是指向我的 python2。我将“python”更改为“python3”,一切正常。