import module in different contexts - python

I have a file structure like this:
work/
├─ analysis.ipynb
├─ app/
│ ├─ __init__.py
│ ├─ class_a.py
│ ├─ script.py
│ ├─ utils.py
File class_a.py contains a class MyClass and also an import from utils like this:
from utils import useful_function
class MyClass():
...
Then I try to import MyClass in analysis.ipynb like this:
from app.class_a import MyClass
and get an error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-8eeb8559d767> in <module>
2 import os
3 from datetime import datetime
----> 4 from app.class_a import MyClass
5 from app.utils import useful_function
6 ...
~/Documents/work/app/class_a.py in <module>
1 import pandas as pd
----> 2 from utils import useful_function
3
4 class MyClass():
5 '''indeed very necessary class for me
ModuleNotFoundError: No module named 'utils'
I have figured out that if I change all imports in app folder to something like this:
from app.utils import useful_function
Then I can import all I need from analysis.ipynb
However app should work as something I run with python script.py and that does not work unless imports are written in the original way.
I do not understand modules and packaging and so can not even ask a question precisely, but how do I align import "styles" in order to both be able to run python scripts.py from the app directory and import MyClass from analys.ipynb?

import statements search through the list of paths in sys.path, so I've added these lines to my analysis.ipynb:
import sys
sys.path.append('/path/to/work/app/')
In case someone would have the same question and struggle as me.

Related

Python ModuleNotFoundError: No module named 'classes'

This is the structure of my project at the moment.
classes/
├─ __init__.py
├─ card.py
├─ find.py
├─ player.py
├─ table.py
.gitignore
main.py
My __init__.py:
from .card import *
from .player import *
from .table import *
The problem is when I want to import from example player to table.py,
like this:
from classes import Player
from classes import Card, CardType, CardValue, HandValue
I get the following error message:
Traceback (most recent call last):
File "/home/adrian/Desktop/Programozas/Poker/classes/table.py", line 1, in <module>
from classes import Player
ModuleNotFoundError: No module named 'classes'
Do you have any idea?
Notice that this error is occurring in ".../classes/table.py", line 1, as you are trying to import classes.Player when both table.py and player.py are contained in classes. To do this in table.py, you would use from .player import Player just like you did in __init__.py (instead of from classes import Player).
Anything at the same level or above classes can be done using this method, but anything defined inside classes cannot call up to classes, as anything therein does not even know classes exists.
Edit: if you need to be able to run both main.py and table.py, you could do something like:
# in table.py
try:
from player import Player # this should work when running table.py directly
except:
from .player import Player # this should work when running main.py (it's a relative import)

Impossible to import a package I made. "ModuleNotFoundError"

I have a project organized like so :
application
├── app
│ └── package
└── __init__.py
│ └── functions.py
└── app2
└── some_folder
└── file_2.py
My "functions.py" contains a basic function:
#functions.py
def add(x,y):
return x+y
The file "_init_.py" is empty
I want to use the "add" function in my "file_2.py" file, so I write:
#file_2.py
from application.app.package.functions import add
print(add(2,3))
But it returns an error message:
ModuleNotFoundError: No module named 'application'
it is the same if i try any of these:
from app.package.functions import add
from package.functions import add
from functions import add
Does anyone know where the problem comes from? I'm doing exactly like in this tutorial so I don't understand what's wrong
tutorial's link
Thank you for your help
One way to import functions.add is to import sys and use sys.path.insert()
after that you can import add from functions:
import sys
sys.path.insert(1, 'the/local/path/to/package')
from functions import add
print(add(1,2))

ModuleNotFoundError on a file in a (working) package when trying to import a class on another folder

Abridged:
When importing a class (in the example below, c2) from another package (folder1), where the imported class (c2) imports a class (c1) from the same package (folder1), the program (file2) raises a ModuleNotFoundError on the import of c1 on c2, even when the import already worked in the package.
Extended:
The example have the following file structure
project/
├── folder1/
│ └── __init__.py
│ └── file1.py
│ └── file2.py
└── folder2/
└── file3.py
where the files in folder1 contain the following classes.
__init__.py is left empty.
(Notice that there's no import error on file2.py)
# file1.py
class c1:
def __init__(self, attr: int = 0):
self.attr = attr
# file2.py
from file1 import c1
class c2:
def __init__(self):
self.attr = [c1() for _ in range(10)]
the file in folder2 imports the class c2
# file3.py
import sys
sys.path.append('../') # to recognize folder1 as a package
from folder1.file2 import c2
but when I try to run file3.py the import of c1 made in file2.py raises ModuleNotFoundError
$ python3 file3.py
Traceback (most recent call last):
File "/home/user/project/folder2/file3.py", line 4, in <module>
from folder1.file2 import c2
File "/home/user/project/folder2/../folder1/file2.py", line 1, in <module>
from file1 import c1
ModuleNotFoundError: No module named 'file1'
Notice that i CAN import c1 on file3.py with the analogous import from folder1.file1 import c1 but can't make it work with c2.
(Of course, this is an abstraction of the actual classes where i found this problem, the actual folder layout is important, but the problem is the same.)
How can I import c2 on folder2/file3.py?
My attempts were trying to import c1 before c2 on file3, also tried to import c1 (and/or c2) in folder1/__init__.py but didn't work, also tried to make folder2/ a package and make the import in its __init__.py but didn't work. Of course i (probably) could simply concatenate file1 and file2 or try to create a package for file1 but i believe I'm doing something wrong on the imports and there must a simple way to solve this.
Add the same correct full path to file1.py in file2.py:
from folder1.file1 import c1
When file2.py trying to import file1.py, it trying to import from ('../') where no file1.py, only /folder1 and /folder2.
And you can delete __init__.py if you are using python 3.3+.

ModuleNotFoundError: No module named

I know that this topic has been dealt with many times already, and I have read all the answers and it seems that I did it right. but I don't understand what is wrong.
python_project:
Chronos
├── extractionScripts
│ ├── __init__.py
│ └── peps.py
└── helperfunctions
├── __init__.py
└── generallHelper.py
└── pos.py
└── logging.py
I have two folders (extractionScripts and helperFunctions).
when I try to import modules from the helperFunctions into the peps.py , I get the error -
from helperFunctions.invoiceHeader import *
ModuleNotFoundError: No module named 'helperFunctions'
peps.py
import re
import sys
print (sys.path)
from helperFunctions.generallHelper import *
from helperFunctions.pos import *
from helperFunctions.logging import *
print (sys.path) shows
['C:\\Users\\djoni\\Desktop\\Sixt\\Chronos\\extractionScripts', 'C:\\Users\\djoni\\Desktop\\Sixt\\Chronos\\helperFunctions', 'C:\\Users\\djoni\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\djoni\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\djoni\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\djoni\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\Users\\djoni\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages']
in other topics they wrote that there should be an init file, or the path should be added to the sys file, but I have them.
When you import any module in Python , Python search in the sys.path list that has all modules
have a look in your sys.path you won't see the Chronos File so in the file you try to import in it put this
import sys
from pathlib import Path
path_to_Chronos = Path(__file__).parent.parent
print(path_to_Chronos ) # Check this Right
sys.path.append(path_to_Chronos)
# then import and you will find it works
Pro Check your names
this is how you want to import it
from helperFunctions.generallHelper import *
from helperFunctions.pos import *
from helperFunctions.logging import *
but Do you see how you name it
└── helperfunctions
├── __init__.py
└── generallHelper.py
└── pos.py
└── logging.py
F it is the Problem ...

How to properly import my module file into my test file?

I have the following file structure:
C:.
├───jobs_struct
│ │
│ └───app
│ └───job_struct
│ │ delta.py
│
└───test
├───integration
└───unitaire
│ test_delta.py
test_delta.py is a pytest file and import delta.py to test its functions.
In test_delta.py, I do not understand how I am supposed to import delta.py. I have tried the following:
Attempt 1
sys.path.append("../../")
from jobs_struct.app.job_struct.delta import ApplyDelta
Throws:
E ModuleNotFoundError: No module named 'jobs_struct'
Attempt 2
from jobs_struct.app.job_struct.delta import ApplyDelta
E ModuleNotFoundError: No module named 'jobs_struct'
Attempt 3
sys.path.append("/jobs_struct/app/job_struct")
from delta import ApplyDelta
from delta import ApplyDelta
E ModuleNotFoundError: No module named 'delta'
Additionnal details
Some answers recommend to include init.py files at specific locations (or everywhere). I would like to avoid changing/adding anything to the app itself.
Moreover, the pytest command is ran from the root of the project (if that has any impact).
Question
How to correctly import my module, knowing that using absolute path is not an option, in order to be able to run my test file.
Have you tried sys.path.append('your_complete_path_to/job_struct')
and then: from delta import ApplyDelta ?

Categories