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