question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[python] 3rd party modules

See original GitHub issue

currently, for 3rd party module, we have to do pip install -t . on the same directory as template.yaml and the main python application file. This pollutes the working directory when you have a lot of modules. Is there a better way to handle this? maybe on the file zipping of a subdirectory?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:18
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

11reactions
randyjohnstoncommented, Sep 13, 2017

Could you please clarify how to set up template.yml for Python module with dependencies? The example template.yml does not make sense and references a non-existent zip file.

7reactions
heitorlessacommented, Nov 17, 2017

I’ll make a proper PR as soon as I get time to clean comments up and test in other OSes, but if that helps here’s what I came up with (tested in OSX only) to have dependencies installed + hot-reloading experience similar to NodeJS.

Essentially it takes advantage of hard links and clones the entire source code/folders (lib, etc.) onto a folder called dev. All you have to do is to change your SAM template to point to <source_code>/dev.

Makefile - Hardcoded for Python 3.6 and pip :

# VirtualEnv vars
SERVICE ?= users
VENV := ${SERVICE}/.venv
VENV_LIBS := $(VENV)/lib/python3.6/site-packages

# help credit (https://gist.github.com/prwhite/8168133)
help: ## Show this help

	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

target: help

	exit 0

clean: ## -> Deletes current virtual env environment

	$(info "[-] Who needs all that anyway? Destroying environment....")
	rm -rf ./$(VENV)
	rm -rf ./$(SERVICE)/dev
	rm -rf ./*.zip

build-dev: _check_service_definition ## -> creates dev folder and install dependencies (requirements.txt) into dev/ folder

	echo "[+] Cloning ${SERVICE} directory structure to ${SERVICE}/dev"
	rsync -a -f "+ */" -f "- *" -f "- dev/" ${SERVICE}/ ${SERVICE}/dev/
	echo "[+] Cloning source files from ${SERVICE} to ${SERVICE}/dev"
	find ${SERVICE} -type f \
			-not -name "*.pyc" \
			-not -name "*__pycache__" \
			-not -name "requirements.txt" \
			-not -name "event.json" \
			-not -name "dev" | cut -d '/' -f2- > .results.txt
	@while read line; do \
		ln -f ${SERVICE}/$$line ${SERVICE}/dev/$$line; \
	done < .results.txt
	echo "[+] Installing dependencies into dev/"
	pip3.6 install \
		--isolated \
		--disable-pip-version-check \
		-Ur $(SERVICE)/requirements.txt -t ${SERVICE}/dev/

_check_service_definition:

	echo "[*] Checking whether service $(SERVICE) exists..."

# SERVICE="<name_of_service>" must be passed as ARG for target or else fail
ifndef SERVICE
	echo "[!] SERVICE argument not defined...FAIL"
	echo "[*] Try running 'make <target> SERVICE=<service_name>'"
	exit 1
endif

ifeq ($(wildcard $(SERVICE)/.),)
	echo "[!] $(SERVICE) folder doesn't exist"
	exit 1
endif

ifeq ($(wildcard $(SERVICE)/requirements.txt),)
	echo "[!] Pip requirements file missing from $(SERVICE) folder..."
	exit 1
endif

run-dev: ## -> Run SAM Local API GW
	sam local start-api

Folder structure - Sample with multiple private packages (vendor/db, vendor/dispatcher) and external dependencies (boto3, requirements, etc.)

.
β”œβ”€β”€ Makefile
β”œβ”€β”€ bootstrap_dynamodb.py
β”œβ”€β”€ setup.cfg
β”œβ”€β”€ template.yaml
└── users
    β”œβ”€β”€ requirements.txt
    β”œβ”€β”€ users.py
    β”œβ”€β”€ something_else.py
    └── vendor
        β”œβ”€β”€ db
        β”‚Β Β  └── __init__.py
        β”œβ”€β”€ dispatcher
        β”‚Β Β  β”œβ”€β”€ __init__.py
        β”œβ”€β”€ helper
        β”‚Β Β  └── __init__.py
        └── users
            β”œβ”€β”€ __init__.py

SAM

    UsersFunction:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: ./users/dev/
        Handler: users.lambda_handler
        Runtime: python3.6
        Environment:
          Variables:
            TABLE_NAME: "users"
        Events:
          CatchAll:
            Type: Api
            Properties:
              Path: /{proxy+}
              Method: any

Usage

make build-dev SERVICE="users"

Where users is where my source code is as it’s a users service sample.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Installing Python Modules β€” Python 3.11.1 documentation
Starting with Python 3.4, it defaults to installing pip into all created virtual environments. virtualenv is a third party alternative (and predecessor) to...
Read more >
11 Python Libraries and Modules Every Developer Should Know
Luckily Python supports a plethora of modules and libraries. Python has built-in modules as well as third-party libraries and modules for the development....
Read more >
PyPI Β· The Python Package Index
Find, install and publish Python packages with the Python Package Index. Search PyPI. Search. Or browse projects. 423,406 projects. 4,036,329 releases.
Read more >
10 Interesting modules in Python to play with - GeeksforGeeks
The fact that it can support the vast majority of third-party modules, it acts as a cherry on the cake. There are some...
Read more >
Installing Python Third Party Libraries - Studytonight
Installing third party python packages Β· For this purpose, download the compressed (tar.gz or . Β· Extract the compressed file (to extract tar.gz...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found