0

Using a Jupyter Notebook running with the python2 kernel, I tried to import emcee and I got an error message:

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

apparently because I am using the python2 kernel.

I then installed ipykernal using: python3 -m pip install ipykernel

I then opened a Jupyter Notebook using the Python3 kernel. I again tried to import emcee but I had the same problem. It was still looking for the emcee module in the python2 path.

I then did pip3 install emcee, opened a Jupyter Notebook (python3 kernel) but I still have the same issue.

I think I need to tell Jupyter Notebook to look for the python3 version of emcee, but I'm not sure how to. I just tried:

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

But again, this didn't fix it. I checked the sys.path in my Jupyter Notebook and the new python path didn't seem to be added.

Can someone tell me what I'm doing wrong please?

user1551817
  • 6,693
  • 22
  • 72
  • 109

2 Answers2

1

Here the issue is not from your side, this issue is mainly from there side. They,there refers to emcee.
Explination:
They are basically using annotation here where you are getting error, and you are using python2 to import this library, which don't support annotation.

Here's a bit of information about annotation(Some of the stackoverflow question).

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.

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.

Now if jupyter notebook is not using python3 then the solution is 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.

imxitiz
  • 3,920
  • 3
  • 9
  • 33
0

The Jupyter Notebook wasn't really using the python3 kernel even though it said it was:

My python3 kernel was located: /home/damejia/.local/share/jupyter/kernels/python3

The kernel.json file was:

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

But "python" just points to my python2. I changed "python" to "python3" and everything worked.

user1551817
  • 6,693
  • 22
  • 72
  • 109