Zappa icon indicating copy to clipboard operation
Zappa copied to clipboard

Unable to import module 'handler': No module named werkzeug.wrappers

Open chennav opened this issue 8 years ago • 34 comments

I tried installing Werkzeug but it seems like it is already installed ~/dev/testzappa% sudo pip install Werkzeug Requirement already satisfied (use --upgrade to upgrade): Werkzeug in /usr/lib/python2.7/site-packages

I guess it isn't packaged into the lambda. I get the following error in my lambda cloudwatch logs Unable to import module 'handler': No module named werkzeug.wrappers

chennav avatar Apr 20 '16 16:04 chennav

Zappa needs to be used in a virtualenvironment. You shouldn't use sudo and pip together.

Miserlou avatar Apr 20 '16 16:04 Miserlou

well, I've tried it out in a virtualenv, still get the same error. Here's what I did,

  1. Created a new EC2 instance, installed pip and virtualenv
  2. Copied credentials to ~/.aws/credentials.
  3. [ec2-user@trialinstance dev]$ virtualenv testzappa
  4. [ec2-user@trialinstance dev]$ source testzappa/bin/activate
  5. Installed zappa (testzappa)[ec2-user@trialinstance dev]$ pip install zappa
  6. cd testzappa and Added demo code to my_app.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()
  1. Added zappa_settings.json
{
    "dev": {
       "s3_bucket": "test_app_deployment",
       "app_function": "my_app.app",
       "parameter_depth": 1
    }
}
  1. Deployed code (testzappa)[ec2-user@trialinstance testzappa]$ zappa deploy dev
  2. I get the same error Unable to import module 'handler': No module named werkzeug.wrappers
  3. Tried installing werkzeug
