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
Related
I would like to use a "helper" decorator in multiple pytest test files:
min311 = pytest.mark.skipif(sys.version_info < (3,11), reason="You need at least Python v3.11 to run this test")
...
#min311
def test_...
Which is the best place for min311? It is not imported automatically from conftest.py.
Given this layout:
.
├── src
│ ├── conftest.py
│ └── mycode
│ ├── __init__.py
│ └── main.py
└── tests
├── conftest.py
├── __init__.py
└── test_mycode.py
4 directories, 6 files
If I define min311 in tests/conftest.py, then in test_mycode.py I can write:
from tests.conftest import min311
#min311
def test_something():
...
This assumes that we are running pytest from the top-level directory.
This also works for the simpler layout:
.
├── conftest.py
├── mycode
│ ├── __init__.py
│ └── main.py
└── tests
├── conftest.py
├── __init__.py
└── test_mycode.py
If you don't want to import from conftest directly, just move the decorator definition somewhere more palatable. I've seen a number of projects that include something like a test_helpers module in their code:
.
├── conftest.py
├── mycode
│ ├── __init__.py
│ ├── main.py
│ └── test_helpers.py
└── tests
├── conftest.py
└── test_mycode.py
Then you can:
from mycode.test_helpers import min311
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.
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
Using Python 3.6
I did create multiple modules (like DBmanager or jsonParser etc which I use across multiple different python projects)
For simplicity: I have created a module, lets call it 'gitmodule03'.
Internally it is supposed to be using yet another module from github 'gitmodule01' for parsing data. I have added 'gitmodule01' to 'gitmodule03' by
'git submodule add http://git/gitmodule01'
Separatenly, I am developing my 'MainPackage' which will use directly 'gitmodule03' and 'gitmodule01' (among others). I've added them all to my main Program with
'git submodule add http://git/gitmodule01'
'git submodule add http://git/gitmodule02'
'git submodule add http://git/gitmodule03'
and my package looks like this:
.
└── MainPackage
├── modules
│ ├── __init__.py
│ ├── gitmodule01
│ │ ├── __init__.py
│ │ └── mymodule01.py
│ ├── gitmodule02
│ │ ├── __init__.py
│ │ └── mymodule02.py
│ ├── gitmodule03
│ │ ├── __init__.py
│ │ ├── mymodule03.py
│ │ └── gitmodule01
│ │ └──
│ └── mymodule04.py
└── myMainProgram.py
At this moment 'gitmodule03' is NOT importing 'gitmodule01' internally. I was hoping that importing it in main myMainProgram.py would propagate across submodules (which is not the case)
If my myMainProgram.py imports them all:
from modules.gitmodule01.mymodule01 import my01class
from modules.gitmodule02.mymodule02 import my02class
from modules.gitmodule03.mymodule03 import my03class
my03class() # will work
my02class() # is internally using 'my03class()' and will error out:
NameError: name 'my03class' is not defined
How can I design those so they can work independently as well as within bigger package, in clean, pythonic way ?
I would like to have those modules idependent so they won't have to use any hard coded sys.path() methods
Edit Test Cases:
1.
myMainProgram.py
sys.path.insert(0, "modules/gitmodule03/gitmodule01/")
from mymodule01 import my01class
from modules.gitmodule03.mymodule03 import my03class
my01class() #works
my03class() # NameError: name 'my01class' is not defined
2.
myMainProgram.py
from modules.gitmodule03.gitmodule01.mymodule01 import my01class
from modules.gitmodule03.mymodule03 import my03class
my01class() #works
my03class() # NameError: name 'my01class' is not defined
3.
mymodule03.py
from gitmodule01.mymodule01 import my01class
my01class() #works
myMainProgram.py
from modules.gitmodule01.mymodule01 import my01class
from modules.gitmodule03.mymodule03 import my03class
my03class() # ModuleNotFoundError: No module named 'gitmodule01'
4.
mymodule03.py
from .gitmodule01.mymodule01 import my01class
my01class() # ModuleNotFoundError: No module named '__main__.gitmodule01'; '__main__' is not a package
myMainProgram.py
from modules.gitmodule03.mymodule03 import my03class
my03class() # works
With Test Case #4 It looks like i could make myMainProgram.py work but i would have to break module on its own.
So far I could not find better option to have both working myMainProgram.py and mymodule03.py on its own.
At the moment I am checking name variable to see whenever module is working on its own or whenever it is run from other package:
mymodule03.py
if __name__ == '__main__':
from gitmodule01.mymodule01 import my01class
my01class() # works
else:
from .gitmodule01.mymodule01 import my01class
myMainProgram.py
from modules.gitmodule03.mymodule03 import my03class
my03class() # works
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