I want to create a runner to activate a virtualenv and run python code in it.
Below configuration doesn't work:
// Create a custom Cloud9 runner - similar to the Sublime build system
// For more information see https://docs.c9.io/custom_runners.html
{
"cmd" : ["source /home/p2/bin/activate && python", "$file", "$args"],
"info" : "Started $project_path$file_name",
"env" : {},
"selector" : "*\.py"
}
I found a solution :)
// Create a custom Cloud9 runner - similar to the Sublime build system
// For more information see https://docs.c9.io/custom_runners.html
{
"cmd" : ["/home/p2/bin/python", "$file", "$args"],
"info" : "Started $project_path$file_name",
"env" : {},
"selector" : "^*\\.py$"
}
You can also pipe the command through bash -c
Related
I have a jenkins pipeline where I have 2 stages:
pipelien {
agent {label 'master'}
stages ('stage 1') {
stage {
steps {
sh "python3 script.py" //this script returns value {"status": "ok"}
}
}
stage ('stage 2') {
// Do something with the response JSON from script.py
}
}
}
The issue is that I cannot do it. What I have tries:
Pass the result to an environment variable, but for some reason Jenkins didn't recognize it. Maybe I did something wrong here?
Playing an parsing stdout of script.py is not an option, because this script prints lot of logs
The only option which is left is to create a file to store that JSON and then to read that file in the next step, but it's ugly.
Any ideas?
In the end we chose to create files and store the messages there.
We pass the file name as an argument to python script and log everything.
We then remove all the workspace after the job succeeds.
I'm writing my first VSCode extension. In short, the extension opens a terminal (PowerShell) and executes a command:
term = vscode.window.activeTerminal;
term.sendText("ufs put C:\\\Users\\<userID>\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\\site-packages\\mymodule.py");
After selecting a Python environment, VSCode should know where Python is located (python.pythonPath). But the path to Python will obviously vary depending on the Python installation, version and so on. So I was hoping that I could do something like:
term.sendText("ufs put python.pythonPath\\Lib\\site-packages\\mymodule.py");
But how can I do this in my extension (TypeScript)? How do I refer to python.pythonPath?
My configuration:
Windows 10
Python 3.8.2
VSCode 1.43.2
Microsofts Python extension 2020.3.69010
Node.js 12.16.1
UPDATE:
Nathan, thank you for your comment. I used a child process as suggested. It executes a command to look for pip. Relying on the location of pip is not bullet proof, but it works for now:
var pwd = 'python.exe -m pip --version';
const cp = require('child_process');
cp.exec(pwd, (err, stdout, stderr) => {
console.log('Stdout: ' + stdout);
console.log('Stderr: ' + stderr);
if (err) {
console.log('error: ' + err);
}
});
Not sure where to go from here to process stdout, but I tried child_process.spawn using this accepted answer:
function getPath(cmd, callback) {
var spawn = require('child_process').spawn;
var command = spawn(cmd);
var result = '';
command.stdout.on('data', function (data) {
result += data.toString();
});
command.on('close', function (code) {
return callback(result);
});
}
let runCommand = vscode.commands.registerCommand('extension.getPath', function () {
var resultString = getPath("python.exe -m pip --version", function (result) { console.log(result) });
console.log(resultString);
});
Hopefully, this would give me stdout as a string. But the only thing I got was undefined. I'm way beyond my comfort zone now. Please advise me how to proceed.
It may be a simple question but i could not find a solution that works for me properly. So i want to start a python skript via a controller in laravel. So far i tried exec(), shell_exec() and this as well:
$cmd = "py pathtoSkript/example.py";
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
My python Version is: Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (I
When the process is started it should only run the skript like you type:
py yourPath/example.py in the windows shell. shell_exec() in my case feezes the current session and exec dont work. The Script works fine when i execute it via pycharm.
You may use Symfony Process https://symfony.com/doc/current/components/process.html
Installation
composer require symfony/process
Usage
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
$process = new Process(['py pathtoSkript/example.py']);
// or $process = new Process(['python', 'pathtoSkript/example.py']);
try {
$process->mustRun();
echo $process->getOutput();
} catch (ProcessFailedException $exception) {
echo $exception->getMessage();
}
Try this
$command = escapeshellcmd('py pathtoSkript/example.py');
$output = shell_exec($command);
$output;
ref link https://www.php.net/manual/en/function.escapeshellcmd.php
I'd like to run the following shell command 10 times
./file.py 1111x
with the 'x' ranging from 0 to 9
i.e. a different port for each .file.py file. I need each instance running in its own shell. I already tried creating a batch file and a python script that calls the windows shell but both without success.
What about this...
import os
import subprocess
for x in range(0,10):
command = './file.py 1111' + str(x)
os.system(command)
#or
subprocess.call('cmd ' + command, shell=True)
What you're looking for is a powershell job. You may need to tweak this a bit to accommodate your specific requirements, but this should do what you need.
[ScriptBlock]$PyBlock = {
param (
[int]$x,
[string]$pyfile
)
try {
[int]$Port = (11110 + $x)
python $pyfile $Port
}
catch {
Write-Error $_
}
}
try {
0..9 | ForEach-Object {
Start-Job -Name "PyJob $_" -ScriptBlock $PyBlock -ArgumentList #($_, 'path/to/file.py')
}
Get-Job | Wait-Job -Timeout <int>
#If you do not specify a timeout then it will wait indefinitely.
#If you use -Timeout then make sure it's long enough to accommodate the runtime of your script.
Get-Job | Receive-Job
}
catch {
throw $_
}
I have a python application that has a shell that needs to do some setup based on whether the shell it's running in is the Windows Command Prompt (cmd) or Powershell.
I haven't been able to figure out how to detect if the application is running in powershell or cmd.
From my searches on stackoverflow and Google, it seems the only way to do this is to use psutil to find the name of the parent process.
Is there a nicer way?
Edit: I've decided to use psutil to find the name of the parent process. Thanks to everyone who helped in the comments.
#Matt A. is right. Use psutil and os package to get the parent process id and the name.
parent_pid = os.getppid()
print(psutil.Process(parent_pid).name())
The following snippet finds md5sum on args.file in bash/powershell, I usually use the first command to check what we are running in, so I can use it later on, using shell=True in subprocess is not very portable.
import os, subprocess
running_shell = None
mycheck='/usr/bin/md5sum' # full path is needed
if not os.path.isfile(mycheck):
try:
file_md5sum = subprocess.check_output("powershell.exe Get-FileHash -Algorithm MD5 {} | Select -expand Hash".format(args.file).split())
except FileNotFoundError as e:
log.fatal("unable to generate md5sum")
sys.exit(-1)
file_md5sum = file_md5sum.lower()
running_shell = 'powershell'
else:
file_md5sum = subprocess.check_output([mycheck, args.file])
running_shell = 'bash'
Here is a sample powershell stub that can do the trick:
$SCR="block_ips.py"
$proc = $null
$procs = Get-WmiObject Win32_Process -Filter "name = 'python3.exe' or name = 'python.exe'" | Select-Object Description,ProcessId,CommandLine,CreationDate
$procs | ForEach-Object {
if ( $_.CommandLine.IndexOf($SCR) -ne -1 ) {
if ( $null -eq $proc ) {
$proc = $_
}
}
}
if ( $null -ne $proc ) {
Write-Host "Process already running: $proc"
} else {
Write-Host "$SCR is not running"
}
Based on this post, you should be able to run this CMD/PS command through the subprocess module in your Python script:
subprocess.call("(dir 2>&1 *`|echo CMD);&<# rem #>echo PowerShell", shell=True)
This will output CMD if you're in CMD, and PowerShell if you're in PS.