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
Related
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.
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.
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
I want to import modules from folder and subfolder with Python 3.6.
Currently I have a structure like this.
└── project
│ main.py
├── package1
│ ├── __init__.py
│ └── module1.py
└── package2
├── __init__.py
├── module2.py
When I import module1 and module2, I have no problem with that.
Within main.py having this scripts to import both modules.
from package1 import module1
from package2 import module2
It works fine!
But I want to move package2 with module2 into a subfolder under package1 as shown below:
└── project
│ main.py
└── package1
├── __init__.py
├── module1.py
└── subpackage1
├── __init__.py
├── module2.py
And want to call module2 from main.py. Having tried following path and scripts does not fix my problem I get import error, it can not find the path.
Within main.py having this scripts to import both modules.
from package1.subpackage1 import module2
or
from package1.subpackage1.module2 import Class_in_module2
or
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
from package1.subpackage1 import module2
It does not work. Path can not be found!
Any help appreciate!
Since the from keyword accepts an hierarchy of folders and import the specific method from the file, this should work.
from MainFolder.SubFolder.SomeDotPy import foo,bar,somevalue
Python has a module named "os". It also has some other module named "os.path" which is categorized under the "os".
I can use "os.path" methods even if only import the "os" module.
import os
print(os.path.join("sdfs","x"))
I wonder how can I define a sub-module like this?
That's the __init__.py 'magic' of the os module - it imports its submodule path to its namespace, essentially giving you a way to access the latter even if you only import os.
os
|- path
|- __init.__.py # 2
|- __init__.py # 1
The first __init__.py (#1) essentially has import .path so whenever you import just os, it imports path in its namespace, and therefore you can access it as os.path.
(NOTE: This is not exactly the case with the os module, but that's how to essentially achieve it)
Use this structure:
/ Package
├── __init__.py
├── file.py
│
├─┐ subpackage
│ ├── __init__.py
│ └── file.py
│
└─┐ subpackage2
├── __init__.py
└── file.py
Note each subpackage has its own __init__.py file. This will make Package.subpackage behave like os.path, importation speaking (considering you do not import .subpackage under the main __init__ file of Package).