Repository: mitmproxy/mitmproxy

Issue:
mitmdump throw ImportError: No module named 'mitmproxy.script'
##### Steps to reproduce the problem:

1. Download mitmproxy binary from https://github.com/mitmproxy/mitmproxy/releases/download/v0.18.2/mitmproxy-0.18.2-linux.tar.gz
2. Extract and then run : ~$ ./mitmdump -T --host -s inline_script.py
3. mitmdump throw exception:

```
Script error: Traceback (most recent call last):
  File "/home/john/inline_script.py", line 1, in <module>
    from mitmproxy.script import concurrent
ImportError: No module named 'mitmproxy.script'
```


##### inline_scripy.py content:
```
from mitmproxy.script import concurrent

@concurrent
def request(context, flow):
   print("Request")

```
##### Any other comments? What have you tried so far?
I tried to install mitmproxy from pip. And rerun with inline_script.py: ~$ mitmdump -T -s inline_script.py
It runs normally.


##### System information
./mitmdump --sysinfo
Mitmproxy version: 0.18.2
Python version: 3.5.2
Platform: Linux-3.13.0-85-generic-x86_64-with-debian-jessie-sid
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Linux distro: debian jessie/sid 

mitmdump throw ImportError: No module named 'mitmproxy.script'
##### Steps to reproduce the problem:

1. Download mitmproxy binary from https://github.com/mitmproxy/mitmproxy/releases/download/v0.18.2/mitmproxy-0.18.2-linux.tar.gz
2. Extract and then run : ~$ ./mitmdump -T --host -s inline_script.py
3. mitmdump throw exception:

```
Script error: Traceback (most recent call last):
  File "/home/john/inline_script.py", line 1, in <module>
    from mitmproxy.script import concurrent
ImportError: No module named 'mitmproxy.script'
```


##### inline_scripy.py content:
```
from mitmproxy.script import concurrent

@concurrent
def request(context, flow):
   print("Request")

```
##### Any other comments? What have you tried so far?
I tried to install mitmproxy from pip. And rerun with inline_script.py: ~$ mitmdump -T -s inline_script.py
It runs normally.


##### System information
./mitmdump --sysinfo
Mitmproxy version: 0.18.2
Python version: 3.5.2
Platform: Linux-3.13.0-85-generic-x86_64-with-debian-jessie-sid
SSL version: OpenSSL 1.0.2j  26 Sep 2016
Linux distro: debian jessie/sid 



Discussion:
This is a bug in the release builds.  Nothing uses mitmproxy.script.concurrent within mitmproxy itself, so PyInstaller doesn't include it in the packaged application.  If you don't need functioning HTTP/2 (I see you're on Ubuntu 14.04, whose provided openssl is too old to have ALPN, and thus you can't use HTTP/2), you can install mitmproxy via pip for functioning concurrency.
We can fix this by either adding `from . import script` to `mitmproxy/__init__.py` or by declaring it as a hiddenimport in pyinstaller. Exposing something useful in `__init__.py` might be useful anyway... @mitmproxy/devs, any preferences?
:+1: to `from . import script` in `__init__.py`
This is a bug in the release builds.  Nothing uses mitmproxy.script.concurrent within mitmproxy itself, so PyInstaller doesn't include it in the packaged application.  If you don't need functioning HTTP/2 (I see you're on Ubuntu 14.04, whose provided openssl is too old to have ALPN, and thus you can't use HTTP/2), you can install mitmproxy via pip for functioning concurrency.
We can fix this by either adding `from . import script` to `mitmproxy/__init__.py` or by declaring it as a hiddenimport in pyinstaller. Exposing something useful in `__init__.py` might be useful anyway... @mitmproxy/devs, any preferences?
:+1: to `from . import script` in `__init__.py`

Resolving patch:
diff --git a/mitmproxy/__init__.py b/mitmproxy/__init__.py
--- a/mitmproxy/__init__.py
+++ b/mitmproxy/__init__.py
@@ -0,0 +1,3 @@
+# https://github.com/mitmproxy/mitmproxy/issues/1809
+# import script here so that pyinstaller registers it.
+from . import script  # noqa
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -1,8 +1,8 @@
-from setuptools import setup, find_packages
-from codecs import open
 import os
+import runpy
+from codecs import open
 
-from mitmproxy import version
+from setuptools import setup, find_packages
 
 # Based on https://github.com/pypa/sampleproject/blob/master/setup.py
 # and https://python-packaging-user-guide.readthedocs.org/
@@ -12,9 +12,11 @@
 with open(os.path.join(here, 'README.rst'), encoding='utf-8') as f:
     long_description = f.read()
 
+VERSION = runpy.run_path(os.path.join(here, "mitmproxy", "version.py"))["VERSION"]
+
 setup(
     name="mitmproxy",
-    version=version.VERSION,
+    version=VERSION,
     description="An interactive, SSL-capable, man-in-the-middle HTTP proxy for penetration testers and software developers.",
     long_description=long_description,
     url="http://mitmproxy.org",


Test patch: