extract python venv utils
This commit is contained in:
parent
480b9c5db1
commit
02aa672af8
@ -90,68 +90,13 @@ return {
|
|||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
|
||||||
local function getVenvSuffix()
|
local pythonVenv = require('utils.python_venv')
|
||||||
if vim.loop.os_uname().sysname == 'Linux' then
|
|
||||||
return 'bin/python'
|
|
||||||
elseif vim.loop.os_uname().sysname == 'Windows_NT' then
|
|
||||||
return 'Scripts/python.exe'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getVenvFromJson(jsonfile)
|
|
||||||
if not vim.fn.filereadable(jsonfile) then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
local f = io.open(jsonfile, 'r')
|
|
||||||
if not f then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
local data = f:read('*a')
|
|
||||||
f:close()
|
|
||||||
if data then
|
|
||||||
local jdata = vim.json.decode(data)
|
|
||||||
if jdata['venvPath'] ~= nil and jdata['venv'] ~= nil then
|
|
||||||
return jdata['venvPath'] .. '/' .. jdata['venv']
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local function getPythonEnv()
|
|
||||||
local venv = os.getenv('VIRTUAL_ENV')
|
|
||||||
if venv ~= nil then
|
|
||||||
return string.format('%s/%s', venv, getVenvSuffix())
|
|
||||||
end
|
|
||||||
local conda = os.getenv('CONDA_PREFIX')
|
|
||||||
if conda ~= nil then
|
|
||||||
return string.format('%s/%s', conda, 'python.exe')
|
|
||||||
end
|
|
||||||
|
|
||||||
local cwd = vim.fn.getcwd()
|
|
||||||
|
|
||||||
local jsonVenv = getVenvFromJson(cwd .. '/pyrightconfig.json')
|
|
||||||
if jsonVenv ~= nil then
|
|
||||||
return jsonVenv .. '/' .. getVenvSuffix()
|
|
||||||
end
|
|
||||||
|
|
||||||
if vim.fn.executable(cwd .. '/venv/' .. getVenvSuffix()) == 1 then
|
|
||||||
return cwd .. '/venv/' .. getVenvSuffix()
|
|
||||||
elseif vim.fn.executable(cwd .. '/.venv/' .. getVenvSuffix()) == 1 then
|
|
||||||
return cwd .. '/.venv/' .. getVenvSuffix()
|
|
||||||
else
|
|
||||||
if vim.loop.os_uname().sysname == 'Linux' then
|
|
||||||
return '/usr/bin/python'
|
|
||||||
elseif vim.loop.os_uname().sysname == 'Windows_NT' then
|
|
||||||
return os.getenv('SCOOP') .. '/apps/python/current/python.exe'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function loadConfigs()
|
function loadConfigs()
|
||||||
require('dap.ext.vscode').load_launchjs()
|
require('dap.ext.vscode').load_launchjs()
|
||||||
-- Make sure we use the correct python env even for the configs from launch.json
|
-- Make sure we use the correct python env even for the configs from launch.json
|
||||||
for _, config in pairs(dap.configurations.python) do
|
for _, config in pairs(dap.configurations.python) do
|
||||||
config.pythonPath = getPythonEnv()
|
config.pythonPath = pythonVenv.getPythonEnv()
|
||||||
config.cwd = vim.fn.getcwd()
|
config.cwd = vim.fn.getcwd()
|
||||||
end
|
end
|
||||||
require 'telescope'.extensions.dap.configurations {}
|
require 'telescope'.extensions.dap.configurations {}
|
||||||
@ -161,12 +106,12 @@ return {
|
|||||||
|
|
||||||
-- PYTHON
|
-- PYTHON
|
||||||
dap.adapters.python = {
|
dap.adapters.python = {
|
||||||
type = 'executable';
|
type = 'executable',
|
||||||
command = masonpath .. '/packages/debugpy/venv/' .. getVenvSuffix();
|
command = masonpath .. '/packages/debugpy/venv/' .. pythonVenv.getVenvSuffix(),
|
||||||
args = { '-m', 'debugpy.adapter' };
|
args = { '-m', 'debugpy.adapter' },
|
||||||
options = {
|
options = {
|
||||||
detached = true;
|
detached = true,
|
||||||
};
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
local dapui = require('dapui')
|
local dapui = require('dapui')
|
||||||
|
59
lua/utils/python_venv.lua
Normal file
59
lua/utils/python_venv.lua
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
local M = {}
|
||||||
|
function M.getVenvSuffix()
|
||||||
|
if vim.loop.os_uname().sysname == 'Linux' then
|
||||||
|
return 'bin/python'
|
||||||
|
elseif vim.loop.os_uname().sysname == 'Windows_NT' then
|
||||||
|
return 'Scripts/python.exe'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.getVenvFromJson(jsonfile)
|
||||||
|
if not vim.fn.filereadable(jsonfile) then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local f = io.open(jsonfile, 'r')
|
||||||
|
if not f then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
local data = f:read('*a')
|
||||||
|
f:close()
|
||||||
|
if data then
|
||||||
|
local jdata = vim.json.decode(data)
|
||||||
|
if jdata['venvPath'] ~= nil and jdata['venv'] ~= nil then
|
||||||
|
return jdata['venvPath'] .. '/' .. jdata['venv']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.getPythonEnv()
|
||||||
|
local venv = os.getenv('VIRTUAL_ENV')
|
||||||
|
if venv ~= nil then
|
||||||
|
return string.format('%s/%s', venv, M.getVenvSuffix())
|
||||||
|
end
|
||||||
|
local conda = os.getenv('CONDA_PREFIX')
|
||||||
|
if conda ~= nil then
|
||||||
|
return string.format('%s/%s', conda, 'python.exe')
|
||||||
|
end
|
||||||
|
|
||||||
|
local cwd = vim.fn.getcwd()
|
||||||
|
|
||||||
|
local jsonVenv = M.getVenvFromJson(cwd .. '/pyrightconfig.json')
|
||||||
|
if jsonVenv ~= nil then
|
||||||
|
return jsonVenv .. '/' .. M.getVenvSuffix()
|
||||||
|
end
|
||||||
|
|
||||||
|
if vim.fn.executable(cwd .. '/venv/' .. M.getVenvSuffix()) == 1 then
|
||||||
|
return cwd .. '/venv/' .. M.getVenvSuffix()
|
||||||
|
elseif vim.fn.executable(cwd .. '/.venv/' .. M.getVenvSuffix()) == 1 then
|
||||||
|
return cwd .. '/.venv/' .. M.getVenvSuffix()
|
||||||
|
else
|
||||||
|
if vim.loop.os_uname().sysname == 'Linux' then
|
||||||
|
return '/usr/bin/python'
|
||||||
|
elseif vim.loop.os_uname().sysname == 'Windows_NT' then
|
||||||
|
return os.getenv('SCOOP') .. '/apps/python/current/python.exe'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
Loading…
x
Reference in New Issue
Block a user