Problems running Python in Notepad++ [duplicate] - python

This question already has answers here:
How do you run a python script from within notepad++? [duplicate]
(6 answers)
Closed 7 years ago.
I'm having trouble executing my programs in Notepad++. I'm currently operating on Windows 7.
When attempting to run the program in the the interpreter, I have to jump through numerous hoops to actually get my program to execute. In Notepad++, I'm unable to provide additional text if I'd like to run a sys.argv command, or need to write in a name for a function.
Are there any solutions? Any easier way to run my code?

Run your code from command interpreter.It will look like this
C:\Python27\python.exe programe_name.py
change directory where your program is save before the command.I recommend you to download IDLE editor.It is easy way to learn python.

What I have been doing is this:
Press F5 to run.
Paste this | C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
Replace Python27 with whatever version you use.
Then run.
It should take what you're writing and run it, make sure to select Python as the current language too.
Sorry if this isn't what you're looking for, I'm just starting too!

There are better editors which can run python without a problem. Github's atom can do this easily and is a great free solution and Sublime Text 2 is also both free and paid.
However, if you absolutely have to run on NotePad++, do click Run on the Menu and then Run again. Now, type in the shell command you would like to run (Path-to-python.exe + Path-To-File) and click run

Related

How do force or change my terminal to run python executable file in python 3 just by opening the file?

My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.

Code pasting incorrectly into IDLE shell on Windows 10 [duplicate]

This question already has answers here:
Pasting multiple lines into IDLE
(4 answers)
Closed 3 years ago.
I was trying to copy and paste some code into my Python shell, but when more than a single line in pasted in, I get the error: SyntaxError: multiple statements found while compiling a single statement.
I have a computer running Windows 10. I have always been annoyed that I couldn't copy-paste code and run it.
Is there any way to get all the code to separate into different statements? As if I had written it line-by-line / copy-pasted each line separately?
Here's what's happening:
The problem stems from the way that the IDLE shell handles copy/paste of multiple lines. Namely, it takes the end of each line as the user hitting the enter key which causes problems when copying code. The problem can be solved by starting a new file in IDLE, copy pasting the code into the new window, saving it as a .py file and then running the file from cmd with python 'path\to\file'.
Let me know if this solves your problem.
The '>>> ' prompt in IDLE means "Enter one statement". Whenever you hit at the end of entered code, whether keyed or pasted, IDLE assumes that you have done just that and tries to compile your code as a single statement by calling compile with mode='single'. I have thought about how to handle multi-statement pastes, but it is a non-trivial problem.
For the present, paste into an editor window, and run from there. I have a scratch file that I use for this purpose. I use it often enough that I usually can load it from File => Recent Files, hit ^A (select all), ^V (paste, on Windows), and F5.
I think that Python is not added to the environment variables, and that's the first problem to solve. To do that, go to Control Panel\Environment Variables\System Variables and add the location to your python.exe file. For example, in my case I am using Anaconda, so I added this "C:\Users..\Anaconda3" to the environment variables. First, you need to know where is your python installed.
Then, open new command prompt and just write : python. If it works, then you see python version and other details. If not, then something wrong with the path setup.
After that, you can run the file that contains the multi lines, that you want to execute.
I hope it will help.

how to run python command line [duplicate]

This question already has answers here:
Running a python script from the command line in Windows
(3 answers)
Closed 6 years ago.
I have pycharm installed and do my class assignment in it and things work well. The teacher wants on to now run some of the programs in command prompt. Given that my programs and the path is here: "C:\Python27\python.exe C:/Users/sz5062/PycharmProjects/untitled1/main.py", how can I run a program that is saved the above path in command line? So, example, how would I run the main.py in command line, not shell prompt?
I have searched and searched for answers here, but I can't fine one. Thanks very much for your help.
This is the full instruction: Install Python and a programming text editor and write a program that prints one line other than 'hello world', then take two screen shots and upload them below. You should use the command line to execute the Python program you wrote in the text editor. Please do not use the IDLE Python Shell, the Python Interpreter (>>>), or a shortcut in your text editor to run the code.
When i run "python main.py", I get an error message "python is not recognized as inernal or external command".
On you Commandline enter:
C:\Python27\python.exe C:\Users\sz5062\PycharmProjects\untitled1\main.py
The above command should work from every directory.
First part is the program you start (python.exe). Second part is the argument you pass to the program(C:\Users\sz5062\PycharmProjects\untitled1\main.py). This is the path to your python code so that python.exe knows what you want to execute.
If you add C:\Python27\ to your PATH variable, you can navigate to your directory C:\Users\sz5062\PycharmProjects\untitled1\ and start your programm with:
python main.py
I'd recommend making sure that python is on your PATH (if you type python does it open a shell?) and then navigate to the C:/Users/sz5062/PycharmProjects/untitled1 directory. Once there you can do:
python main.py
and that should work.
I think I made this hard. since python was in installed on C:\python27, all I needed to run the command line was simply to write the code in a text editor, save it in the same directory and call it from there. So, essentially, c:\python27\code.py

