A clean way to import scripts on other folders via a context manager.
Project description
# Python Path
`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
```
`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
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for python_path-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a931878ac6e40a924221ba1f371fb5b60fd58f12c5b7fab48b27c34a0f034a2 |
|
MD5 | 54ed180d61b5b20b00c51f8e97aa9fd8 |
|
BLAKE2b-256 | dc53fd22f47d0b1873d9d7ca8286ed28810ecc4dbebdea4cc713cd878147b875 |