Skip to main content

Import Python script from any directory or git commit history.

Project description

pyportal

Dependable Imports for Your Ever-Changing Scripts.

Working with constantly changing Python scripts poses a unique challenge: how do you maintain reliable imports? The answer is pyportal. This innovative tool enables you to lock in on a specific version of your script, ensuring that future changes won't disrupt your dependencies. With pyportal, you're free to innovate and iterate your scripts while keeping other projects stable and dependable.

Examples

Example 1: Simple Import You can use pyportal to import the latest version of the say_hello function from myutils.py. This will always import say_hello from the most recent commit (as the suffix .latest suggests).

# This is your original script file `myutils.py`
def say_hello():
    print('hello')

# Now you want to use it in another place:
import pyportal
from pyportal.myutils.latest import say_hello

# Using the function
say_hello()  # prints: hello

Example 2: Version-specific Import With the .v suffix, pyportal can import a specific version of a function. Even though myutils.py has been modified, you can still access the old version of say_hello thanks to pyportal.

# Assume that you have modified `myutils.py` and it now looks like this:
def say_hello():
    print('hello, world!')

# But you still want the old version in another place:
import pyportal

# v20230103192241 is a version identifier, 'v' followed by the datetime of the specific git commit
from pyportal.myutils.v20230103192241 import say_hello

# Using the function
say_hello()  # prints: hello

Example 3: Managing Multiple Script Versions pyportal allows you to manage and use multiple versions of a script within the same project. By importing different versions of the say_hello function under different names, you can use the version that suits your needs in each part of your project.

# Assume that you have multiple versions of `myutils.py` and you want to use different versions in different places:
import pyportal

# Importing a specific version, again using the datetime of the commit as the version identifier
from pyportal.myutils.v20230103192241 import say_hello as old_hello
old_hello()  # prints: hello

Example 4: Importing Like Standard Python Imports With the .file suffix, you can import whatever is currently in the script file with pyportal. It works just like the standard Python import. This is particularly useful for testing out your scripts. But be aware, you must commit your changes to be able to lock in on them, as shown in Example 2.

# Assume that you have multiple versions of `myutils.py` and you want to use different versions in different places:
import pyportal

# Importing a specific version, again using the datetime of the commit as the version identifier
from pyportal.myutils.v20230103192241 import say_hello as old_hello
old_hello()  # prints: hello

How to use

Install the package

pip install pyportal

Import syntax

You must import the pyportal package first. Then you can import a specific version of a script.

For example, you have a folder with the following structure

# /utility_scripts/myutil.py
def foo():
    pass

###

# In any python script on your computer
import pyportal
# add script folder
pyportal.path.insert(0, "/utility_scripts")

# Import from script myutil.py
from pyportal.myutil.file import foo
# OR import the whole script
import pyportal.myutil.file as Util
# OR import everything
from pyportal.myutil.file import *

Package statement follows the syntax pyportal.<name_of_scriptfile>.<version> (e.g., pyportal.myutil.latest). It always has three components, separated by dots.

  1. pyportal. The name of the package.
  2. <name_of_scriptfile>. The file name of your script, no space allowed. pyportal searches in order of specified folders (see the setup guide below), and returns the first result. Try not to have scripts under the same name.
  3. <version>. Specify the version of the script to import from. Can be one of
    1. file. Import the file on disk just like the standard Python import.
    2. vyyyymmddHHMMSS. Import a specific version to lock in. It is a v followed by the datetime of the git commit in yyyymmddHHMMSS (year month day hour minutes seconds) format.

Setup script folders

Working like Python imports, pyportal searches for the requested script by its file name in the given folders. You can set the folders globally in the environment variable pyportal_PATH. For example, we add a folder under C drive named my scripts, and another folder named PythonScripts under our Documents folder. In all OS systems, you need to separate multiple folders by a semicolon. Note that the order of folders matters. If you have two script files with the same name, the one that appeared first in the folders will be used.

pyportal_PATH=C:\my scripts;C:\Document\PythonScripts

You can also modify the path pyportal searches by manipulating its path attribute:

import pyportal
# modify the path list and add a new folder to search for scripts
pyportal.path.append('D:/new script')
# import a constant from a script located at D:/new script/foo.py
from pyportal.foo.file import pi
print(pi) 
# prints 3.14

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyportal-1.0.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyportal-1.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pyportal-1.0.1.tar.gz.

File metadata

  • Download URL: pyportal-1.0.1.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.11

File hashes

Hashes for pyportal-1.0.1.tar.gz
Algorithm Hash digest
SHA256 26ccbd3872e2d9314d85fab12d1e53dc4df6ab074608ee47c4af637d99299731
MD5 906dcb835124d32d4e629c119b1ff0b0
BLAKE2b-256 10e0b6cfacf7280fd54593bc2d6160f4c078fb6143e52145b4d36da329a59607

See more details on using hashes here.

File details

Details for the file pyportal-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pyportal-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.11

File hashes

Hashes for pyportal-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1df758cbc8d1b837d7659258fb620714a839253afffa081418a32c2122b3b320
MD5 877f5bdf457427c2ec95fbdda514e5c2
BLAKE2b-256 42b6a5e7c1db38c2caec5f1312c1f6fc29fb6b05e6997a1fcde5e17a905e9f85

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page