Issue importing BeautifulSoup into a script

I’m trying to get BeautifulSoup imported into a mitmdump script and am stuck. I’ve verified that b4 is installed in the same python runtime that mitmdump is executing and even tried forcing the path using os.environ[‘PYTHONPATH’] inside the script. doesn’t work.

Fails with this error:

Script error: Traceback (most recent call last):
  File "", line 9, in <module>
    from b4 import BeautifulSoup
ImportError: No module named 'b4'

Please help!

import sys
from mitmproxy import io
import os

    #import pdb; pdb.set_trace()

    from b4 import BeautifulSoup

    class Writer:
        def __init__(self, path):
            self.f = open(path, "wb")

        def response(self, flow):
            self.f.write( bytes(flow.request.url,'utf-8'))
            soup = BeautifulSoup(flow.response.content, 'html.parser')
            self.f.write( flow.response.content )

    def start():
        return Writer("out")

Never mind, I’m an idiot, looking at the mitmscript that brew installs, I saw “cellar” in the file path and skipped to the assumption that it was brew’s copy of python rather than it’s own executable.


first in my path and correctly setting PYTHONPATH to

and then running
pip3 install BeautifulSoup4

fixed the issue.