(testzappa)[ec2-user@trialinstance testzappa]$ pip install werkzeug but it already exists
Requirement already satisfied (use --upgrade to upgrade): werkzeug in ./lib/python2.7/dist-packages
(testzappa)[ec2-user@trialinstance testzappa]$ `

chennav avatar Apr 20 '16 18:04 chennav

I think this issue is actually this issue (#63). Can you rename your virtualenv to env (or probably anything that isnt the same name as your project) and see what happens?

collingreen avatar Apr 20 '16 20:04 collingreen

I agree

bjinwright avatar Apr 20 '16 23:04 bjinwright

Update -- this also happens when flask is not installed in the virtualenv.

collingreen avatar Jun 22 '16 20:06 collingreen

@collingreen THANK YOU !

ceefour avatar Aug 31 '17 18:08 ceefour

Seeing this same issue while trying to use zappa with apistar (0.3.9).

(.venv) $ zappa tail
Calling tail for stage dev..
[XXX] Unable to import module 'handler': No module named 'werkzeug'
[XXX] Unable to import module 'handler': No module named 'werkzeug'
^C
(.venv) $ python -m pip install werkzeug
Requirement already satisfied: werkzeug in ./.venv/lib/python3.6/site-packages

I've rebuilt my venv several times with no effect...no sure what to try next. (my virtrualenv (.venv) is not related or named anything that would conflict with my project..)

apistar==0.3.9
.
.
.
Werkzeug==0.12
whitenoise==3.3.1
wsgi-request-logger==0.4.6
zappa==0.44.3

monkut avatar Oct 13 '17 09:10 monkut

@Miserlou any advice for the

Unable to import module 'handler': No module named 'werkzeug'

error ?

Getting this same issue trying to deploy a django service. Flask & Werkzeug are installed in the venv, name is not conflicting.

Sniedes722 avatar Nov 06 '17 22:11 Sniedes722

@Sniedes722 Downgrading to 0.44.3 fixed the issue for me.

jjorissen52 avatar Nov 07 '17 19:11 jjorissen52

Can anybody figure out where the regression was committed? Or what it is?

Miserlou avatar Nov 07 '17 20:11 Miserlou

This happens to me with slim_handler=True. I wonder if this is causing the issue https://github.com/uiri/toml/issues/129

When the handler venv pip install tries to happen it tries to install toml==0.9.3 but it can't find it. Maybe we could ask toml to bump their version.

  Could not find a version that satisfies the requirement toml==0.9.3 (from versions: 0.6.0, 0.6.5, 0.7.0, 0.7.1, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.9.3.1)
No matching distribution found for toml==0.9.3

I asked the maintainer to cut a new release in that issue. It seems weird stuff is happening because v0.9.3.1 on pypi still references itself as 0.9.3 in code. So if this is the problem we either wait for upstream or temporarily downgrade to 0.9.2 :/

jwkvam avatar Nov 17 '17 01:11 jwkvam

Same issue, I'm trying to use slim_handler = True for my flask-ask skill but I get the same error message. Really hope they fix this soon!

daviskeene avatar Nov 19 '17 01:11 daviskeene

Would this be solved if Zappa didn't peg requirement versions?

mcrowson avatar Dec 04 '17 18:12 mcrowson

Im failing to update my zappa projects due to this " Could not find a version that satisfies the requirement toml==0.9.3 (from versions: 0.6.0, 0.6.5, 0.7.0, 0.7.1, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.9.3.1) No matching distribution found for toml==0.9.3". Unfortunately it shows the error but still deploys it; breaking the previously working api. Is there a way for the deployment to fail if some requirements aren't met?

cotzanium avatar Dec 04 '17 18:12 cotzanium

The same issue with toml. Can this be manually solved?

technolingo avatar Dec 20 '17 02:12 technolingo

toml has released 0.9.4

technolingo avatar Dec 24 '17 08:12 technolingo

@Miserlou Is it caused by toml?

technolingo avatar Jan 08 '18 06:01 technolingo

Getting a similar error: Unable to import module 'handler': attempted relative import with no known parent package. (And earlier also: Unable to import module 'handler': No module named 'werkzeug' )

Windows 10, Python 3.6.2. Virtual environment name does not conflict. I have deleted and recreated several times, most recently with --no-site-packages. Spot checked some and found all are in .\venv\Lib\site-packages. handler.py of course is there too. Downgraded Zappa from 0.45.1 to 0.44.3. My project does include toml 0.9.4.

Zappa settings:

{
    "dev": {
        "app_function": "my_app.app",
        "aws_region": "us-west-2",
        "project_name": "my_app",
        "manage_roles": true,
        "runtime": "python3.6",
        "s3_bucket": "my_app-zappa-temp",
        "apigateway_enabled": false
    }
}

Any suggestions? I am stumped.

ariemer avatar Mar 13 '18 04:03 ariemer

Ultimately, I fixed this by doing a clean deployment with a new function name and keeping more default settings in zappa-settings, particularly apigateway_enabled.

ariemer avatar Apr 24 '18 00:04 ariemer

Getting this too, unfortunately, and am pretty stumped. Amazon Linux, Python 3.6. All my packages are inside of my virtualenv, which is named env. I had to downgrade to pip 9.0.3 because of a different error but can't seem to figure out why this one is happening...

aphelionz avatar May 25 '18 21:05 aphelionz

@aphelionz I had to hack my pips on Lambda. Not sure if that's what you mean by Amazon Linux, exactly, but just in case it's helpful:


# AWS broke pip. )-:
pip install --user --ignore-installed boto3 six virtualenv pip==9.0.3
export PYTHONPATH=$HOME/.local

# ...

$HOME/.local/bin/pip install --user --upgrade virtualenv

# now build the venv
rm -rf /tmp/venv
virtualenv /tmp/venv
. /tmp/venv/bin/activate
/tmp/venv/bin/pip install --ignore-installed pip==9.0.3

PYTHONPATH=`pwd`:$PYTHONPATH

/tmp/venv/bin/pip install --no-cache-dir --upgrade -r requirements.txt

scoates avatar May 25 '18 21:05 scoates

hmm same error :( the only thing i changed in your code was, in creating the virtualenv:

virtualenv /tmp/venv -p python3

aphelionz avatar May 26 '18 10:05 aphelionz

Could this be caused by https://github.com/Miserlou/Zappa/issues/905 ?

At least for me it was fixed by copying contents of dist-packages to site-packages with: cp -R ./.venv/lib/python3.6/dist-packages/* ./.venv/lib/python3.6/site-packages/

dins avatar Jun 12 '18 11:06 dins

I'm hitting this as well... if I unzip the handler_*.zip file the packaging deploys I'm noticing that werkzeug is not installed. This is all that's in there:

__init__.py
_sqlite3.so
django_zappa_app.py
handler.py
package_info.json
zappa
zappa-0.46.1.dist-info
zappa_settings.pyp

sheats avatar Jun 22 '18 17:06 sheats

I can reliably reproduce this issue using this repo: https://github.com/sheats/zappa_issue

make reproduce
make tail
[1529698001385] Unable to import module 'handler': No module named 'werkzeug'
[1529698009786] Unable to import module 'handler': No module named 'werkzeug'

sheats avatar Jun 22 '18 20:06 sheats

Turns out my problem was not activating the venv properly, this fixed it: https://github.com/sheats/zappa_issue/commit/331f69870320b31f6166731585ec7ae7dd8680fb

I thought the entrypoint.sh was activating the venv correctly but apparently not.

sheats avatar Jun 24 '18 02:06 sheats

Copying the contents of dist-packages to site-packages in my virtualenv as @dins suggested fixed the issue for me. I'm using Amazon Linux, and installed Werkzeug through Flask.

xRichieGarcia avatar Mar 29 '19 00:03 xRichieGarcia

I just solved this issue for me a few minutes ago. I deleted my env using python3.6 and created a new one using 3.7. once in the new env, and after pip install zappa and pip install flask all is well.

Jbrown95 avatar Jun 07 '19 05:06 Jbrown95

Make sure you run pip install zappa and pip install flask AFTER creating and activating your virtual environment (running virtualenv venv and source venv/bin/activate). This fixed the error for me.

humphriesjm avatar Apr 01 '20 06:04 humphriesjm

Make sure you run pip install zappa and pip install flask AFTER creating and activating your virtual environment (running virtualenv venv and source venv/bin/activate). This fixed the error for me.

I did a 'pip freeze >requirements.txt' and verified both zappa and flask were present in requirements.txt prior to 'zappa init'. Still having this problem.

overcoil avatar Apr 01 '20 22:04 overcoil

I just solved this issue for me a few minutes ago. I deleted my env using python3.6 and created a new one using 3.7. once in the new env, and after pip install zappa and pip install flask all is well.

This worked for me, too. Was using Cloud9 IDE, which had Python3.6, and had to install Python3.7 first, then create a new venv using Python3.7, reinstall everything, and everything works after that.

mattsahn avatar Apr 11 '20 15:04 mattsahn

I did a 'pip freeze >requirements.txt' and verified both zappa and flask were present in requirements.txt prior to 'zappa init'. Still having this problem.

Same here. Windows 10, py 3.7, tried everything recommended here and then some, still no luck. if I run python after activating my env and run import werkzeug it imports fine, but then running zappa deploy dev it says it can't find werkzeug.

isodrosotherm avatar May 29 '20 19:05 isodrosotherm

For me, I was creating separate 'run' steps in a CircleCI workflow/job for sourcing the virtualenv and running zappa update. But eventually bringing all shell commands under same 'run' step made the virtualenv persistent and the issue was solved.

yeaminhyperanna avatar Dec 09 '20 01:12 yeaminhyperanna