I've recently started learning python and got tired of running it on the command line (terminal mac os x). I wanted an environment that I could code nicely in and run that code only when pieces of it were done, not line-by-line as in the shell. I decided to use Xcode as the interface is clean and simple, and followed this tutorial to set up Xcode so that I could run python scripts.
My problem is that when I use raw_input() and then click run, I can't type a value to pass to the variable it's stored in. Take this simple line for example:
word = raw_input("Enter a word: ")
Later on in the program, word is printed. When I click run on Xcode, the prompt shows up as expected in the console:
Enter a word:
However, I can not type anything into it, the cursor is blinking so I know it is responding but when I type a value, nothing happens. I'm not sure what is wrong here, hopefully one of you can help me out.
If you really want to use an IDE for Python development, go for Aptana Studio 3 .
Once you are done with Pydev setup which is very easy Refer this for Pydev Setup.
Before running a script which expects some command line inputs you can click on the small arrow button adjacent to run button, it will give you a drop down menu with different options. Click on run configurations and then click on a tab named Arguments and enter your parameters there in the field names Program Arguments. Click on apply and you are good to go.
Next time you need to run the code just click on run and it will automatically fetch arguments that you have specified in the option above.
Hope this help.
You just need a text editor to store you Python script and run it in Terminal whenever you like. As an example, I use emacs. You may use vi, Sublime Text 2, TextWrangler or many other alternatives.
$ emacs a.py
word = raw_input("Enter a word: ")
print "Your word is: %s" % word
After the a.py file is saved, simply cd to the directory and run the script in Terminal.
$ python a.py
Enter a word: Hello
Your word is: Hello
Or import your script as a module under interactive mode.
$ python
>>> import a
Enter a word: Hello
Your word is: Hello
Related
Hi I am a beginner to Python. I am writing the following sample code for input but when I press Enter after entering value for input command, the program doesn't go forward after printing the first line. I've done it in both Atom and Sublime Text. A basic print statement works perfect.
print ("hello")
x = input("Enter value")
print(x)
Sublime does not support user input unless you install a third-party plugin. And Atom is no different.
If you like to input values interactively, I would suggest running your script from the app Python IDLE then go to file and open your Python script and run it.
Or, alternatively, open a CMD (if you are PC user) or Terminal (in case of MacOS) and execute your script directly from the command line interface, like below:
python yourscript.py
may be the problem is with the code editor. Try running the file using cmd if you are on windows or terminal on mac/linux.
python file_name.py
I'm unable to find/execute the interactive mode in visual studio code - could you guys step-by-step me? Seems like a ridiculous question, I guess, but none of the waypoints I Googled seem to work;
"View > Other Windows > Interactive menu commands)" https://learn.microsoft.com/en-us/visualstudio/python/python-interactive-repl-in-visual-studio?view=vs-2019
I don't have "other windows"
If there's something I need to install, please let me know.
(Yes, I'm a beginner).
In VSCode, you can enter the Python interactive window in the following places:
Option 1
F1 -> Python: Create Python Interactive Window
This is the Python interactive window that comes with VSCode.
You can enter the interactive window without inputting instructions.
Please use "Ctrl+Enter" to execute the code:
Option 2
Ctrl + Shift + `
This is the cmd window that comes with the VSCode integration computer.
We need to enter the command "python" to enter the Python interactive window:
PS
For more information about using Python in Visual Studio Code, you can refer to the official VSCode documentation: Python in VSCode.
If you have the Python extension, you can use the Python Interactive feature (this is a IPython/Jupyter console, which can run parts of your code as 'cells', i.e. snippets of code executed in one go).
This article writes about the interactive window and many more possibilities of using Jupyter in VS Code: https://code.visualstudio.com/docs/python/jupyter-support-py
Best option
When editing Python code, select something and press Shift + Enter. This will open the interactive window and run your selection as a single cell.
I have personally been annoyed many times by accidentally pressing Shift + Enter instead of Enter on a line of code which (alone) would be a syntax error. Then, one day I decided to start using this feature consciously.
Terminal fallback
Press:
Ctrl+`
This opens a system shell (bash, or on Windows: Powershell or cmd).
Run python, or ipython if you have it (pip install ipython), and use the interactive window. In the terminal, you can also execute your script, by typing python myscript.py arguments.
If you type # %% in your vscode editor while editing a .py file,
then a interactive cell is created and it can be evaluated. The nice part of this is that # denotes comment in a .py file so you can save and run your code as a script
I have downloaded Atom.io for window and done setup successfully, then I download the package for running codes. but the Python result always come with an error like the picture.
Can you help me on this issue? Thank you
Try typing in:
python myprogram.py
However, you will still receive an invalid syntax error because:
print(*hi*)
should be:
print('hi')
Also, if you want to enter the python terminal, type:
python
Then you can type these commands (i.e. print("hi")) directly into the terminal instead of creating a .py file and running it.
One final point, I use the following instructions to add the python to PATH: how to add python to path in windows.
You can check this Stack Overflow thread for more details.
However, if you just want to play around with Python in Windows:
Download Python and install it.
Go to Start > type "IDLE" and click "IDLE (Python)".
Start coding! For example, type print("Hello, world!") and press the Enter key.
My first contact with Python was IDLE and I liked it a lot. Have fun!
It looks like you're running this from powershell. You can tell it's powershell because it says "Windows Powershell" on the first line of the output. Here's what I would do if I were you:
To start the python shell type python and press enter.
1a. Enter what you want to run in python, print('test') for example
If you want to get out of the python shell, enter ctrl-z and press enter to return to powershell.
To run python scripts use the command python {script}.py
Installing python on path
If you tried the above steps and got an error to the effect of "I don't recognize python" you should ensure that python is correctly installed in your PATH.
You can follow these instructions to install python correctly.
I'm just getting into programming and took the common advice of using a free text editor with python. I seem to be running into a problem and everything I've tried so far hasn't worked.
I downloaded Anaconda from this url and whenever I put the location of python.exe into the run bar: C:\Users\Home\Anaconda2\python.exe all I get is the python console to pop up. I've tried adding: "$(FULL_CURRENT_PATH)" to the end as one user had suggested, but that just opens up the console and closes it immediately after. Any idea what I'm doing wrong?
First, I usually create a space for all my projects. Usually C:\projects so it's easy to find. Then I create a folder for specific work. I'd do C:\projects\learningPython.
Now, create a new file and save it to C:\projects\learningPython\hello-world.py
In the file put this line
print('Hello World!')
Then click save.
Now open a command prompt (enter cmd in the run bar)
now enter this in the command prompt:
python C:\projects\learningPython\hello-world.py
Hopefully this will work for you. Let me know if you do not see 'Hello World' appear on the command prompt.
After you followed nlloyd instructions and you are assured that your setup works and you can use Python from the commandline, I would use the nppexec Plugin to integrate Python (and other external tools) with notepad++ . (You can get it from Notepad++ PluginCentral or directly via the Plugins -> PluginManager.)
There I would create a nppexec script: with the following lines (use Plugins -> NppExec -> Execute... ):
npp_save
C:\Tools\Python\Python.exe $(FULL_CURRENT_PATH)
change C:\Tools\Python\Python.exe to whereever you have installed python
save this nppexec script
use the commands Execute... and Direct execute previous from Plugins -> Nppexec menu to select the nppexec script (if you have several) and repeatedly use the same nppexec script
I've got a script in python which looks exactly like this:
x = input("Enter your name: ")
print("Hello " + x)
input("Press<enter>")
I've saved it correctly, and when I open the .py file, the terminal opens, and then closes almost instantly. I've figured out it says SyntaxError: invalid syntax. I've checked my code and to me its correct? I'm new to Python and I'm also using Python 3.3.2, the latest version.
Why is this happening?
The Python 2.x function input() can only be used with integers.
3.x can be used with both strings and integers. You are probably using Python 2.x.
For Python2.x, you must use raw_input()
To get your code to work, you must use Python3.x
These are the steps that I would follow to run the file:
open Terminal (or Powershell if you're using Windows)
go to the directory in your terminal where the python file resides that you're trying to fun
run the python file using for Terminal: python filename.py
or for Powershell: filename.py
This should work for you. If you're double clicking the file and trying to get it to run that way then yes, it will show a Terminal pop up and close immediately. If you're trying to open it to edit it and you're using Windows, you need to right-click the file, and select "Edit with IDLE"
Before it says SyntaxError, you should also see something like File ..., line ...
which would give you the exact line where the error occurred.
Add the following lines in front of your code:
import sys
print(sys.version)
... here the rest
and launch the script the same way you did before. This will display what version of Python is really executed.