GAE Python : dev_appserver.py: error: too few arguments - python

I am trying to run the basic helloworld code described here https://cloud.google.com/appengine/docs/python/. However, whenever I try the dev_appserver.py helloworld/ command, I get a usage error for the dev_appserver.py command.
I have installed Python 2.7 and also have Python 2.7 Anaconda installed on my system. Could the Anaconda Python be the cause of the issue?
The file structure of my code is as follows:
Project
helloworld
app.yaml
helloworld.py
README.md
I have tried executing the dev_appserver.py helloworld/ command from inside the 'Project' folder and the 'helloworld' folder. But I get the same error in both cases.
Any help would be greatly appreciated!
Thanks!

I think I have found the "real" problem behind this error.
Tried putting some print statements in the dev_appserver.py file and found that what ever argument we are giving is not being passed and hence the error. On googling came across this SO post which explains the problem. On doing that change the following command worked flawlessly :D
dev_appserver.py app.yaml
Quoting that SO answer for easier reference:
I think I solved this. For some reason there is a SECOND place in the registry (besides that shown by the file associations stored in HKEY_CLASSES_ROOT\Python.File\shell\open\command):
[HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command]
#="\"C:\\Python25\\python.exe\" \"%1\" %*"
This seems to be the controlling setting on my system. The registry setting above adds the "%*" to pass all arguments to python.exe (it was missing in my registry for some reason).

You just need to execute the dev_appserver.py with path to app.yaml in the command itself, as suggested in comments.

I think OP could've identified a potential issue when describing multiple Python installations.
If I don't specify which Python installation (I thought I only had one...), then, it fails:
C:\Python27>"C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\dev_appserver.py" "C:\users\jessmine\documents\ttbtamer\app.yaml"
usage: dev_appserver.py [-h] [-A APP_ID] [--host HOST] [--port PORT]
...etc...
dev_appserver.py: error: too few arguments
But if I specify which Python to use by invoking Python first, # C:\Python27\python.exe, then it works:
C:\Python27>"C:\Python27\python.exe" "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\dev_appserver.py" "C:\users\jessmine\documents\ttbtamer\app.yaml"
INFO 2016-11-18 10:09:14,299 devappserver2.py:769] Skipping SDK update check.
Anyway since I don't think I have other Python installations on my computer, then I may be misinterpreting the difference here. But for me, the fix is to explicitly invoke python.exe.
(And to be clear, I know about putting the Python.exe location into %PATH%, but I expected that if it wasn't there, that the error would've been something like "'dev_appserver.py' is not recognized as an internal or external command", rather than executing and printing a Python error....)
EDIT
After changing the "associate a file type or protocol with a specific program" (example here) for *.py to explicitly use C:\Python27\python.exe , then I no longer needed to manually invoke C:\Python27\python.exe in my cmd; i.e. my first example worked correctly :
C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin>dev_appserver.py "C:\users\jessmine\documents\ttbtamer\app.yaml"
INFO 2016-11-18 10:33:50,269 devappserver2.py:769] Skipping SDK update check.

I had the same problem. I even installed everything on a fresh machine and I was getting the same error again.
So I "debugged" dev_appserver.py and I discovered that the argument passed to it (i.e. 'app.yaml' or '.' or '\hello_world') was not passed down to the following code file:
...\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\devappserver2.py
In this file the arguments were checked, producing the infamous error "too few arguments".
Out of desperation for the time lost, I made a quick (and for sure ugly) patching.
I commented out at lines 302 and 303 the validation rule:
parser.add_argument(
'config_paths', metavar=arg_name, nargs='+', help=arg_help)
At line 758 I replaced:
options.config_paths, options.app_id)
with a static file name:
{'app.yaml'}, options.app_id)
At least now I am able again to start the server and develop my application. Now that I can work again, I will try to understand better how to correct the problem.
I'm not a Python nor a Google App Engine expert, so I hope someone will propose a better correction and a better explanation of the problem, because I cannot believe that Google can release such bugged code!

I suggest cd into your helloworld folder and and run dev_appserver.py . don't forget the ending dot '.' sign.
Hope it works

