Closed jacobevans closed 5 years ago
Is the virtualenv active? serverless-python-requirements is only for deployment, and sls invoke local
only uses a virtualenv if it's active by using the VIRTUAL_ENV
environment variable. It can't magically find it, since it could be anywhere.
@dschep Yes, sorry, I should have specified. it is activated with source venv/bin/activate
(venv
is where the venv is). I've double and triple checked that.
Also, I don't believe I'd be able to import the library in the python shell otherwise.
$ which python
.../projectdir/venv/bin/python
So, it does seem like the invoke local
is somehow not operating inside of the virtualenv properly. if I add the following to the top of handler.py
, it makes it work:
import sys
sys.path.append('./venv/lib/python3.6/site-packages')
Here is what sys.path
looks like before I append that path:
['/usr/local/lib/node_modules/serverless/lib/plugins/aws/invokeLocal',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/site-packages',
'.']
For comparison, here's what I see if I print sys.path
in a python shell that I open inside my project directory with the venv activated:
['',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python36.zip',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
'/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload',
'/full/path/to/projectdir/venv/lib/python3.6/site-packages']
I've figured out the issue. The venv
module doens't create a binary at $VIRTUAL_ENV/bin/pythonX.Y
like virtualenv
does. I'm creating PR to fix it :smile:
As a work around for now, create a python3.6 symlink:
cd venv/bin
ln -s python3 python3.6
Not sure if this is related to the problem mentioned above but If anyone comes here through Google try this:
npm install serverless --save-dev;
./node_modules/serverless/bin/serverless invoke local --function transferContent --data '{"content_type":"scholarship"}'
This is a Bug Report
Description
I have a aws/python3.6 serverless project (with serverless-python-requirements). I've added other libraries with no issue, but I'm trying to use the python-dateutil module, and experiencing issues with
sls invoke local
, and onlysls invoke local
.When I try to invoke a function locally, I get this error:
This is the relevant line at the beginning of my handler.py:
However, if I deploy and do a regular
sls invoke
i get no such error. Additionally if I open a python shell and try the exact same import statement, it works just fine.I'm on a mac, using homebrew, and a venv created with:
python3 -m venv
versions, etc:
Additional Data