Imports an object based on a string import_string('package.module:function_name')() - Based on werkzeug.utils

Overview

DEPRECATED don't use it. Please do:

import importlib
foopath = 'src.apis.foo.Foo'

module_name = '.'.join(foopath.split('.')[:-1]) # to get src.apis.foo
foo_module = importlib.import_module(module_name)
clazz_name = foopath.split('.')[-1] # to get Foo
Foo = getattr(module_name, clazz_name)
print Foo()

import_string

Documentation Status Updates

Imports an object based on a string

Features

Imports an object based on a string. This is useful if you want to use import paths as endpoints or something similar. An import path can be specified either in dotted notation (.) or with a colon as object delimiter (:). If silent is True the return value will be None if the import fails.

Usage

import import_string

module = import_string('my_system.my_package.my_module')

function = import_string('my_system.my_module:some_function')

Class = import_string('my_system.my_module:SomeClass', silent=True)
# If path doesn't exist Class = None

Live demo

See it in action here: https://repl.it/EGdS/0

Credits

You might also like...
Practice10 - Operasi String With Python
Practice10 - Operasi String With Python

Operasi String MY SOSIAL MEDIA : Apa itu Python String ? String adalah urutan si

A "multiclipboards" script for an efficient way to improve the original clipboards which are only able to save one string at a time

A "multiclipboards" script for an efficient way to improve the original clipboards which are only able to save one string at a time. Works on both Windows and Linux.

A pypi package details search python module

A pypi package details search python module

PyPIContents is an application that generates a Module Index from the Python Package Index (PyPI) and also from various versions of the Python Standard Library.

PyPIContents is an application that generates a Module Index from the Python Package Index (PyPI) and also from various versions of the Python Standar

carrier.py is a Python package/module that's used to save time when programming

carrier.py is a Python package/module that's used to save time when programming, it helps with functions such as 24 and 12 hour time, Discord webhooks, etc

PyPI package for scaffolding out code for decision tree models that can learn to find relationships between the attributes of an object.

Decision Tree Writer This package allows you to train a binary classification decision tree on a list of labeled dictionaries or class instances, and

A test repository to build a python package and publish the package to Artifact Registry using GCB

A test repository to build a python package and publish the package to Artifact Registry using GCB. Then have the package be a dependency in a GCF function.

An extension module to make reaction based menus with disnake

disnake-ext-menus An experimental extension menu that makes working with reaction menus a bit easier. Installing python -m pip install -U disnake-ext-

Module-based cryptographic tool
Module-based cryptographic tool

Cryptosploit A decryption/decoding/cracking tool using various modules. To use it, you need to have basic knowledge of cryptography. Table of Contents

