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.
Related
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
I have a folder structure as
└── project
├── package1
│ ├── module1.py
│ └── module2.py
└── package2
├── __init__.py
└── module3.py
From module1.py I can currently import something from module3.py with from package2.module3 import abc. However I want to alias package2 as something else without changing the name of the folder so that I can write from new_name.module3 import abc and I want VS Code to recognize the import path.
Add a module new_name (i.e. a file new_name.py) with the following content:
__path__ = [
"package2",
]
For more info, see Packages and the role of path.
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
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
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