`python_path` is a simple utility that lets you cleanly add routes to your `PYTHON_PATH`. Its very useful to safely load scripts from other folders.
## Installing
You can install `python_path` from pip
```
pip install python_path
```
## Usage
Lets do an example, image you have the following project structure
```
- my_project/
- main.py
- some_folder/
- other.py
- another_folder/
- another.py
```
Say we want to load `other.py` form `main.py`, since some_folder is not a submodule because it doesn't contain a `__init__.py` then we need to add `some_folder` to the `PYTHON_PATH`. To make this very easy we'll use `python_path` like this:
```python
# my_project/main.py
from python_path import PythonPath
with PythonPath("some_folder"):
import other
```
and execute the file like this:
```bash
cd my_project
python main.py
```
Here `some_folder` is added relative the `PWD` (`my_project`), if you are executing `main.py` from another directory and still want to reference `some_folder`, then you should directly state relative to what file or folder the operations are being done:
```python
# my_project/main.py
from python_path import PythonPath
with PythonPath("some_folder", relative_to = __file__):
import other
```
The previous is equivalent to:
```python
# my_project/main.py
from python_path import PythonPath
import os
path = os.path.dirname(__file__)
path = os.path.join(path, "some_folder")
with PythonPath(path):
import other
```
Now we can also execute the script from any path and it sould still work e.g:
```bash
python my_project/main.py
```
Finally we can also navigate the folder structure by passing multiple parameters, so if we want to import `main.py` from `another.py` which is inside `another_folder` we could do it like this
```python
# my_project/some_folder/another_folder/another.py
from python_path import PythonPath
with PythonPath("..", "..", relative_to = __file__):
import main
```
This is equivalent to
```python
# my_project/some_folder/another_folder/another.py
from python_path import PythonPath
import os
path = os.path.join("..", "..")
with PythonPath(path, relative_to = __file__):
import main
```
Metadata
Release files for python-path 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| python_path-0.1.3.tar.gz | 2.3 kB | Without provenance | Details |
Release files / python_path-0.1.3.tar.gz
| Download URL | python_path-0.1.3.tar.gz |
|---|---|
| Size | 2.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b62d9aac1da4daee3f036ed088532cf8b68666d3aa103567dc22b6539316c8b3
|
|
BLAKE2b-256 checksum How to use checksums |
149588242c8d41bd18e825f8b76f38746d52b25746509e73d3909d9e19947dc1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for python-path 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.