Let me preface this post that I am incredibly new to python. I took a course in codeacademy but haven't actually used python on my own computer until now. Let me also explain to everyone that my coding experience is limited to VBA and excel. I have very little knowledge of how to use the command prompt, how libraries work, etc, etc.
I'm having some issues. I've changed my directory to recognize python when I type python in my command prompt. Then I can do simple things like
print "hello"
however if i write something like this
def firstfunction(t):
print t+5
return
firstfunction(5)
I would expect this to print the number 10, however I get the following error:
File"stdin", line 4
first function(5)
^
syntaxerror: invalid syntax
However if I use this in IDLE and run it it works fine.
Next I've saved some python programs I wrote. One I saved as Hello.py
i want to run this code from my python exe or from the command prompt but can't figure out how.
Please any answers helpful, if you use computer lingo, try and keep it as simple as possible. I'm super new!
Thanks
If the location of python is not included in your path, add it. But in any case, you should be able to run this successfully from the directory containing your python.exe.
e.g, if python lives in C:\Python27:
cd C:\Python27
python mypythonscript.py
Related
I'm attempting to get Irfanview to extract some multipage images for me. As you can't batch do that operation within Irfanview I'm trying to use Pythons subprocess.call() to do the commandline work for me.
I've got the command in powershell working fine with no issues. But when I try the exact same command via subprocess.call() Irfanview tells me I've got an "unsupported save type !"
PS command:
i_view32.exe .\multiPage.tif /extract=(".\,tif")
Python code:
cmd = r'i_view32.exe .\multiPage.tif /extract=(".\,tif")'
subprocess.call(cmd, shell=True)
I've tried with no shell too. Also I tried giving it fully qualified names. No difference.
Any ideas?
Cheers,
James
Ahh. I've found the problem. Turns out in PS I need to give the extract folder path as a string whereas via Python I don't.
So in Py instead of
i_view32.exe .\multiPage.tif /extract=(".\,tif")
It needed to be
i_view32.exe .\multiPage.tif /extract=(.\,tif)
Thats actually the same way that the Irfanview manual asks for it. No idea why PS demanded the string bit.
Whenever I try to build code in Sublime Text 2 (Python 2.7) in which there is either input() or raw_input(), I get this error:
EOFError: EOF when reading a line
[Finished in 0.1s with exit code 1]
I've found some tutorials that said it's a problem about ST not locating Python, so I edited the file that should have fixed it, to no avail. I still get the same error.
What is the problem here, and how can this be fixed?
It's not an issue with ST not locating Python, rather, it's the fact that ST does not support interactivity when building programs - input()/raw_input() in Python, gets in Ruby, scanf and associates in C/C++, Scanner in Java, etc. For compiled languages, the workaround is to run your program through the shell, which handles input. This can also be done with interpreted languages like Python, Ruby, Perl, etc., but for many people their preferred solution is to use SublimeREPL, available via Package Control. This essentially runs an interactive Python shell within Sublime, either the standard Python shell or IPython, which I prefer for a number of reasons.
SublimeREPL comes with a series of commands for taking lines, blocks, selections, or entire code files and passing them through the REPL (it has to be started manually first), which then takes care of handling input and output. Alternatively, if you're just testing smaller snippets of code, you can just enter and run it within SublimeREPL itself - I find myself doing this extremely often, it's incredibly helpful.
I'm using Enthought Canopy on Mac OSX Lion. I'm using this because of the ease at which modules and libraries can be downloaded and installed (had a lot of trouble downloading pandas and numpy through terminal due to a number of issues).
So, now I'm doing my coding for a project in Canopy, which is OK (still prefer Wing, though). The problem I have encountered is that I need to ask the user for input. When I do, for instance:
x = input('Enter your input here: ')
I get an EOF error as follows: EOFError: EOF when reading a line
I was looking around and believe that this may be something which Canopy does not support. Was wondering if this is, indeed, the case and if there is a solution/workaround to this problem?
I assume that you are entering this code in the IPython shell that is embedded in the Canopy editor. This is an upstream bug/deficiency in IPython's Qt console. Because the remote shell is not actually hooked up to a terminal, functions like input() and raw_input() need to be replaced to get the input from the GUI console widget instead. IPython (and thus Canopy) does replace raw_input() but does not replace input(). This code will work using Canopy's Python interpreter if you were to put it into a script and execute it from the command line, and it would also work in a terminal instance of IPython. It was most likely overlooked because it is usually considered a bad idea to use input().
Please use raw_input() instead and parse the string that you get. You can use eval() if you must, but I do recommend using a more specific parsing/conversion function.
I am new to python and wxpython, I am making a automated tool using python and for user interface wxpython and i use shell script.shell script can be called from the python. but now I am facing problem with the spinctrl value. whenever that spinctrl value changes it have to send that value into one txt.exe file which is written in BASH .{ if we run txt.exe file in command line it will ask for number then it will accept that value whenever we press enter}. but i am not able to understand that how to send value from spinctrl to txt.exe whenever i press "ok" button in GUI. please share your thoughts and knowledge.
Thank you
To call an executable in Python, you need to look at Python's subprocess module: http://docs.python.org/2/library/subprocess.html
If your exe doesn't accept arguments, then it won't work. If you created the exe yourself using something like PyInstaller or py2Exe, then you need to make it so your app can accept arguments. The simplest way is using sys.argv, but there are also the optparse and argparse libraries in Python as well.
I do not get a good feeling about your concept. It is hard to say, but I suspect you would benefit from talking with someone experienced about what you are trying to achieve and how it could best be done.
However, to get you started down your current path, I think you should take a look at wxExecute ( http://docs.wxwidgets.org/2.8/wx_processfunctions.html#wxexecute ). This will allow you to run your txt.exe utility from inside your GUI.
wxExecute is a wxWidgets utility. If you are working in python then there will be a more direct way to do this using a python utility - some-one else will have to advise on that. I suggest you edit your question for clarity so a python expert can help you.
I just installed Django on my Mac and I am trying to figure out a way to edit the python modules directly from the terminal.
I know I can view the contents of a file using the less command in the command line, but is there a way I can edit Django modules from the command line, as well? It's just kind of annoying having to switch between the command line and a text editor, so I was hoping I could do everything in one spot.
Thanks,
Jerry
EDIT: rephrased.
There are lots of TUI text editors available for *nix. vim and emacs are popular, and are available in several forms for OS X.
This question is a little confused.
There's no such thing as "editing in Python", or "editing in Bash". I don't know what you're regarding as the "Python" that you have to switch to, but most people using a Mac edit their Python files in something like Textmate. That can be always running, and the Django development server automatically reloads when you save changes to any of the files in use, so this is the often most convenient way of doing it.
Bash is the shell that runs inside the terminal. Instead of talking about "editing in Bash", I expect you mean "editing in the terminal". Of course, there are hundreds of editors that run inside the terminal. One that comes installed on the Mac is vim, which I use all the time - but it has its fair share of idiosyncrasies, and takes a lot of getting used to.
However, if anything, editing in vim inside the terminal seems like it would involve more "switching" rather than less. You'd either need to have a separate terminal tab, and switch between the one running the server and the one running vim, or continually quit your server to go into vim.