Jupyter notebook set project dependency - python

I have two different project folders. I want to make one of the projects a root so that when I import something from first project in the second project it does not show ModuleNotFoundError. For example, In PyCharm we can set project dependencies. Is there way to do so in Jupyter Notebook?

The best way to handle this in notebooks or jupyterlab would be to set the PYTHONPATH environment variable. For example, on MAC or unix-based OS, using bash, do this:
export PYTHONPATH="${PYTHONPATH}:/path/to/project"
Then re-load the notebook. Adding your project path to the PYTHONPATH variable will allow you to import any modules that are specified in this variable.
Additionally, you can set this variable in a jupyter notebook itself by entering the bash command into a notebook cell, prepended with the ! symbol like this:
! export PYTHONPATH="${PYTHONPATH}:/path/to/project"
Then executing the cell.

Related

Creating multiple Jupyter working directories/config files in Anaconda

I have Anaconda Navigator on my work computer and I've changed the default working directory for Jupyter notebooks to be a certain location on the firm server, using the steps given here
I have also created a second environment in Anaconda, for which I would like to use a different Jupyter Notebook working directory than that of the base (root). To do this, I believe I would need to:
Create a second Jupyter Notebook config file
Get the second environment to refer to the new config file, while ensuring that the old file still referred to the original config file.
How would I go about this? Alternate approaches to creating multiple working directories also welcome.
Was looking to achieve the same result: pass different configurations which would include specific settings such as working and workspace directories when launching Jupyter Notebook/Lab in different conda or other virtual environments.
Noticing the following:
jupyter lab --help
--config=<Unicode>
Full path of a config file.
Default: ''
Equivalent to: [--JupyterApp.config_file]
Thus, to achieve the desired result, it is possible to pass the path to the relevant config file upon launching Jupyter Lab/Notebook as such:
jupyter lab --config=~/.jupyter/path_to_my_custom_jupyter_config_for_env_1.py
# or
jupyter notebook --config=~/.jupyter/path_to_my_custom_jupyter_config_for_env_1.py
You can copy your current configuration file, make the relevant adjustments and save it as a different file. You would then pass the path to that new config file when launching Jupyter as per above. To simplify, you can create shortcuts that do that (instead of typing/copying the specific parameters on each launch).
Otherwise (with no specific argument passed), Jupyter Lab/Notebook will launch using the default configuration file if it exists, which is always located in the home directory of the Unix user launching Jupyter (~/.jupyter/jupyter_notebook_config.py).

Import error: module object detection not found

When i try run code into Jupyter notebook i getting Import error(attached image).
I add paths to PYTHON_PATH and add %PYTHON_PATH% in system PATH, but i still get thos error
If you are using Anaconda, you must know that it ignores PYTHONPATH!. Use the following commands:
conda develop ~/models/research/
conda develop ~/models/research/slim/
here is why you need to do it in this way. When you issue the above commands, it will create a .pth file inside your current's environment site-packages folder. Then, adds those two paths to this .pth file. Then, whenever you load your Anaconda prompt, those are on the path. This works for both Linux and Windows.

Jupyter file path change to project location

I have experience in RStudio and Visual Studio and new to Python environment, I am struggling to set Python project in Jupyter like above two IDE. Here when I open new notebook then it create it as default location and I am not finding a way to put the file in my project location, Just want to know how to make production ready project like folder structure in Jupyter.
If this post is duplicate then I am not able to search the original one.
Under Linux, specify which directory Jupyter notebook uses to locate the notebooks with the command:
jupyter notebook --notebook-dir=<path to notebook location>
I have this aliased in my .bashrc file to jupytern.
In Windows I created a desktop shortcut and specified the notebook directory as:
C:\python36\Scripts\jupyter-notebook.exe --notebook-dir=<path to notebook locations>
You need to change the path to the jupyter-notebook.exe file to where it is on your installation.

I cannot use jupyter notebooks in pycharm

I am using pycharm with a virtual environment set in preferences. That environment has installed python 2.7.13. I have installed anaconda in the virtual environment, which contains jupyter. I had the professional version but have allowed the subscription to lapse.
According to https://www.jetbrains.com/help/pycharm/2016.3/using-ipython-jupyter-notebook-with-pycharm.html I can create a notebook by opening the project window and using command-N, When I do this, I CAN select jupiter notebook but there is no way to enter the filename that they require.
Trying it another way, I use File->New->Python File and, for the name use test.ipynb. Pycharm accepts it but puts on a '.py' suffix, treating it as a normal python code file: test.ipynb.py.
Any ideas on how I get this to work?
I think u should try File->New->Jupyter Notebook, this works for me.

How to place custom Jupyter kernels inside virtual environment?

I have a custom Jupyter kernel which runs IPython using a custom IPython profile which uses a matplotlib stylesheet.
I know to run this successfully normally I would put:
The matplotlib stylesheet in ~/.config/matplotlib/stylelib/
The IPython profile in ~/.ipython/
The kernel json in ~/.jupyter/kernels/my_kernel/
But I am doing this as part of larger program which runs in a virtualenv, and if I put the things as above then any notebook server running on the computer will be able to see the custom kernels, even if it is running outside the venv. I don't what this because I don't want my program to interfere with other notebooks on the computer.
I think what I need to do is put the things above somewhere equivalent inside the venv but I can't figure out where they should go. Doe anyone know where they would go? Or is this just a thing IPython/Jupiter can't/won't do?
It's probably worth mentioning that in the case of the stylesheet for example I don't want to just put it in the working directory of my program (which is one option matplotlib offers).
You can put kernelspecs in VIRTUAL_ENV/share/jupyter/kernels/ and they will be made available if the notebook server is running in that env. In general, <sys.prefix>/share/jupyter/kernels is included in the path to look for kernelspecs.
You can see the various locations Jupyter will look, you can see the output of jupyter --paths:
$ jupyter --paths
config:
/Users/you/.jupyter
/Users/you/env/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/Users/you/Library/Jupyter
/Users/you/env/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/Users/you/Library/Jupyter/runtime
Kernelspecs are considered data files, and will be found in any of those directories listed under data:, in a kernels subdirectory, e.g. /usr/local/share/jupyter/kernels.

Categories

Resources