In my case I was able to resolve this issue by peforming following two actions:
1). added "python" in the start of the command
2). provided full path to the "dev_appserver.py" file.
So in Google docs you will find to execute the following:-
> dev_appserver.py ./ --php_executable_path=/path/to/php-cgi
(note that I was trying to run the php example and on windows environment here...)
Instead running the following command worked:
> python "c:\<path to the directory containing the dev_appserver.py script>\dev_appserver.py" ./ --php_executable_path=/path/to/php-cgi

Related

CodeDeploy failing with error Errno::ENOEXEC with message Exec format error

I have a CodeDeploy which deploys application on Windows instances. I have a Python script which is running as part of ValidateService hooks. Below is the code I have in that script:
print("hello")
So, I have removed everything and just printing hello as part of this script. When this script is called by CodeDeploy I get below error:
My appspec.yml file:
...
ValidateService:
- location: scripts/verify_deployment.py
timeout: 900
I tried getting some help on Google but got nothing. Can someone please help me here.
Thanks
As Marcin already answered in a comment, I don't think you can simply run python scripts in CodeDeploy. At least not natively.
The error you see means that Windows does not know how to execute the script you have provided. AFAIK Windows can't run python natively (like most linux distros can).
I am not very accustomed to CodeDeploy, but given the example at https://github.com/aws-samples/aws-codedeploy-samples/tree/master/applications/SampleApp_Windows, I think you have to install python first.
After so much of investigations, I found my answer. The issue is little misleading, there is nothing to do with Code format or ENOEXEC. The issue was due to Python path. While executing my script, CodeDeploy was unable to find Python (Though I had already added python.exe in Environment variable path).
Also, I found that CodeDeploy is unable to execute .py file due to Python path issue. So, I created a PowerShell script and invoking Python script from there. Like below:
C:\Users\<username>\AppData\Local\Programs\Python\Python37-32\python.exe C:\Users\<username>\Documents\verify_deployment.py
It executed Python script successfully and gave me below output:
hello

How to make python run shell commands under /usr/bin instead of /bin/sh?

first of all sorry if this question was asked I could not find any answers.
I have a Flask application served over uwsgi and nginx (not really relevant to my question).
The problem I am facing is that if I run:
os.system(f"grep {needle} /some/path/haystack.txt")
my server will output the following encountered error:
/bin/sh: 1: grep: not found.
If I run:
os.system(f"/usr/bin/grep {needle} /some/path/haystack.txt")
everything works as expected.
I know that letting users run commands is insecure: this is part of a hacking CTF I am developing for the company and they MUST be able to run commands, such as ;cat /tmp/flag.txt.
I thought I could have a whitelist of commands such as cat less more etc, and if such a command is detected just replace it with the /usr/bin/ variant, for example:
; cat /tmp/flag.txt
becomes
os.system(f"/usr/bin/grep ; /usr/bin/cat /tmp/flag.txt /some/path/haystack.txt")
However this is not ideal and I would like to tell Python to run commands without having to specify it should use /usr/bin.
Any help is greatly appreciated!
If you do not want to specify in the Python script to look at /usr/bin then an option would be to create a symlink to /usr/bin and then add the symlink to the PATH variable on the server and test.
First things first i would look into these file locations on the server, does /usr/bin exist on the server, or is it the Python script cannot, and then where the files actually are, if they do not exist on /usr/bin. In addition check which user you are running the script as, and check your permissions.

Python 2.7 to 3.4 Subprocess Issue

