Specify path for import modules separately for each file - python

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.

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

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.

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).

Import local modules in Jupyter notebook

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

Import directory errors with Python 3.5 and above

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

Categories