Comments
  • Initial Update

    Initial Update

    Hi 👊

    This is my first visit to this fine repo, but it seems you have been working hard to keep all dependencies updated so far.

    Once you have closed this issue, I'll create seperate pull requests for every update as soon as I find one.

    That's it for now!

    Happy merging! 🤖

    opened by pyup-bot 0
  • Improve error message

    Improve error message

    Using it from manage when some file has missing library import inside it the error message is not clear.

    # robottelo.decorators.func_locker
    
    from pytest_services.locks import file_lock  
    
    # pytest_services x is not installed
    # so the following error message should be more clear about it.
    
    @localhost(rob63) :~/P/robottelo|masterâš¡?
    ➤ manage shell                                                                                                                                                            12:41:26
    Traceback (most recent call last):
      File "/home/brocha/.virtualenvs/rob63/bin/manage", line 11, in <module>
        sys.exit(main())
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/manage/cli.py", line 235, in main
        return manager()
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/click/core.py", line 722, in __call__
        return self.main(*args, **kwargs)
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/click/core.py", line 697, in main
        rv = self.invoke(ctx)
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/click/core.py", line 895, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/click/core.py", line 535, in invoke
        return callback(*args, **kwargs)
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/manage/cli.py", line 196, in shell
        MANAGE_DICT
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/manage/cli.py", line 108, in create_shell
        auto_imported = import_objects(manage_dict)
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/manage/auto_import.py", line 57, in import_objects
        import_submodules(name)
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/manage/auto_import.py", line 21, in import_submodules
        import_string('{0}.{1}'.format(name, item[1]))
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/import_string/base.py", line 61, in import_string
        sys.exc_info()[2])
      File "/home/brocha/.virtualenvs/rob63/lib/python2.7/site-packages/import_string/base.py", line 54, in import_string
        raise ImportError(e)
    import_string.base.ImportStringError: import_string() failed for 'robottelo.decorators.func_locker'. Possible reasons are:
     
    - missing __init__.py in a package;
    - package or module path not included in sys.path;
    - duplicated package or module name taking precedence in sys.path;
    - missing module, class, function or variable;
     
    Debugged import:
     
    - 'robottelo' found in './robottelo/__init__.py'.
    - 'robottelo.decorators' found in './robottelo/decorators/__init__.py'.
    - 'robottelo.decorators.func_locker' not found.
     
    Original exception:
     
    ImportError: 'module' object has no attribute 'func_locker'
    
    opened by rochacbruno 0
  • Sharing some experience

    Sharing some experience

    Hi @rochacbruno, nice work exporting this code to a new library, it's really usefull :)

    I've done done this kind of import in some projects, but using this approach:

    import importlib
    foopath = 'src.apis.foo.Foo'
    
    module_name = '.'.join(foopath.split('.')[:-1]) # to get src.apis.foo
    foo_module = importlib.import_module(module_name)
    clazz_name = foopath.split('.')[-1] # to get Foo
    Foo = getattr(module_name, clazz_name)
    print Foo()
    

    It's almost the same thing, but using importlib instead of import directly. Actually, importlib uses the import function as well. Anyway, just creating this issue to share some alternatives to the same goal.

    Thank you!

    opened by felipevolpone 0
Owner
Bruno Rocha Archived Projects
I abandoned those projects, feel free to fork and do WTF you want, if you want to adopt/own a repo contact me.
Bruno Rocha Archived Projects
Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses

Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses. Obtain the postfix expression of the infix expression Step 1.Reverse the postfix expression to get the prefix expression

Sazzad Hossen 1 Jan 4, 2022
Utils to quickly evaluate many 🤗 models on the GLUE tasks

Utils to quickly evaluate many ?? models on the GLUE tasks

Przemyslaw K. Joniak 1 Dec 22, 2021
An easy python calculator for those who want's to know how if statements, loops, and imports works give it a try!

A usefull calculator for any student or anyone who want's to know how to build a simple 2 mode python based calculator.

Antonio Sánchez 1 Jan 6, 2022
A numbers extract from string python package

Made with Python3 (C) @FayasNoushad Copyright permission under MIT License License -> https://github.com/FayasNoushad/Numbers-Extract/blob/main/LICENS

Fayas Noushad 4 Nov 28, 2021
Module for remote in-memory Python package/module loading through HTTP/S

httpimport Python's missing feature! The feature has been suggested in Python Mailing List Remote, in-memory Python package/module importing through H

John Torakis 220 Dec 17, 2022
Minimal, super readable string pattern matching for python.

simplematch Minimal, super readable string pattern matching for python. import simplematch simplematch.match("He* {planet}!", "Hello World!") >>> {"p

Thomas Feldmann 147 Dec 1, 2022
String Spy is a project aimed at improving MacOS defenses.

String Spy is a project aimed at improving MacOS defenses. It allows users to constantly monitor all running processes for user-defined strings, and if it detects a process with such a string it will log the PID, process path, and user running the process. It will also (optionally) kill the process. For certain default C2s and other malicious software, this tool can quickly log and stop malicious behavior that normal AV does not recognize, and allows for customization.

null 10 Dec 13, 2022
Analisador de strings feito em Python // String parser made in Python

Este é um analisador feito em Python, neste programa, estou estudando funções e a sua junção com "if's" e dados colocados pelo usuário. Neste código,

Dev Nasser 1 Nov 3, 2021
Standard mutable string (character array) implementation for Python.

chararray A standard mutable character array implementation for Python.

Tushar Sadhwani 3 Dec 18, 2021
Modeval (or Modular Eval) is a modular and secure string evaluation library that can be used to create custom parsers or interpreters.

modeval Modeval (or Modular Eval) is a modular and secure string evaluation library that can be used to create custom parsers or interpreters. Basic U

null 2 Jan 1, 2022