i want to access config/cache.py from test.py And ,I need internal access like from config/env.py to database/modules/game.py but i get this error always:
ModuleNotFoundError: No module named 'modules'
my project tree:
project
├─ modules
│ ├─ cache
│ │ ├─ playtime.py
│ │ └─ __init__.py
│ ├─ config
│ │ ├─ cache.py
│ │ ├─ database.py
│ │ ├─ env.py
│ │ ├─ yaml.py
│ │ └─ __init__.py
│ ├─ database
│ │ └─ models
│ │ ├─ game.py
│ │ ├─ member.py
│ │ ├─ room.py
│ │ ├─ wiki.py
│ │ └─ __init__.py
│ ├─ room
│ ├─ utils
│ │ ├─ functions.py
│ │ ├─ logger.py
│ │ └─ __init__.py
│ └─ __init__.py
├─ run.py
└─ test
└─ test.py
and inits file are like that:
from .config import *
from .utils import *
from .database import *
from .cache import *
Python automatically insert the folder in which the main script resides to sys.path, which is a list containing folders where to find packages and modules.
So, if test.py were in folder project, that folder would be in sys.path and then you could import the modules package and any of its subpackages or modules.
import module # main package module
from module import config # subpackage config
from module.config import env # module env
(Just in case, a module is a file ending in .py, and a package is a folder with a file called __init__.py inside and optionally subpackages and/or modules.)
So one solution wood be to move the file test.py to projects
Another alternative is to add the folder projects (as a str) manually to sys.path which is a simple python list before importing.
import sys
sys.append('.../projects')
from modules.config import env
Related
I have multiple small projects that are somewhat related to eachother so i want them to be in single sphinx documentation.
Each handler.py module have different docstring but when i build documentation both modules reference handler.py from App1.
Each App is single project but containing similar file names:
/
├─ conf.py
├─ index.rst
├─ App1/
│ ├─ handler.py
│ ├─ errors.py
│ ├─ DOCS/
│ │ ├─ modules.rst
│ │ ├─ handler.rst
│ │ ├─ errors.rst
├─ App2/
│ ├─ DOCS/
│ │ ├─ modules.rst
│ │ ├─ handler.rst
│ ├─ handler.py
in root "conf.py" i insert paths for them:
sys.path.insert(0, os.path.abspath("App1/"))
sys.path.insert(0, os.path.abspath("App2/"))
in root "index.rst" i include them in toctree:
.. toctree::
:maxdepth: 12
:caption: Content:
app1/docs/modules
app2/docs/modules
each hander.rst contains:
handler module
================
.. automodule:: handler
:members:
:undoc-members:
:show-inheritance:
How should i structure or reference python files so that each App.handler page contains docstring only from that app handler.py?
I have the following project structure:
my-app/
├─ README.md
├─ LICENSE
├─ tests/
│ ├─ my_test.py
├─ src/
│ ├─ utils/
│ │ ├─ __init__.py
│ │ ├─ utils.py
│ ├─ __init__.py
│ ├─ app.py
And my app.py (located in src) uses some functions of utils.py. When I run python src/app.py it works fine, but, when I run python -m unittest tests.my_tests for the tests code (which instantiates the main class at app.py) I get the following error:
from utils.utils import *
ModuleNotFoundError: No module named 'utils'
I have the following folder structure:
...
│
├── src
│ ├── folder_A
│ │ └── file_A.py
│ │ └── __init__.py
│ │
│ ├── folder_B
│ │ └── file_B.py
│ │ └── __init__.py
│ │
│ └── __init__.py
│
│
└── something else
In the file file_A.py I put from folder_B import file_B as fb. But file_A.py works only in debug mode (meaning that the code produces the expected results). If I run file_A.py in the standard way I get the error ModuleNotFoundError: No module named 'folder_B'.
I also changed the configuration before running the code, putting C:\Users\***\***\***\src as the working directory of file_A.py but it still doesn't work.
What can be a solution?
If your current directory is src, then folder_B is in the path because of that. If you want to make a package with sub-packages that can access each other, place everything into a root package:
│
├── src
│ └── root_package
│ ├── folder_A
│ │ ├── file_A.py
│ │ └── __init__.py
│ │
│ ├── folder_B
│ │ ├── file_B.py
│ │ └── __init__.py
│ │
│ └── __init__.py
│
└── something else
Now in file_A, you can do
from ..folder_B import file_B as fb
Since src is not a package, you can't do a relative import through it. By adding root_package, you make it possible to find folder_B in the same package hierarchy (albeit a different branch) as the module doing the import.
I have a project structure, like so:
└── auto/
│ ├──── __init__.py
│ └──── __pycache__/
│ ├──── deploy
│ ├──── destroy
│ └──── helpers/
│ │ ├──── __init__.py
│ │ ├──── cli.py
│ │ └──── zip.py
│ └──── synth
└── src/
│ ├──── __init__.py
│ ├──── main.py
│ └──── models/
│ │ ├──── __init__.py
│ │ ├──── filter.py
│ │ └──── json_extract_config.py
│ └──── utils/
│ │ ├──── __init__.py
│ │ └──── json_extractor.py
I am trying to call the script synth in the ./auto/synth location.
The synth file is a script with a shebang at the top #!/usr/bin/env python3
It imports a function from the ./auto/helpers/zip.py file.
When I run, ./auto/synth from the root folder it throws an error:
from auto.helpers.zip import zip_folder
ModuleNotFoundError: No module named 'auto'
Although when I run PYTHONPATH="$PWD" ./auto/synth the script executes successfully.
Can someone help me understand this behaviour and how to avoid it so that my scripts can be run normally without having to change the PYTHONOPATH?
Thanks in advance.
If you are using python version 3.3+ you do not need to use the __init__.py files anymore, as the namespacing is built into the modules now. link --> Is __init__.py not required for packages in Python 3.3+
I believe you will have to add the {full path}/src and {full path}/auto directories to the PYTHONPATH to provide python scripts the proper pathing, or you can use the python sys module and use sys.path inside your modules to set your pathing for the module you would like to import link --> https://docs.python.org/3/tutorial/modules.html#the-module-search-path
import sys
# example path could be "C:/Users/Bob/Desktop/project/auto"
sys.path.append("{full path to your auto directory}/auto")
I have the following directory structure:
F:.
│ .gitignore
│
└───Tests
│ main.py
│ __init__.py
│
├───compare
│ │ __init__.py
│ │
│ └───process
│ process.py
│ __init__.py
│
├───lookup
│ User.py
│ __init__.py
│
├───requestor
│ Requestor.py
│ __init__.py
│
├───search
│ UserSearch.py
│ ProductSearch.py
│ __init__.py
│
└───utils
constants.py
__init__.py
I am able to import any file/class from main.py like
from search.UsersSearch import UsersSearchPayLoad
from search.ProductSearch import ProductSearchPayLoad
from utils.constants import *
but I cannot do any import from any other package like, from search.UserSearch:
from ..utils.constants import *
from Tests.utils.constants import *
I was referring this for understanding the correct way to use relative imports.
I am using Python 2.7.11
Try adding:
import os
import sys
sys.path.append(os.path.realpath(os.getcwd()))
In:
Tests/__init__.py
For the latest phython Version e.g 3.7.0
from directory import class-name
A directory is your folder structure. It could be subDirectory\directory