Can a module import of a pex from pantsbuild be exposed? - python

Cross-post from https://github.com/pantsbuild/pex/issues/1181 as I didn't really know where to ask this.
I'm using Nginx Unit, which has an ASGI configuration for my FastAPI application. It's working fine, but I have no clue how to use it with a PEX.
I've attached it below, but the salient point is that there is a Python plugin for Nginx Unit which is looking for a "module" and a "callable".
What this looks like now is "apigateway.main:app" similar to uvicorn or whatever other server implementation you're using. Unlike uvicorn, gunicorn, or whatever - I don't think I can package any other tool in my pex file and use PEX_SCRIPT.
Is there any suggestion on if/how I can expose a module and variable outside of the pex?
{
"listeners": {
"*:80": {
"pass": "routes"
}
},
"routes": [
{
"action": {
"pass": "applications/api"
}
}
],
"applications": {
"api": {
"type": "python 3.9",
"path": "/app",
"module": "apigateway.main",
"callable": "app",
"limits": {
"requests": 100
},
"processes": {}
}
},
"access_log": "/var/log/access.log"
}

This was answered in the Github ticket (https://github.com/pantsbuild/pex/issues/1181):
All the traditional language here is meant to segue to the recent venv
pex tool feature released with Pex 2.1.22. If you build your PEX file
with venv support (add --include-tools to the Pex command line) then
you gain the ability to create a venv from your PEX file. Doing so is
a one-time manual step on the target machine or image:
PEX_TOOLS=1 ./my.pex venv /app That will create a virtual environment
containing your app and its dependencies under /app. You can point
Nginx Unit there with home. I've done all this over at
https://github.com/jsirois/pex-issues-1181 to prove it works.

Related

Use debugger in Visual Studio Code with Docker

I am working on a project where we use Docker to develop an application. Currently I'm running my programmes by typing docker-compose run --rm app in the console.
Then I can run my scrip like this root#ce4d1325fb94:/home/app# python3 example.py. But I would like to use my debugger. Normally I can use it be pressing F5.
I have found Debug containerized apps and Debug Python within a container.
But id does not work.
I get the error Could not find the task 'docker-run: debug' or ConfigError: The project 'Projekte_635c102' is not a valid java project.
What am I doing wrong? Note: I have no clue what Docker exactly is. I was told to use it.
I've done everything exactly as #MingJie-MSFT said. I’m still getting the same Could not find the task 'docker-run: debug' error. I can't create an example since I have no clue what I'm doing. I never worked with Docker or got an explanation on how to use it. I was just told to use the existing code. I can give you the Dockerfile if that is of any help:
# syntax=docker/dockerfile:1
FROM ubuntu:latest
WORKDIR /home/app
# Pass 8 and 63 to tzdata to set up the default time zone
RUN apt-get update && echo 8 63 | apt-get install -y tzdata
RUN apt-get install -y python3-pip libgdal-dev gdal-bin default-jre && pip3 install -r requirements.txt
I also tried to just type docker-run: debug in my console while being in the container.
root#cd70584fb560:/home/app# docker-run debug
and I've got
bash: docker-run: command not found
I've tried to include a "tasks" in my launch.json file. But again no luck. I thought maybe it's trying to find the "preLaunchTask" specified in the launch.json file before. Again I am absolutely clueless here. How is this launch.json file supposed to work? What are those commands supposed to do? I only know a bit of Python and barely anything about using command-line commands.
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
],
"tasks": [
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"python": {
"args": ["runserver", "0.0.0.0:8000", "--nothreading", "--noreload"],
"file": "manage.py" // how to I change this to current file?
}
}
]
}
Read the documentation about debug in Docker.
ConfigError: The project 'Projekte_635c102' is not a valid java project.
It identifies your project as a Java project. It seems that your launch.json file has an error. Check your launch.json file. It should look like this:
{
"configurations": [
{
"name": "Docker: Python - Django",
"type": "docker",
"request": "launch",
"preLaunchTask": "docker-run: debug",
"python": {
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}
],
"projectType": "django"
}
}
]
}

How to run python module command from VSCode tasks

I am trying to run my alembic migration which is in a subdirectory so I am writing VSCode tasks for it but when I run it I get command not found error:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/my_sub_directory"
}
}
]
}
How can I run it under my virtual environment?
Activating the virtual environment, then installing alembic, you can run alembic revision directly, that's because it's in the virtual environment and able to call the module.
So, when executing alembic command in task, you also need to activate env first.
Copy the activation command and paste it before alembic and separated by ; , the task should be ran successfully:
Unfortunately, I've not been able to find a solution that doesn't require hardcoding the virtualenv path.
I believe an extension would be able to solve this, possibly by adding support for python in the tasks's type field.
A workaround
Use launch configurations instead of tasks for running python scripts. It is a bit weird, but it works.
Here's your code adapted to launch.json schema:
{
"version": "0.2.0",
"configurations": [
{
"name": "Alembic autogenerated revision",
"type": "python",
"request": "launch",
"module": "alembic",
"args": ["revision", "--autogenerate", "-m", "init"],
"cwd": "${workspaceFolder}/my_sub_directory"
}
]
}
Also, consider using input variables for specifying revision messages and other variable things.
Thanks for your responses. To resolve this we can use the selected interpreter path and run the module.
{
"label": "Alembic autogenerated revision",
"type": "shell",
"command": "${command:python.interpreterPath} -m alembic revision --autogenerate -m init",
"options": {
"cwd": "${workspaceFolder}/ts_tools"
}
},
Reference: https://github.com/microsoft/vscode-python/issues/18728
Regards.

Attach a debugger to Azure Function App running in a container

I am curious to know if anyone successfully attached a debugger to a python script -
which is actually an Azure Function App running in a container.
I have all the VSCODE extensions but still can't seem to connect to the running container.
I've run the container from docker-compose.debug and from Bash cmd:
$ docker run -p 5678:5678 -it -e AzureWebJobsStorage="UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://host.docker.internal" nfunc:latest
NB I have to set an environment variable AzureWebJobsStorage for the storage.
My launch.json as follows:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
What am I missing please ..
Can Functions Apps be debugged at all in a container?
Why does it not hit a breakpoint ?
Thank you
In this case, I believe you'll have to do the profiling. You will have to follow these:
If its not a blessed image, then first you would have to install SSH
if you want to get into the container.
Then you will have to make use of tools such as cProfile or other related python modules to profile the code.
Here is a documentation for windows application. You might want to take a look :
https://azureossd.github.io/2017/09/01/profile-python-applications-in-azure-app-services/index.html
This issue has been tracked : https://github.com/Azure/azure-functions-docker/issues/17

can't cancel python build in sublime3 using virtualenv

I can't cancel a build in python in Sublime Text 3. I tried canceling using the default and custom key bindings, and even using the command pallet.
I'm using a virtual environment via virtualenv. That seems to be the only difference between my current development platform and other ones I've been using.
Default(windows).sublime-keymap - User:
I tried using all of these, I know the last one was a stretch. None worked
[
{ "keys": ["alt+b"], "command": "exec", "args": {"kill": true} },
// { "keys": ["ctrl+alt+z"], "command": "build" }
{ "keys": ["alt+b"], "command": "cancel_build" },
{ "keys": ["alt+b"], "command": "Build: Cancel" }
]
Using the command prompt:
Without virtualenv:
Using the default python build, without the python environment, seems to work
so i'm pretty confident it has something to do with virtualenv

Setting environment variables for integrated terminal

I am seeking help figuring out how to setup environment variables for integrated terminal within Visual Studio Code. Currently I am able to do so with .env file inside my workspace folder, but I'd like to change the filename for this file and create another one or two, let's say dev.env and prod.env. Setting "python.envFile" for my workspace doesn't do the trick and from what I understand changing things in launch.json is for debugging.
The overall goal of all this (and that is important) is to run flask shell (integrated shell for flask web framework, python) with a certain set of env variables and be able to change them by swapping files. I know I could set these by introducing "terminal.integrated.env.osx" to my workspace settings, but I'd rather get these variables from a file.
Thanks a lot for your time and help.
UPD >>
I guess one way to go about it would be to create two tasks as such:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "FLASK SHELL DEV",
"type": "shell",
"command": "source ${workspaceFolder}/dev.env && ${config:python.pythonPath} -m flask shell",
"problemMatcher": []
},
{
"label": "FLASK SHELL PROD",
"type": "shell",
"command": "source ${workspaceFolder}/prod.env && ${config:python.pythonPath} -m flask shell",
"problemMatcher": []
}
]
}
but the question stands. Is there a way to do the same with integrated terminal?
Yes, you can use one of these settings (depending in your platform):
"terminal.integrated.env.linux": {},
"terminal.integrated.env.windows": {},
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}/src:${env:PYTHONPATH}"
},
However, they will only work for the integrated terminal, not for any other processes the vs code python extension might spawn (tests, linting, debugger). For those you need to use the .env file.
Edit:
It is worth noting that if you start VS Code from the terminal it should inherit any env variables from terminal environment (at least for all the spawned subprocesses, probably not for the integrated terminal).
If you don't want to set environment variables using the terminal you can do it using configuration files.

Categories