Unable to create process using '/usr/bin/env - python

I am trying to use Pidcat for logging. I downloaded the Pidcat.py and pasted it in the following directory:
C:\Python\Scripts
Added this path in the Environment Variables as well. But when I try to log in using the following command:
pidcat -s deviceId
I get the following error:
Unable to create process using '/usr/bin/env -S python -u "C:\Python\Scripts\pidcat.py" -s deviceId'

I used CMD to run a python file getofflinelog.py.
The command is:
python getofflinelog.py -m txt -d camlog -o offline.log
I got a similar error Unable to create process using '/usr/bin/env...., and I solved it by changing the command to:
C:\Users\liutongjun\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe getofflinelog.py -m txt -d camlog -o offline.log
I also found that if I added this path to the environment variables, the first short command would work.
I'm not very clear why it works, I do not install Python in this path and there are only a series of exe programs in this folder. Just for reference.

Related

Errno 2] No such file or directory: '$(Agent.ToolsDirectory)/azcopy/azcopy'

I am currently running a python script in the Azure pipeline.
The python script runs subprocess.run([cmd], capture_output=True), and the cmd is the azcopy command in the format of
$(Agent.ToolsDirectory)/azcopy/azcopy copy source target{token}
currently i have the azcopy installed
- task: Bash#3
displayName: Install azcopy
inputs:
targetType: 'inline'
script: |
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
mkdir $(Agent.ToolsDirectory)/azcopy
wget -O $(Agent.ToolsDirectory)/azcopy/azcopy_v10.tar.gz https://aka.ms/downloadazcopy-v10-linux
tar -xf $(Agent.ToolsDirectory)/azcopy/azcopy_v10.tar.gz -C $(Agent.ToolsDirectory)/azcopy --strip-components=1
Then I have another bash script task that runs the python file, but i get the error message of [Errno 2] No such file or directory: '$(Agent.ToolsDirectory)/azcopy/azcopy'
$(Agent.ToolsDirectory)/azcopy/azcopy is recognized when i run it in the inline script, but it doesn't when I run the command through the python script.
According to my experience, the possible reason is that the python script can't know the real path when you use the $(Agent.ToolsDirectory)/azcopy/azcopy. You can try to give the absolute path without any variables or set the variables inside the python script. Of course, you need to ensure that all the tasks run in the same agent.

How to run bash commands from Python preferably with the os library?

I am trying to run the following python script named test.py. It contains multiple bash commands which I would like to execute in a Linux terminal (unix). This is the content of the file:
import os
os.system('echo install virtualenv')
os.system('sudo pip install virtualenv')
os.system('echo create virtual environment')
os.system('virtualenv my_virtualenvironment')
os.system('echo activate virtual environment')
os.system('source my_virtualenvironment/bin/activate')
I am running the Python script using the following in the terminal:
python3 test.py
The problem that I have is that the commands do not run the same way as they would on a Linux terminal. The output is the following error when trying to execute the last line of the Python script:
sh: 1: source: not found
The last command source my_virtualenvironment/bin/activate normally runs fine if I execute it directly in the terminal (without my Python script). Now, what does sh: 1: mean and why does it not work with my code? I would expect to get something starting with bash: .
Also I have found this solution, but I would like not to use lists for executing commands and maybe even to stick with the os library (if there is a simpler solution without os, I am also open for that):
https://stackoverflow.com/a/62355400/11535508
source is a bash built-in command, not an executable.
Use the full path to the python interpreter in your commands instead of venv activation, e.g. os.system('<venv>/bin/python ...').
The second option is to write your commands into a separate bash script and call it from python:
os.system('bash script.sh')

sbin/start-stop-daemon not able to start python - ubuntu docker container

I have a simple python script which I want to start a daemon-service in background in docker container
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec 'python /opt/app/uc/monitor/bin/my-application.py'
when I execute this command in a shell I get
/sbin/start-stop-daemon: unable to stat //python /opt/app/uc/monitor/bin/my-application.py (No such file or directory)
However when execute just the below command in shell it works
python /opt/app/uc/monitor/bin/my-application.py
I'm sure the python is installed and all the links have been setup.
Thanks for the help
That error message implies that start-stop-daemon is looking for a file to open (the stat operation is a check before it opens the file) and treating your 'python ... ' argument as if it was a file.
See this example which confirms this. You may need to read the man page for start-stop-daemon, for your Ubuntu version, to check what a valid command would be for your setup.
Simplest solution is probably to create a shell script (say /opt/app/uc/monitor/bin/run-my-application.sh), and put this into it:
#!/bin/bash
python /opt/app/uc/monitor/bin/my-application.py
Be sure to do chmod +x on this file. If python is not found, use which python to find the path to python and use that in the script.
Now try:
/sbin/start-stop-daemon --start --user root --make-pidfile --pidfile /var/lock/subsys/my-application.pid --exec '/opt/app/uc/monitor/bin/run-my-application.sh'

Python fabric cant be executed from python using subproccess

In python I am trying to execute a fabfile and ,I get the below error from the subproccess output. I installed fabric using easy install. If I run the code from the command line it works. From python no go. I assume there is an issue with how I am using the Popen command?
/bin/sh: 1: fab: not found
Below is how I start fabric from python:
cmd = """fab -H 111.111.111.111 aws_bootstrap initial_chef_run:aws_server.json,aws-test,development -w """
os.chdir(fab_path) #change to the dir where the fabfile is located
p = subprocess.Popen(cmd, shell=True,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
PS I added the below to the Popen but get the below error:
executable="/bin/bash"
/bin/bash: fab: command not found
From the command line I get the below which means the terminal can find fab.
fab
Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for help.
Aborting.
Try to use the whole path for fab. To get the path to fab on your system, you can use which fab.
However, I can't think of any reason why calling fab from python might be better than using the execute function of fab:
from fabric.tasks import execute
import my_fabfile
r = execute(my_fabfile.aws_bootstrap, hosts=["root#%s" % '111.111.111.111])
Return value r will contain a dict with the hosts as key(s).

how can i use python to deploy proxies from the command line

Deployment by using Python throws error:
I used Python code ( its your deploy.py) to deploy our proxy (our company proxy) into apigee platform. i read http://apigee.com/docs/api-services/content/deploying-proxies-command-line
but it throws error when i run "python api-platform-samples-master/tools/deploy.py -n apikey -u "yusuf.karatoprak#mobgen.com:Welcome#2014" -o yusufkaratoprak123 -e test -p / -d sample-proxies"
i would like to solve this situation. i added to python code it is not working. it throws me Error: name 'ZipFile' is not defined
The -d flag value needs to point to the directory that contains the /apiproxy directory for the sample you want to deploy. (In your command above, it appears that you are pointing at /sample-proxies, rather than, for example, /sample-proxies/apikey
Try using the deploy scripts. There is one in each sample proxy directory. There's a also a script, /setup/deploy_all.sh if you want to deploy all sample proxies.
Make sure you update /setup/setenv.sh before running the deploy scripts.
The error is in how you are calling it from the command line. You have a space in one of the parameters you pass in, which needs to be put inside of quotes. Turn -u yusuf karatoprak:123 into -u "yusuf karatoprak:123"
Fixed command line call:
python api-platform-samples-master/tools/deploy.py -n weatherapi -u "yusuf karatoprak:123" -o yk123 -e test -p / -d simpleProxy

Categories

Resources