Я создал проект с УФ, который имеет следующую структуру пакета: папка SRC имеет все модули и функции папку данных, а также папки ноутбука и сценарии, где загружаются модули в SRC и данные. Тем не менее, я не могу найти способ добавить корневой путь к проекту и в конечном итоге получить: < /p>
ModuleNotFoundError: No module named 'src'
< /code>
Чтобы иметь возможность загружать модули в SRC в ноутбуке Jupyter, мне нужно добавить в каждую ноутбук < /p>
import sys
sys.path.append('..')
И для загрузки их в сценарии Python в папке Scripts мне нужно использовать описанный здесь подход (https://stackoverflow.com/a/77576864).
>>import os
import sys
project_path = os.path.abspath(os.path.join('..','sample_template'))
if project_path not in sys.path:
sys.path.insert(0, project_path)
Хотя оба параметра работают, я надеялся иметь более общее решение, которое не потребует повторяющегося Sys Append/INSERT в каждый сценарий.sample_project
├───.pytest_cache
├───.ruff_cache
├───.venv
├───.vscode
├───data
│ sample_data.csv
├───notebooks
│ sample_notebook.ipynb
├───scripts
│ sample_script.py
│ sample_script_declared.py
│ __init__.py
├───src
│ │ __init__.py
│ │
│ ├───sample_template
│ │ │ sample_functions.py
│ │ │ __init__.py
│ │ │
│ │ └───__pycache__
│ │
│ ├───sample_template.egg-info
│ │
│ └───__pycache__
│ __init__.cpython-313.pyc
│
└───tests
│ conftest.py
│ test_sample_functions.py
│ __init__.py
│
└───__pycache__
< /code>
pyproject.toml:
[project]
name = "sample-template"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
license = {text = "MIT License"}
requires-python = ">=3.13"
dependencies = [
"pandas>=2.2.3",
]
[project.scripts]
sample-template = "sample_template:main"
sample_script_declared = "scripts:sample_script_declared"
[build-system]
requires = ["setuptools>=72.0"]
build-backend = "setuptools.build_meta"
[tool.setuptools.packages.find]
where = ["src"]
include = ["sample_template*"]
[dependency-groups]
dev = [
"ipykernel>=6.29.5",
"ruff>=0.9.10",
"pytest>= 8.3.5",
"pip>=24.2",
]
Подробнее здесь: https://stackoverflow.com/questions/795 ... oject-toml
Добавление относительного пути вверх по течению к проекту в pyproject.toml ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение