can't cancel python build in sublime3 using virtualenv - python

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

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 use Mamba Miniforge CLI in VSCode with Windows

I am trying to debug some Python code that must run in a Mamba environment. In order to run the code (but not debug), I can open the Miniforge Prompt command line application, activate my enviroment (mamba activate my_env), and then run my python file (python my_file.py). Running this code is producing an error that I would like to trace back using the Visual Studio Code debugging interface. I am having a problem trying to get this to run in Visual Studio Code, because it cannot seem to run the Miniforge Prompt command line. I am also running on Windows 10.
The default terminal options (for Windows) in VSCode are Powershell and CMD (and Git Bash), which both work fine, however, when I added another terminal method for Miniforge (via the settings.json), it doesn't seem to be working properly.
Here is my settings.json file:
{
...,
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
},
"MambaPython": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": ["\"/K\"", "C:\\ProgramData\\mambaforge\\Scripts\\activate.bat", "C:\\ProgramData\\mambaforge"],
"icon": "terminal-cmd"
}
},
"terminal.integrated.defaultProfile.windows": "MambaPython",
}
I also modified the launch.json to activate the mamba environment once running in the miniforge CLI. Here is my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
},
{
"name": "Python: ProjectEnv",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"preLaunchTask": "ProjectEnv_activate",
"args": ["--kwarg_one=Something", "--kwarg_two"],
}
]
}
also, here is the tasks.json file that actually activates the environment:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [{
"label": "ProjectEnv_activate",
"command": "mamba activate ProjectEnv",
"type": "shell"
}]
}
When I execute any code (in run or in debug) in VSCode, it appears to just run with the standard CMD terminal, not in a Mamba environment as specified. If anyone knows how to get this to work, or any way to activate a Mamba environment when debugging python in VSCode, any help would be much appreciated!
I came across this question trying to configure my VS Code terminal to play nice with miniforge. I got it working correctly after just fiddling with the args syntax a bit. I'm using conda, not mamba, but I don't think that should matter for activating the environment.
"Command Prompt": {
"path": [
"${env:windir}\\System32\\cmd.exe"
],
"args": ["/K", "C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\Scripts\\activate.bat","C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\envs\\base" ],
"icon": "terminal-cmd"
},
There is a vscode extension
https://github.com/mamba-org/vscode-micromamba
With this extension one can create mamba environments based on environment.yml file.
Next to mamba environment a .env file is generated. If you use python vscode extension you could configure it to use the .env file. When using terminal window the environment is activated automatically.
I had no luck with the micromamba extension.
I'm using micromamba, but the workaround should be the same.
Try set this alias in your powershell $profile:
Set-Alias conda mamba
Now the env is also activated, when the terminal starts in vs code.

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.

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

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.

Visual Studio code and virtualenv

I am trying to use Visual Studio Code with virtual environment. In the Launch JSON I specify the nosetests launch like this:
{
"name": "nosetests",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${workspaceRoot}/env/dev/bin/nosetests",
"args": [
"--nocapture",
"tests"
],
"externalConsole": false,
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit"
]
},
However when I launch the environment variables do not get picked up. I've tried setting up the python path in workspace settings:
"python.pythonPath": "${workspaceRoot}/env/dev/bin/python"
but it doesn't seem to set the right environment. There needs to be something that' the equivalent of source activate. Has anyone figured it out?
#mikebz you need to configure the path to the python executable as follows:
"pythonPath":"${workspaceRoot}/env/dev/bin/python"
The path may not be 100% accurate (please double check it), but that's how you need to configure it in launch.json.
With the next version of VS Code you will no longer have to do this, i.e. you won't have to configure this same setting in two files.
More details on configuring the path for debugging can be found here:
https://github.com/DonJayamanne/pythonVSCode/wiki/Python-Path-and-Version#python-version-used-for-debugging
As 2018.03 there's an article how to add virtualenv to your python path list per particular workspace: https://code.visualstudio.com/docs/python/environments
All you have to do is File->Save workspace as.. and then add to the settings of workspace virtualenv:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.pythonPath": "${workspaceFolder}/.venv/bin/python"
}
}
You need to set up the path for your virtual environments in your workspace settings file: https://code.visualstudio.com/docs/python/environments
Save your workspace then open <workspace_name>.code-workspace file and add your virtual environment folder to the settings:
{
"folders": [
{
"path": "<path-to-your-workspace>"
}
],
"settings": {
"python.venvPath": "<path-to-your-virtual-env-folder>"
}
}
After that open command palette (CMD/CTRL + SHIFT + P) and type select interpreter. Then choose the one from the desired virtual environment.

Categories