'Pycharm Jupyter Notebook `PYTHONPATH`?
System information
- Mac OS X 10.12 Sierra
- Pycharm 2016.2.3
- Python 3.5.1
- Jupyter 4.1.0
Question
When I'm using Jupyter Notebook under Pycharm, it doesn't seem to include my current working directory, or the source root directory, in the PYTHONPATH
of the currently active Jupyter Notebook. This is unlike the behaviour of ordinary .py
files, for which Pycharm automatically includes its current directory and the project source root directory in the PYTHONPATH
. So, is it possible to set Pycharm to automatically include the project source root directory in the PYTHONPATH
for Jupyter Notebooks run under Pycharm?
Solution 1:[1]
It turns out, in a past endeavour, I modified my jupyter_notebook_config.py
in %USER%\.jupyter
folder. Looked at lines as follows:
## The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = 'D:\\Works\\GitHub\\Jupyter\\'
The line under the ##
, indicates the override path, evidently.
When I commented it out...Pycharm all of the sudden successfully launched with pwd of the file (location of the .ipynb). good luck, hth.
Solution 2:[2]
Pycharm 2019.1 has done an overhaul of Jupyter support. Jetbrains says this in reference to Pycharm 2019.1: "Jupyter Notebook support in PyCharm was redesigned from the ground up."
So, as of Pycharm 2019.1, you can now set Pycharm to include the project source root directory for Jupyter Notebooks.
See this page of Pycharm 2019.1 help.
Solution 3:[3]
There's a bunch of ways to mess with the environment, but the PyCharm specific way that worked for me was to make the directory with my source in it a PyCharm "sources root" as described here: https://www.jetbrains.com/help/pycharm/content-root.html
This can be done from the context menu of a directory in the navigator, or from the "Project Structure" settings pane.
This seems to update the PYTHONPATH as well as sorting out things like autocomplete.
Solution 4:[4]
For anyone using the free version of PyCharm, which has no direct Jupyter Notebook support, there's a simple workaround:
- Go to PyCharm "Terminal" tab at the bottom (NOT to be confused with the Python console tab!)
[NOTE: steps 2 and 3 are for Window7; on other OS's, just research how to set environment variables thru the terminal or command prompt]
Issue the command:
set PYTHONPATH=\path_to_your_project_root_folder\
EXAMPLE on Windows:
set PYTHONPATH=\Docs\- MY CODE\my_project_root_folder\
Optionally, you may verify that the environmental variable got set by issuing the command:
set PYTHONPATH
Issue the command:
jupyter notebook
That's all! A new browser tab will open up... and, insider any notebook, you can verify the path by issuing the python commands:
import sys
sys.path
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LoneWanderer |
Solution 2 | geekandglitter |
Solution 3 | Nathan Buesgens |
Solution 4 | Julian - BrainAnnex.org |