For running python code as modules under root a package, sys.path
needs to have a full absolute path in the list of it that python can recognize the package location. There are some ways to do that as below.
Related post is Consideration about python logging and file configuration(2)
Environment
- MacOS Catalina 10.15.7
- Python 3.9.7
1. Append path to sys.path
from python code
For checking sys.path
, just print it.
1
2
3
4
5
6
7
8
|
$ python
Python 3.9.7 (default, Sep 10 2021, 10:49:24)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/Users/a-user/.pyenv/versions/3.9.7/lib/python39.zip', '/Users/a-user/.pyenv/versions/3.9.7/lib/python3.9', '/Users/a-user/.pyenv/versions/3.9.7/lib/python3.9/lib-dynload', '/Users/a-user/.pyenv/versions/3.9.7/lib/python3.9/site-packages' ]
>>> quit()
|
For adding it, sys.path.append("<path name>")
1
2
3
|
>>> sys.path.append('~/repo/tatoflam/python_logging')
>>> print(sys.path)
['', '/Users/a-user/.pyenv/versions/3.9.7/lib/python39.zip', '/Users/a-user/.pyenv/versions/3.9.7/lib/python3.9', '/Users/a-user/.pyenv/versions/3.9.7/lib/python3.9/lib-dynload', '/Users/a-user/.pyenv/versions/3.9.7/lib/python3.9/site-packages', '/Users/a-user/repo/tatoflam/python_logging']
|
For removing it, user sys.path.remove("<path>")
.
2. Add to environment variable: PYTHONPATH
sys.path is initialized by environment variable: PYTHONPATH
.
1
|
export PYTHONPATH="~/repo/tatoflam/python_logging":$PYTHONPATH
|
Preparing setup.py
, then pip install -e .
, pip adds the project path to sys.path
.
If setup.py
does not exists in package root directory and run pip install -e .
, error is raised like:
1
2
3
4
|
$ pip install -e .
ERROR: File "setup.py" or "setup.cfg" not found. Directory cannot be installed in editable mode: /Users/a-user/repo/tatoflam/python_logging
WARNING: You are using pip version 21.2.3; however, version 21.3 is available.
You should consider upgrading via the '/Users/a-user/.pyenv/versions/3.9.7/bin/python3.9 -m pip install --upgrade pip' command.
|
directory structure
1
2
3
4
5
6
7
|
python_logging
├── sample5
│ ├── __init__.py
│ ├── logging5.conf
│ ├── sample5_called.py
│ └── sample5_main.py
└── setup.py
|
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import glob
import sys
from os.path import splitext, basename
from setuptools import find_packages, setup
import warnings
setup(
name="python_logging",
version="0.1-DEV",
url="https://github.com/tatoflam/python_logging",
author="tatoflam",
license="MIT",
packages=find_packages(),
include_package_data=True,
zip_safe=False,
description="Experiment for python standard logging library",
)
|
Then, '/Users/a-user/repo/tatoflam/python_logging'
is appended to sys.path
4. Add by site-packages
Add site-packages/any-name.pth
file, then site
module process it to add path to sys.path. In any-name.pth
file, sub directory needs to be listed.
See detail information in docs.python.org - site — Site-specific configuration hook
Sources
Sample code for setup.py
is uploaded in github
References