How would I run command lines in Python? - python

I am playing around with this Instagram ID tracker because my friends are constantly changing usernames so I can never keep up. The code is from here. The only way I am aware of, is that you have to go to any sort of terminal and type a command in this format
$ usage: t.py [-h] -u USERNAME [-p] [-s] [-t]
I've already tried to do some on my own and I recently saw other people running command lines in the Python text editor but it doesn't seem to work for me. This is what I did
import os
import main
import api
os.system('python3 main.py -u <USERNAME>')
There are 2 files, main.py and api.py. I imported both but when I run it, it still says
usage: t.py [-h] -u USERNAME [-p] [-s] [-t]
t.py: error: the following arguments are required: -u/--username
Is there an easier way to go about this?

Related

Run Python scripts without explicitly invoking `python`

I am trying to run my python script without writing python func.py.
I've added to my script file #!/usr/bin/python2.7
did chmod +x func.py
when I try to run: ./func.py -p show -c all
the script works fine but if I try to take off the "flags" (-p , -c) or the "./" or ".py" it won't work.
taking flags off returns:
[root#pg66 tmp]# ./func.py show all
usage: func.py [-h] [-p PROCESS] [-c CLUSTER] [-t TYPE]
func.py: error: unrecognized arguments: show all
taking "./" & ".py" off returns:
[root#pg66 tmp]# func.py -p show -c all
-bash: func.py: command not found
edit: i have 3 flags -p -c -t , dont know where the -h came from. -t can be null so when i write -p show -c all it works.
you can simply use the chmod +* x in the location with the python file. This makes it easier for linux/unix After that you can run it by ./main.py Assuming the file is named as main.py. You can also remove the .py extension then give the permission by chmod +* x and after that then just execute by ./main. Another way to do this is simply going to the execution script of your terminal then add an alias for directly running the script just by typing the alias name

git subcommand with --help arg, not working

Having this example code:
#!/usr/bin/env python3
import argparse
def main():
parser = argparse.ArgumentParser(description="Some Description")
parser.add_argument('some-arg')
args = parser.parse_args()
print(args)
if __name__ == '__main__':
main()
I add this code to file called git-mycommand, made it executable and copied it to /usr/bin.
Now trying to run command with --help, gives me this unintended output:
user#user:~$ git mycommand --help
No manual entry for git-mycommand
See 'man 7 undocumented' for help when manual pages are not available.
If I run command normally without --help, It works properly, like:
oerp#oerp:~$ git mycommand some_val
Namespace(**{'some-arg': 'some_val'})
Or if I dont use it as git subcommand and run it directly, like:
oerp#oerp:~$ git-mycommand --help
usage: git-mycommand [-h] some-arg
Some Description
positional arguments:
some-arg
optional arguments:
-h, --help show this help message and exit
Does anyone know why custom git subcommand does not work properly with --help argument? Or maybe there is something else, I need to do, so it would show intended output?
The git command is receiving the --help option, not your subcommand.
Note that git --help ... is identical to git help ... because the former is internally converted into the latter.
https://git-scm.com/docs/git-help
git help invokes git-help which opens the man page for the given command.

How to input password to a shell command 'git push' from a Jupyter Notebook?

I have the following code in a Jupyter Notebook's cell:
!git push origin master
which will ask my password for Github but the cell keeps on running as I can't find the way to input my password. For some reason, I want to push the code this way.
I tried to follow the similar questions but nothing seems to be working in my case. Here's what I tried and it didn't work:
import getpass
import os
password = getpass.getpass()
command = "git -S push origin master " #can be any command but don't forget -S as it enables input from stdin
os.system('echo %s | %s' % (password, command))
Here's the log for above:
unknown option: -S
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
The -S is an option of sudo, not git... as you can see git is not even asking for password it just tells you that -S is invalid.
I believe git uses a secure way to get the password, reading from the tty and not stdin. It's probably quite hard to get a hand on the correct tty to input that password to make this work. Moreover this means that you have to write the password in plaintext in your notebook.
The correct way to handle this is to:
Generate an ssh key using ssh-keygen without a passphrase
Configure your server to use that key
Configure git to use the SSH protocol to do the push
This completely avoids a request for passwords.

Three.js - Exporting obj files with python

I thought I'd post this here because hours of googling ended up fruitless. I have an obj file and I'm trying to use the python script included with three.js to export it for use in three.js however I have never used python before and don't know what I'm doing.
These have been my steps so far:
I've opened up the Python GUI, opened up convert_obj_three.py, clicked run -> run module, and then I get this:
======= RESTART: C:\three.js\utils\converters\obj\convert_obj_three.py =======
Usage: convert_obj_three.py -i filename.obj -o filename.js [-m morphfiles*.obj] [-c morphcolors*.obj] [-a center|top|bottom] [-s flat|smooth] [-t binary|ascii] [-d invert|normal]
When I try typing in something like convert_obj_three.py -i test.obj -o test.js I get an SyntaxError: invalid syntax error.
test.obj is in the same folder as convert_obj_three.py however that does not seem to be the issue as no matter what I type I seem to get a syntax error...
Run the command:
convert_obj_three.py -i test.obj -o test.js
at a command prompt. On Windows, Python will have to be in your path for this to work, that's an option in the installer (at least in 2.7) right at the bottom of the list of options, IME sometimes not visible - very easy to miss - so scroll down to find it.

ipython not showing argparse help message

In my python script myscript.py I use argparse to pass command-line arguments. When I want to display the help information about the input arguments, I just do:
$ python myscript.py --help
If instead I want to use ipython to run my script, the help message won't be displayed. Ipython will display its own help information:
$ ipython -- myscript.py -h
=========
IPython
=========
Tools for Interactive Computing in Python
=========================================
A Python shell with automatic history (input and output), dynamic object
introspection, easier configuration, command completion, access to the
system shell and more. IPython can also be embedded in running programs.
Usage
ipython [subcommand] [options] [files]
It's not so annoying, but is there a way around it?
You need to run your .py script inside the ipython. Something like that:
%run script.py -h
This is an IPython bug, corrected in https://github.com/ipython/ipython/pull/2663.
My 0.13 has this error; it is corrected in 0.13.2. The fix is in IPthyon/config/application.py Application.parse_command_line. This function looks for help and version flags (-h,-V) in sys.argv before passing things on to parse_known_args (hence the custom help formatting). In the corrected release, it checks sys.argv only up to the first --. Before it looked in the whole array.
earlier:
A fix for earlier releases is to define an alternate help flag in the script:
simple.py script:
import argparse, sys
print(sys.argv)
p = argparse.ArgumentParser(add_help=False) # turn off the regular -h
p.add_argument('-t')
p.add_argument('-a','--ayuda',action=argparse._HelpAction,help='alternate help')
print(p.parse_args())
Invoke with:
$ ./ipython3 -- simple.py -a
['/home/paul/mypy/argdev/simple.py', '-a']
usage: simple.py [-t T] [-a]
optional arguments:
-t T
-a, --ayuda alternate help
$ ./ipython3 -- simple.py -t test
['/home/paul/mypy/argdev/simple.py', '-t', 'test']
Namespace(t='test')

Categories

Resources