Running a simple script from desktop [duplicate]

This question already has answers here:
How do I run a Python program in the Command Prompt in Windows 7?
(24 answers)
Closed 7 years ago.
So I'm an extreme beginner to programming, just starting the Python class on Coursera. Using Python 2.7.10
Anyway, I made a simple print statement script in Notepad++
print "Hello World"
and saved as a python file on my desktop
newprog.py
However when I try to run it a cmd window appears and disappears and I'm not quite sure whats wrong.
The other question that this was linked as a duplicate to is about accessing python through the command prompt, which I don't have a problem with. From answers given it is now apparent to me that my dilemma was due to an erroneous belief that the interpreter would remain open after running whatever script I wrote.
Sounds like your program simply opened, ran and exited. So nothing was wrong, it just all happened a bit quick for you to see it.
You should run it from a command prompt or get an IDE like Pycharm, which will allow you to both write and run your script in one program.
To run from command prompt, use either Windows Key + R and type 'cmd' or click start and type 'cmd' into search box. Then you can drag your script to the command prompt window and press Enter to run it.
If you wanted to run it by double click, you'd need something to stop it from finishing until you'd read the message. To achieve this you can use the raw_input function, which waits for user input.
So your script would then look like
print "Hello World"
raw_input("Press Enter to exit")
Then you could double click and press enter when you are ready to exit.
Go to the command prompt window
python
then type in
execfile('path to newfile.py here')
Your file will now be executed
I'm running python 3.4.3. But it should be the same, I hope.
Go go "..\PythonXX\Lib\idlelib" and look for idle.pyw NOT idle.py and using the you're able to execute simple one line commands like the one you have up there.
From that you can also create a new file and do more complicated stuff.
If you create a shortcut to your desktop, you'll be able to access it easier.
Let me know if it helps, or at least correct path.
Your script is probably working and then finishing, the result is shown but not for long. I recommend opening the console and running your script from there, or you could use a simple batch file to run python scripts and then wait for a key press.
To open the console you can use the Windows key along the R key, Win-R (to run a new process) and write cmd, or you look for cmd in your Window's start menu.
With the console opened, you must locate the path where your script is, you can use the cd (Change Directory) to get there, for example:
cd C:\Users\your_name\Desktop
and then write:
python newprog.py
to run your script.
Another option is to use this simple batch file (save it as python34.bat or similar, but the extension must be .bat, put it wherever you like):
#ECHO OFF
C:\Python34\python.exe %*
pause
#ECHO ON
And then use that to run your scripts by right clicking a python script file, open with (run with) and use this batch script as default (if you want). Also, if you have another version of Python, or is installed elsewhere, you must change the "C:\Python34\" part.
This is a computer we're talking about here. It might take you triple the time it takes a computer to multiple two numbers for example. With this notion in mind, the computer quickly prints then exits.
raw_input() # at the end of script wait for user to supply input, delaying script exit

python programming query

I'm new to Python programming.
My question is where to write python programs in linux--
in terminal or
any writing pad like gedit etc
Should we write one program again and again for running or we should call the program and run it.
After you install Python (it's installed by default on most Linux-es), you have two options:
Terminal. Just type python <ENTER> and you will be taken to the Python interpreter interactive prompt. For anything but the most basic stuff, however, you may want to intall IPython - an alternative interactive prompt that's much better than the default one.
In a file. Save your code into a .py file and run it with python myfile.py
But first and foremost, start by learning Python - the official tutorial is a great place to start. Among other useful information, its chapter 2 discusses how to use the Python interpreter for beginners.
Stackoverflow also has a lot of great resources for learning Python. This question, for example, and many others.
If you are learning, or you are evaluating expressions, you could run Python in terminal, or use IDLE.
But if you are writing large chunks of code, then you should consider using an IDE.
You could either use Geany, or use Eclipse with PyDev. I prefer Eclipse myself.
For running, you can run it using the command python program.py, or just add the line
#!/bin/python
to the beginning of your program, grant it execution permission using chmod, and run it with ./program.py command.

Categories

Resources