Skip to content

[Plugin] Unable to load using absolute path #726

Open
@moham96

Description

@moham96

Describe the bug
This states that one can load a plugin simply by using the absolute path for the plugin but it's not working for me

python -m proxy --log-level d --ca-key-file ca-key.pem --ca-cert-file ca-cert.pem --ca-signing-key-file ca-signing-key.pem --plugins /home/mohammad/repos/plugintest/proxyPlugin.proxyPlugin
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/mohammad/repos/proxy.py/proxy/__main__.py", line 14, in <module>
    entry_point()
  File "/home/mohammad/repos/proxy.py/proxy/proxy.py", line 176, in entry_point
    main(input_args=sys.argv[1:])
  File "/home/mohammad/repos/proxy.py/proxy/proxy.py", line 155, in main
    with Proxy(input_args=input_args, **opts):
  File "/home/mohammad/repos/proxy.py/proxy/proxy.py", line 105, in __init__
    self.flags = FlagParser.initialize(input_args, **opts)
  File "/home/mohammad/repos/proxy.py/proxy/common/flag.py", line 100, in initialize
    Plugins.discover(input_args)
  File "/home/mohammad/repos/proxy.py/proxy/common/plugins.py", line 38, in discover
    Plugins.importer(bytes_(part))
  File "/home/mohammad/repos/proxy.py/proxy/common/plugins.py", line 75, in importer
    importlib.import_module(
  File "/usr/lib/python3.9/importlib/__init__.py", line 122, in import_module
    raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '.home.mohammad.repos.plugintest.proxyPlugin'

To Reproduce
Steps to reproduce the behavior:
1- create a simple plugin in a directory outside proxy.py tree for me i created a directory at /home/mohammad/repos/plugintest/ that contains a file named proxyPlugin.py the file contains a plugin class named proxyPlugin
2. Run proxy.py as python -m proxy --log-level d --ca-key-file ca-key.pem --ca-cert-file ca-cert.pem --ca-signing-key-file ca-signing-key.pem --plugins /home/mohammad/repos/plugintest/proxyPlugin.proxyPlugin
3- The following exception is raised TypeError: the 'package' argument is required to perform a relative import for '.home.mohammad.repos.plugintest.proxyPlugin'

Expected behavior
According to the README.md file using the absolute path for the plugin should work just fine

Version information

  • OS: Manjaro linux

  • Device: Desktop pc

  • proxy.py version: git version

Activity

abhinavsingh

abhinavsingh commented on Nov 11, 2021

@abhinavsingh
Owner

@moham96 IIRC, relative imports are not allowed within plugins. I might have to double check on this, but can you try to make your imports absolute and give it a try. If necessary add your plugin module as PYTHONPATH too. Let me know.

changed the title [-]loading plugin using absolute path not working[/-] [+][Plugin] Unable to load using absolute path[/+] on Nov 11, 2021
abhinavsingh

abhinavsingh commented on Nov 11, 2021

@abhinavsingh
Owner

You can also try:

PYTHONPATH=/home/mohammad/repos/plugintest proxy --plugins proxyPlugin.proxyPlugin

Ref: https://github.com/abhinavsingh/proxy.py#unable-to-load-plugins

moham96

moham96 commented on Nov 11, 2021

@moham96
Author

You can also try:

PYTHONPATH=/home/mohammad/repos/plugintest proxy --plugins proxyPlugin.proxyPlugin

Ref: https://github.com/abhinavsingh/proxy.py#unable-to-load-plugins

using PYTHONPATH works fine, but the entry in the README file suggest that it is possible to use the absolute path without setting the PYTHONPATH variable

abhinavsingh

abhinavsingh commented on Nov 11, 2021

@abhinavsingh
Owner

using PYTHONPATH works fine, but the entry in the README file suggest that it is possible to use the absolute path without setting the PYTHONPATH variable

Correct. In my tests it worked while documenting. But IIRC, I was using absolute imports in my external plugin. Probably, I we must update the README with relevant context. If you wish, please send out a PR.

abhinavsingh

abhinavsingh commented on Nov 18, 2021

@abhinavsingh
Owner

TypeError: the 'package' argument is required to perform a relative import for '.home.mohammad.repos.plugintest.proxyPlugin'

Can double confirm, absolute path currently doesn't work as advertised. I think code got broken at some point, because this used to work. Let's keep this open until fixed. Thank you again for reporting it.

added
PluginRelated to plugin code (not core)
and removed
PluginRelated to plugin code (not core)
on Nov 18, 2021
pinned this issue on Nov 18, 2021
removed their assignment
on Dec 27, 2021
unpinned this issue on Jan 25, 2022
pinned this issue on Jan 26, 2022
LanguidBasil

LanguidBasil commented on May 30, 2025

@LanguidBasil

Are there plans to fix this?

I too tried absolute import because proxy.py docker container couldn't find a proxy I was passing

For context, here is proxy.py service in my docker-compose.yml file:

  proxy_test_server:
    image: abhinavsingh/proxy.py:v2.4.10
    ports: 
      - "8899:443"
    command: >
      --cert-file /certs/cert.pem
      --key-file /certs/key.pem
      --hostname 0.0.0.0
      --port 443
      --plugins /plugins/https_to_http_plugin.Plugin
    volumes:
      - ./src/tests/proxy_test_server/cert.pem:/certs/cert.pem
      - ./src/tests/proxy_test_server/key.pem:/certs/key.pem
      - ./src/tests/proxy_test_server/https_to_http_plugin.py:/plugins/https_to_http_plugin.py

And my https_to_http_plugin.py:

from typing import override
from proxy.http.parser import HttpParser
from proxy.http.proxy import HttpProxyBasePlugin


class Plugin(HttpProxyBasePlugin):
    @override
    def before_upstream_connection(self, request: HttpParser) -> HttpParser:
        if request.port == 443:
            self.flags.upstream = f"http://{request.host}:80"
        return request
abhinavsingh

abhinavsingh commented on Jun 11, 2025

@abhinavsingh
Owner

@LanguidBasil If you intend to use absolute paths, simply add that path to PYTHONPATH variable and it should pick it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugBug report in proxy serverGood First IssueIssues for new contributors to pick-up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @abhinavsingh@moham96@LanguidBasil

        Issue actions

          [Plugin] Unable to load using absolute path · Issue #726 · abhinavsingh/proxy.py