Import local modules in Jupyter notebook - python

I would like to outsource some general functions useful for multiple notebooks in a module (also for testing purposes). The current directory structure looks like the following
jupyter/
├─ notebooks/
│ ├─ 01 Notebook 1.ipynb
│ ├─ ...
├─ src/
│ ├─ module_a/
│ │ ├─ __init__.py
│ │ ├─ func_a.py
│ ├─ module_b/...
├─ tests/...
├─ data/...
├─ .../
In func_a.py, there is a simple function def print_a(): print('a')
However, when I would like to import and use module_a in 01 Notebook 1.ipynb by using (what I think makes sense)
from .. src.module_a import print_a
I got an ImportError: attempted relative import with no known parent package. What am I doing wrong? I am using Python 3.9.

I would try to append the src directory to the system path like so:
import sys
sys.path.append("/path/to/your/src")
from src.module_a import a
please note that you can use the relative path from your notebook root and not absolute path like in the example above, so the following:
sys.path.append("src")
should work too

Related

Cannot import from module inside the same package, from a subpackage

I have the following structure
folder/
├─ subfolder/
│ ├─ __init__.py
│ ├─ script.py
├─ __init__.py
├─ module.py
Inside script.py I want to import the function my_function from module.py. I have tried various variants
from ..module import my_function
from ...folder.module import my_function
from .. import module # and then use module.my_function
from ... import folder.module # and then use folder.module.my_function
However, whenever I am in the terminal inside folder and run python3 subfolder/script.py I get the error
ImportError: attempted relative import with no known parent package

Pytest location issues

I have one problem with pytest. I am creating objects and I like to test these objects if they are complete or buggy. Now the thing: When I put the test script into the src-folder, it works fine. But when I want to use it in a tests folder, I get always an error, that if I like to load my object, a module is not found.
in this structure it works:
my-app/
├─ output/
├─ input/
├─ src/
│ ├─ some_package/
│ ├─ some_code.py
│ ├─ test.py
This structure gives me a ModuleNotFoundError:
my-app/
├─ output/
├─ input/
├─ src/
│ ├─ some_package/
│ ├─ some_code.py
├─ test/
│ ├─ test.py (with changed the relative paths)
I guess that pytest has some problems with using the correct path.

import function from another folder's file

My project directory looks like this:
project/
├─ new/
│ ├─ test.py
├─ docs.py
├─ main.py
Within my main.py, I import a function from docs.pylike this:
from docs import get_my_conn
and it works fine.
How can I import the same thing within new/test.py as well? I tried:
from ..docs import get_my_conn
but I get this error:
ImportError: attempted relative import with no known parent package
What you need to do is initialize the new directory as a package. In order to do this, inside the new directory make an empty file titled __init__.py. After you do this, go back to your main.py code and import it like this instead:
from new.test import (function)
Your new tree should look like this:
project/
├─ new/
│ ├─ test.py
| ├─ __init__.py
├─ docs.py
├─ main.py
P.S.
If you are trying to import a function from docs.py into test.py, you probably should not do this. This is because it will result in an error known as a circular import. This will cause your script to no longer work. If you want to import a function from docs.py into test.py then put them in the same directory (or directory at the same level of the project hierarchy).
The issue is in how you are running your code - you should add the init files as in
project/
├─ new/
│ ├─ test.py
├─ __init__.py
├─ docs.py
├─ main.py
then run your code as
python -m new.test # note no py
from the project folder.

Specify path for import modules separately for each file

I have to reuse a code from another project, that I don't want to modify
project1/
├─ src/
│ ├─ utils.py
├─ experiments/
│ ├─ myexperiment.py
project2/
├─ utils.py
├─ experiment.py
I'm running file experiment.py
$ python experiments/myexperiment.py
File project1/experiments/myexperiment.py contains
...
sys.path.append('../src')
from utils import func1
sys.path.pop()
sys.path.append(<path_to_project2>)
from experiment import MyExperiment
The problem is, that file project2/experiment.py contains
...
from utils import func2
The problem is that when I do
$ python experiments/myexperiment.py
import in project2/experiment.py tries to search function func2 in project1/src/utils.py instead of project2/utils.py. I've double-checked, that at this moment the path to project1/src/utils.py already removed from sys.path.

ModuleNotFoundError: No module named 'SLCT'

English is not my mother tongue, so there might be some grammatical errors in my question.
Sorry about that.I git clone a project from github to my VScode. When I wanted to run demo code, a "ModuleNotFoundError" occured. I was confused about this error. Because I checked module and it did exit, I also haven't install same name module before. Here is the project-tree of the project.(Only parts including "SLCT" are given)
source code
└─ logparser_root
├─ benchmark
│ ├─ SLCT_benchmark.py
├─ demo
│ ├─ SLCT_demo.py
├─ logparser
│ ├─ SLCT
│ │ ├─ cslct.c
│ │ ├─ cslct.h
│ │ ├─ README.md
│ │ ├─ SLCT.py
│ │ ├─ __init__.py
│ │ └─ __pycache__
│ │ └─ __init__.cpython-39.pyc
"SLCT_demo.py" import SLCT from logparser.
import sys
import os
# sys.path.append('../')
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# sys.path.append('../logparser//SLCT//SLCT.py')
# print(sys.path)
from logparser import SLCT
When I ran "SLCT_demo.py", this error occured.
Traceback (most recent call last):
File "c:\Users\57142\Desktop\Project files\source code\logparser_root\demo\SLCT_demo.py", line 9, in <module>
from logparser import SLCT
File "c:\Users\57142\Desktop\Project files\source code\logparser_root\logparser\SLCT\__init__.py", line 1, in <module>
from SLCT import *
ModuleNotFoundError: No module named 'SLCT'
Here is "init.py" of "SLCT".from SLCT import *
Thanks for spending time on my question. Have a nice day!
In order to run from SLCT import * inside file x.py, you need to have the following directory structure:
File x.py
Folder SLCT
- File __init__.py
In your case, you are trying to run from SLCT import * inside file __init__.py, which means that you need to have the following directory structure:
File __init__.py
Folder SLCT
- File __init__.py
And in your case, since this whole thing is already inside a folder named SLCT, you're probably caught in your own confusion.
In short, it sounds like you want to simply move the entire contents of file SLCT.py into file __init__.py (although it's hard to say for sure, because you haven't made the rest of your code visible).

Categories