AWS Python Lambda giving Error: Invalid 'cross-device link' for patsy-0.5.1

Hi everyone!

I’m trying to deploy Python lambda via Windows Powershell. I am trying to eventually get statsmodels into AWS Lambda however I am running into an ‘Invalid cross-device link’ and ‘Permission denied’ error with the patsy-0.5.1 Python module. If anyone knows an easier way to get statsmodels into AWS please let me know (maybe with layers - but then the size is too big?).
Anyways! Here is my issue:

After calling: serverless deploy --force -v

This is the error I am getting:

Error --------------------------------------------------
STDERR: ERROR: Exception:
Traceback (most recent call last):
File “/var/lang/lib/python3.7/shutil.py”, line 566, in move
os.rename(src, real_dst)
OSError: [Errno 18] Invalid cross-device link: ‘/tmp/pip-target-yxcd5g32/lib/python/patsy-0.5.1.dist-info’ -> '/var/task/patsy-0.5.1.dist-info’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/var/lang/lib/python3.7/site-packages/pip/_internal/cli/base_command.py”, line 153, in _main
status = self.run(options, args)
File “/var/lang/lib/python3.7/site-packages/pip/_internal/commands/install.py”, line 505, in run
options.target_dir, target_temp_dir, options.upgrade
File “/var/lang/lib/python3.7/site-packages/pip/_internal/commands/install.py”, line 563, in _handle_target_dir
target_item_dir
File “/var/lang/lib/python3.7/shutil.py”, line 577, in move
symlinks=True)
File “/var/lang/lib/python3.7/shutil.py”, line 324, in copytree
os.makedirs(dst)
File “/var/lang/lib/python3.7/os.py”, line 221, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/var/task/patsy-0.5.1.dist-info’

  at C:\forecast\node_modules\serverless-python-requirements\lib\pip.js:320:13
  at Array.forEach (<anonymous>)
  at installRequirements (C:\forecast\node_modules\serverless-python-requirements\lib\pip.js:307:28)
  at installRequirementsIfNeeded (C:\forecast\node_modules\serverless-python-requirements\lib\pip.js:551:3)
  at ServerlessPythonRequirements.installAllRequirements (C:\forecast\node_modules\serverless-python-requirements\lib\pip.js:630:29)
  at ServerlessPythonRequirements.tryCatcher (C:\forecast\node_modules\bluebird\js\release\util.js:16:23)
  at Promise._settlePromiseFromHandler (C:\forecast\node_modules\bluebird\js\release\promise.js:547:31)
  at Promise._settlePromise (C:\forecast\node_modules\bluebird\js\release\promise.js:604:18)
  at Promise._settlePromise0 (C:\forecast\node_modules\bluebird\js\release\promise.js:649:10)
  at Promise._settlePromises (C:\forecast\node_modules\bluebird\js\release\promise.js:729:18)
  at _drainQueueStep (C:\forecast\node_modules\bluebird\js\release\async.js:93:12)
  at _drainQueue (C:\forecast\node_modules\bluebird\js\release\async.js:86:9)
  at Async._drainQueues (C:\forecast\node_modules\bluebird\js\release\async.js:102:5)
  at Immediate.Async.drainQueues [as _onImmediate] (C:\forecast\node_modules\bluebird\js\release\async.js:15:14)
  at processImmediate (internal/timers.js:439:21)
  at process.topLevelDomainCallback (domain.js:130:23)

Thanks for any help!!

Hi SuprNerd, and welcome to the community. It looks as if the library you are trying to use is attempting to create some kind of symlink on the Lambda. The structure of a Lambda though does not necessarily allow this. A quick Google of Invalid cross-device link seems to indicate that symlinking across partitions or devices is not possible, which must mean that /tmp and /var are not on the same device.

This is just a guess however as I am no Python expert.

i am running into a similar error.Any solutions or help?
but it’s with any python package i try to use i get invalid cross link error

Any update on this as of yet? Still struggling with the issue haha

just make sure all your files exists in the same directory.
Especially we you run sls deploy make sure you run that with proper permission.
just do ls -la you will get list of all hidden files. .serveless directory must be create in the same filesystem.
if its created in the root and you have other files in your user filesystem.Just try avoiding sudo.
if sls deploy (without sudo) throws error remove .serveless directory and again run the sls deploy without sudo.
The key is all your files must be in same filesystem.
This solved my error!
Hope this helps!

Can I ask what are the contents of your .serverless directory? I just have requirements.txt in there…

It is created during runtime.
when you do sls deploy

I had a similar issue. I looked looked up the error OSError: [Errno 18] Invalid cross-device link. Some people said it was an issue with pip so I tried pip install --upgrade pip.
Here’s what did work for me. (Also I am assuming you are using the serverless-python-requirements plugin.)

rm -rf /venv  //Deleted my virtual environment
python3 -m venv venv //Re created my venv
source venv/bin/activate //Activated my venv
pip install --upgrade pip //Upgraded pip. I'm not sure if that was necessary but I did it.
pip install -r requirements.txt //Installed everything again

After doing all that I deployed again and it worked.
It seems like the issue was something in my virtual environment

Any help here?, still with that error (try all you suggested here)