Import class from file in a different folder - python-3.x

I’m trying to use a class from other folder.
I created an __init__.py file in all my folders. But I sill can’t import the class.
I’m trying to import Vector class from ex02/vector,py to ex03/matrix.py
Here is the tree file
day01
.
├── README.md
├── __init__.py
├── ex00
│ ├── __init__.py
│ ├── book.py
│ ├── recipe.py
│ └── test.py
├── ex01
│ ├── __init__.py
│ └── game.py
├── ex02
│ ├── __init__.py
│ ├── pytest.py
│ └── vector.py
└── ex03
├── __init__.py
└── matrix.py
I’m importing using this command:
from day01.ex02.vector import Vector
But I’m getting this error:
Traceback (most recent call last):
File "matrix.py", line 1, in <module>
from day01.ex02.vector import Vector
ModuleNotFoundError: No module named 'day01'
The python code that is importing is this one:
https://pastebin.com/8AZAV6fv
And the one that I'm trying to import is this one
https://pastebin.com/BkYX9sc4

To access files between folders inside your project you just need to mention the folder name and them the filename.
for example:
ProjectFolder:
Main.py
folderA -> FileA.py
FolderB -> FileB.py
let's say that you are in right now in file FileA and you want to use FileB's content in it.
all you have to do is:
from FolderB import File
Here's an actually example in VS code
So in your code in the matrix.py file just write:
from ex02 import Vector

Related

import from another directory outside of the package but in same project

I am trying to import from another directory outside of the package 'example' but in the same project. here is my project directory:
project_name
├── __init__.py
├── demo3
│ ├── __init__.py
│ └── test.ipynb
├── example
│ ├── demo1
│ │ ├── __init__.py
│ │ └── test1.py
│ └── demo2
│ ├── __init__.py
│ └── test2.py
project_name/example/demo1/test1.py contains a function hello()
I want to import this function in project_name/demo3/test.ipynb.
I tried different options with different error msgs:
**
Method1:
from example.demo1.test1 import hello
Error:
ModuleNotFoundError: No module named 'example'
**
**
Method2:
from ..example.demo1.test1 import hello
Error:
ImportError: attempted relative import with no known parent package
**
NOTE: the import works when I pull test.ipynb out of demo3 directory.
I want to be able to import without run
if module_path not in sys.path:
sys.path.append(module_path)``` on every file

ModuleNotFoundError: No module named <module-name>

I have a package named ImgUtils in my project directory, which looks something like this:
├── myproject
│ ├── test.py
│ ├── Analyzer
│ ├── __init__.py
│ ├── utils.py
│ └── ImgUtils
│ ├── __init__.py
│ ├── ImgUtils.pyd
│ ├── ImgUtils.so
│ └── a bunch of *.dll files
ImgUtils.pyd exports a class called PyImageUtils.
\\ utils.py
from ImgUtils.ImgUtils import PyImageUtils
def my_test_function():
a = PyImageUtils.analyze()
return a
\\ test.py
from Analyzer import utils
print(utils.my_test_function())
The import in utils.py works fine, but if I run test.py, I get ModuleNotFoundError: No module named 'PyImgUtils'.
I am using Python 3.10. The path to ImgUtils.pyd is in the sys.path list.
UPDATE: I was able to solve this by importing ImgUtils like so:
\\ utils.py
from myproject.Analyzer.ImgUtils.ImgUtils import PyImageUtils
I was using Django and this answer explains the solution.

How to solve python moduleNotFoundError

I'm having trouble understanding the module layout in python. Here is my directory / file structure
Project2.1/
├── project2
│   ├── data_mining
│   │   ├── process.py
│   │   └── __init__.py
│   └── __init__.py
└── tests
   ├── data
   │   └── data.csv
   ├── data_mining
   │   ├── __init__.py
   │   └── test_process.py
   └── __init__.py
File test_process.py, contains the following import
from project2.data_mining.process import ClassP
Run file tests/data_mining/test_proecss.py using the following command from directory Project2.1
$ cd Project2.1
$ python3 tests/data_mining/test_process.py
Generates the error
File "tests/data_mining/test_process.py", line 7, in <module>
from project2.data_mining.process import ClassP
ModuleNotFoundError: No module named 'project2'
ClassP is a class inside project2/data_mining/process.py
Since you are in the data_mining directory of tests folder , only those files inside the data_mining folder are accessible directly, and you can't type the path of module with from, you need to add the path of the data_mining folder of project2 , so get the exact path of data_mining (of project2 )
and
import sys
sys.path.append(exact path)
from process import ClassP
this will append the path of that folder and make all files inside it accessible to the import system
also remeber we don't use .py or any extension while importing
it is just like importing any other module from random import randint for instance
:D

Is there a way to import a file in a sub directory with a space [duplicate]

This question already has answers here:
How do you import a file in python with spaces in the name?
(4 answers)
Closed 1 year ago.
import function from a file in the same folder
I got a pretty good answer in this post on how to import a file from a sub directory. But now I also want to know how to import a subdirectory that contains a space,
so from the original example in the post above; lets say that we have an identical sub directory called "app 2" and I want to import the config.py from that sub directory.
DoubleDibz
├── app 2
│ ├── __init__.py
│ ├── api
│ │ ├── __init__.py
│ │ └── helloworld.py
│ ├── app.py
│ ├── common
│ │ ├── __init__.py
│ │ └── constants.py
│ ├── config.py
│ ├── extensions.py
│ ├── static
│ └── templates
└── run.py
How would I go about doing this?
import app 2.config doesn't seem to work and adding single or double quotes does not seem to work either.
As seen in the top answer of the question shared by #truth, you can do something like this:
_config = __import__("app 2.config")
Ideally a Python project would not have folder names with spaces in them, as this is method is not recommended, so if this is yours then I would suggest to just change the name of that folder to app_2

How to import from a project folder that is dedicated to containing packages?

I need to write a bunch of libraries for the current project I am working on. The support libraries are called WestPond and EastPond and the main project is called AtlanticOcean. I don't want to install WestPond and EastPond to my site-packages folder because I want site-packages to be only for packages that weren't written by me. The folder sturcture looks like:
atlantic_ocean/
│
├── AtlanticOcean/
│ └── main.py
│
├── scripts/
│ └── send_report.py
│
├── requirements.txt
│
│
└── src/
│
├── WestPond
│ └── main.py
│
└── EastPond
└── main.py
Inside of atlantic_ocean\src\WestPond\main.py I have:
def main():
return ['salmon', 'trout', 'tuna']
Inside of atlantic_ocean\AtlanticOcean\main.py I have:
import sys
src_path = r'C:\Users\robert_levey\pycharmprojects\atlantic_ocean\src'
sys.path.append(src_path)
from WestPond.main import main as west_pond_species
I feel like the current sys.path.append method is ugly and unpythonic. Is there a better way to import all the packages inside the src folder?
Ideally, there would be something like:
from .src import WestPond, EastPond

Resources