I'm calling another program with a piece of code that looks like this:
import subprocess
lc="/package/bin/program --do stuff"
command_list = lc.split()
ljs=subprocess.Popen(command_list,stdout=subprocess.PIPE)
ljs.communicate()[0]
The string works fine at the UNIX command line and the code works in Python 2.7. But, in Python 3.4, I get an error like this:
File "/package/bin/program", line 2, in <module>
from package import module
ImportError: No module named package
"/package/bin/program" is calling a dependency from another file in the package here, which I think is the core issue. I have calls to other programs that are working fine in 3.4.
What's changed in 3.4 that might be causing this?
(Sorry in advance for the cryptic code - I'm calling company internal tools that I can't expose here).
The problem is that the working directory of the subproccess instance is default the directory of bash shell. To set a new working directory, set the cwd argument in your Popen to your working directory.
Here's an example:
subprocess.Popen(['random' '--command'], stdout = subprocess.PIPE, cwd='C:/Path/To/Working/Directory/')
Comments above have been helpful in exploring the issue, but at the end of the day this seems to be some permissions conflict - adding a sudo -u <user> before the command fixes the issue. Still not clear why Py3 requires this and Py2 doesn't, but perhaps I need to explore the issue more closely with other internal users.
Thanks!

Python subprocess returned non-zero exit status -6

this is my first post on stackoverflow. I hope my question is not to stupid:)
I'm using django to develop a website, inside the django I need to grab thumbnail of website, so I use https://github.com/coderholic/PyWebShot to achieve it.
I run PyWebShot in django shell with subprocess it work well, but when I put django into apache2 and access it with a url then it fail.
Here is the error message:
CalledProcessError at GIVEN_URL
Command '['echo', '$HOME']' returned non-zero exit status -6
It fail on this command actually:
subprocess.check_output(['echo', '$HOME'])
I've tried write output and error of the process into a file, but the file always empty.
I'm hoping someone can give me a note or direction, Thanks!
ps: If need more information please just let me know.
I check Apache error logs and find a strange error. After that I surf the internet and found it is a old python2.7 error. So I patch it then solved this problem! Thanks everybody.
The "echo" command may not be available to the apacher server (depends on the value of PATH, not necessarily same as when you run it manually). More likely though, the $HOME env var is not defined for the account running the Apache server. There is a standard set of env vars available to CGI scripts (see for instance http://www.perlfect.com/articles/cgi_env.shtml or even the CGI spec itself). Finally, apache's mod_env module allows you to pass some env vars to scripts, you would specify those in your httpd.conf.
This is most likely a bug reported in Python 2.7.2. Upgrading to a newer version should resolve the issue.
The error reported in the apache logs is something along the lines of:
Fatal Python error: Couldn't create autoTLSkey mapping

usr/bin/env: bad interpreter Permission Denied --> how to change the fstab

I'm using cygwin on windows 7 to run a bash script that activates a python script, and I am getting the following error:
myscript.script: /cydrive/c/users/mydrive/folder/myscript.py: usr/bin/env: bad interpreter: Permission Denied.
I'm a total newbie to programming, so I've looked around a bit, and I think this means Python is mounted on a different directory that I don't have access to. However, based on what I found, I have tried to following things:
Change something (from user to exec) in the fstab: however, my fstab file is all commented out and only mentions what the defaults are. I don't know how I can change the defaults. The fstab.d folder is empty.
change the #! usr/bin/env python line in the script to the actual location of Python: did not work, same error
add a PYTHONPATH to the environment variables of windows: same error.
I would really appreciate it if someone could help me out with a suggestion!
In my case the problem was in the missing executable flag on the file.
The solution for me was in the following code
chmod +x ./executed_file
You script should start with:
#! /usr/bin/env whateverelse ...
^ this first one is important
This seems to be a late answer, but may be useful for others. I got the same kinda error, when I was trying to run a shell script which used python. Please check \usr\bin for existence of python. If not found, install that to solve the issue. I come to such a conclusion, as the error shows "bad interpreter".
I would recommend you to 'run cygwin as administrator' Thanks.
EDIT:- try chmod for permissions read for more here
You should write your command as 'python ./example.py ',then fix it in your script.
This was in Git Bash for me. I changed the first line (shebang line) of the .py file being run, leaving the original, adding the one above it, and that worked:
#!python
#!C:\Users\Emiri\Anaconda\envs\_build\python.exe
("Emiri" doesn't exist on my system, was hardcoded in a zipline install)
In my case I used cmder and started bash as Admin instead of WSL
You can disable SELinux by using:
setEnforce 0
More info about how to use SELinux

Categories

Resources