sys

Python sys.argv to preserve ' ' or “”

只谈情不闲聊 提交于 2019-12-09 03:38:11
问题 terminal: python test.py blah='blah' in test.py print sys.argv ['test.py', 'blah=blah'] <------------ How can blah arg preserve its '' OR Is there a way to know if an arg is wrap with either "" or ''? 回答1: Your shell removes the quotes before invoking Python. This is not something Python can control. Add more quotes: python test.py "blah='blah'" which can also be placed anywhere in the argument: python test.py blah="'blah'" or you could use backslash escapes: python test.py blah=\'blah\' to

Python will not read sys.argv

ⅰ亾dé卋堺 提交于 2019-12-08 19:18:45
问题 import sys print sys.argv[1] hi, this may seem very basic but I can't get Python to read in anything from the command line. thats the code above and what I type is: myfile.py helloworld and what i get back is: IndexError: list index out of range It seemed to work once for me but won't work any more, and I've tried uninstalling and reinstalling Python and it still doesnt work. So my question is, am I doing anything wrong? or have I just broken Python? Thanks for any help Using: Windows 7

Detect from a running python script if the optimize flag is -O or -OO

霸气de小男生 提交于 2019-12-08 15:40:04
问题 Sometime I'd like to spawn a child process with the same optimization flags used to start the parent. I can use something like: optimize = not __debug__ But this way I match both -O and -OO flags. Is there some python internal status that contains that info? 回答1: After some digging in the documentation I've found that the sys.flags struct sequence (http://docs.python.org/dev/library/sys#sys.flags) that has an optimize attribute containing the information I was searching for. python -c "import

Ahk: run a python script with args

依然范特西╮ 提交于 2019-12-05 07:21:37
问题 I have seen this thread (and many others) but I am still not able to retrieve the args in python. 1) With this... Run Cmd \k "Python C:\my.py %myvar%" ... a Cmd is open but nothing happens : my.py isn't started. 2) With this... Run C:\my1.py %myvar% ... the python script is run but when I retrieve the args with sys.argv , I only get the path of the script not myvar . Len(sys.arg) return 1 so myvar isn't passed down to python. 回答1: This thread solved it. commands= (join& python "C:\my.py" "

Importing python libraries from Github

让人想犯罪 __ 提交于 2019-12-05 04:21:50
I have written some libraries in Python for use in my project. I have stored them locally on my system and also remotely on Github. Now every time I write some code I use sys.path.append() in the beginning to help import my libraries from the directory in my system. I was wondering that if there is anyway to import these files directly from my Github repository The link to my repo is this - Quacpy This feels a bit off the wall but might work for you (if any of your libraries depend on each other, you'll have to change those imports to githubimports too!?): import requests def githubimport(user

Is it possible to write to a python frame object as returned by sys._getframe() from python code running within the interpreter?

不羁的心 提交于 2019-12-03 21:39:16
Apropos of This question , there is a bit of scaffolding within the interpreter to inspect frame objects, which can be retrieved by sys._getframe() . The frame objects appear to be read only, but I can't find anything obvious in the docs that explicitly states this. Can someone confirm whether these objects are writeable (in some way) or read only? import sys def foobar(): xx='foo' ff = sys._getframe() ff.f_locals['xx'] = 'bar' print xx if __name__ == '__main__': foobar() This prints out ' foo ' when run but the post below demonstrates the variable being writable when run from the current

Ahk: run a python script with args

核能气质少年 提交于 2019-12-03 21:38:44
I have seen this thread (and many others) but I am still not able to retrieve the args in python. 1) With this... Run Cmd \k "Python C:\my.py %myvar%" ... a Cmd is open but nothing happens : my.py isn't started. 2) With this... Run C:\my1.py %myvar% ... the python script is run but when I retrieve the args with sys.argv , I only get the path of the script not myvar . Len(sys.arg) return 1 so myvar isn't passed down to python. This thread solved it. commands= (join& python "C:\my.py" "%myvar%"`n ) Run, cmd /c %commands% return It's also possible to use Run, cmd /k %commands% or Run,%comspec% /k

Why is sys.getdefaultencoding() different from sys.stdout.encoding and how does this break Unicode strings?

走远了吗. 提交于 2019-12-03 19:31:04
问题 I spent a few angry hours looking for the problem with Unicode strings that was broken down to something that Python (2.7) hides from me and I still don't understand. First, I tried to use u".." strings consistently in my code, but that resulted in the infamous UnicodeEncodeError . I tried using .encode('utf8') , but that didn't help either. Finally, it turned out I shouldn't use either and it all works out automagically. However, I (here I need to give credit to a friend who helped me) did

Get parent of current directory from Python script

做~自己de王妃 提交于 2019-12-03 03:05:33
问题 I want to get the parent of current directory from Python script. For example I launch the script from /home/kristina/desire-directory/scripts the desire path in this case is /home/kristina/desire-directory I know sys.path[0] from sys . But I don't want to parse sys.path[0] resulting string. Is there any another way to get parent of current directory in Python? 回答1: Using os.path To get the parent directory of the directory containing the script (regardless of the current working directory),

python command line arguments in main, skip script name

旧巷老猫 提交于 2019-12-01 03:44:18
This is my script def main(argv): if len(sys.argv)>1: for x in sys.argv: build(x) if __name__ == "__main__": main(sys.argv) so from the command line I write python myscript.py commandlineargument I want it to skip myscript.py and simply run commandlineargument through commandlineargument(n) so I understand that my for loop doesn't account for this, but how do I make it do that? Since sys.argv is a list, you can use slicing sys.argv[1:] : def main(argv): for x in argv[1:]: build(x) if __name__ == "__main__": main(sys.argv) But, if you can only have one script parameter, just get it by index: