I am using Notepad++ as an editor and I am running Python 3 from Notepad.
This is the code:
import sys
def write():
print ("Creating new file")
name = 'NewFile.txt'
file = open(name,'w')
file.close()
write()
The problem is not the code itself, I think. When I run the code from Windows PowerShell like this: python code.py, it works fine and creates the file, but this is creating the file using Python 2.7. I need to use Python 3.
When I run Python 3 from Notepad++ the file won't be created.
I tried running Python 2.7 from Notepad++ but it just doesn't work. I run it like this:
C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)"
or with Python 3 I run it like this:
C:\Python35\python.exe -i "$(FULL_CURRENT_PATH)" .
I also run Notepad++ as administrator.
I think I could solve this by running Python 3 alongside Python 2 in PowerShell, but I don't know how and the answers to these questions do not work for me:
How do I add Python 3.3 to Powershell?
I'm trying to use python in powershell
I am open to changing my editor (Notepad++) or any solutions really.
So, how can I make Notepad++ create a new file? Or How can I make Python 3 run in PowerShell? Or Which editor could I use to fix this? Or maybe my code is just wrong.
Edit: When I say it doesn't work I mean that the file will not be created even tough my code runs (no error msg).
To be clear, what you're describing seems to be: a) when you run your file manually from the command prompt, it gives the expected results; b) when you run it instead through Notepad++, you don't see any evidence that it runs at all. Is that right?.
Yes, that's right. I'm also not sure what is the interactive interpreter.
Most likely the file is being created, but not in the place where you expect it. You define the file without a path, so it's created in the current working directory. Check the program directory or the system directory for the file.
Specify output files with the full path to avoid this issue:
import sys
def write():
print ("Creating new file")
name = 'C:/path/to/NewFile.txt'
file = open(name,'w')
file.close()
write()
I think the problem is related to the encoding of your input file.
When you open the file, put an encoding option to match your editor.
Related
I'm trying to run a simple Python Script on the CMD but nothing happens when I run it. I get no errors or anything. The py script is just a simple print ("Hello World").
All my .py files are in the Python/Projects file.
http://puu.sh/FFfJm/ee63955506.png
Just some context regarding this:
I did install Python then Pycharm then Anaconda. I don't know if that has anything to do with conflicting Python files.
python isn't in your path. Checkout Adding directory to PATH Environment Variable in Windows, which is a good reference to the question of adding a variable to the path in CMD.
Are you sure you entered your command line query correctly?
python python_file_name.py
If you just entered your file_name.py it might have executed it but ended it simultaneously before you could see any result.
also try putting your file_name in double quotes.
If you have Python in PATH but script execution still doesn't work, inspect the C:\Users<user>\AppData\Local\Microsoft\WindowsApps folder.
There might be a zero-size python.exe file. It seems that this folder is earlier in PATH and zero-byte file intercept the console command execution. The microsoft store is opened with python page if I run that file.
I removed this folder and python script start working again.
You also can ensure the correct python is available to the terminal with 'where python' to see the full path.
From your screenshot, you'd need to call
python "Hello World.py"
with quotes, not
python Hello World.py // no, won't work
The reason is, when you don't include quotes around a filename with a space, it only takes the FIRST thing you wrote as the script name, the rest of the "words" are just passed as "command line arguments"
When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)
I just started learning Python 3.3.3 with the book "Learning Python" from O'Reilly by Mark Lutz, 4th Ed.
I was able to run code interactively, but when I tried to run the code from files through the command line, I just kept getting syntax errors.
FYI, I am using Windows 7.
The book asks that one create a file using a text editor with the following:
import sys
print(sys.platform)
print(2 ** 100)
x = 'Spam!'
print(x * 8)
I did this using notepad and saved the file with the suffix "py" and the file type "all.files" so there would not be a "txt" file saved. When I try to run the script in the python command I get an error message. I've tried entering the PATH but that as not worked either. It is located in C:\Python33\
Any guidance you are able to provide is very welcomed. I've tried numerous ways for the past 1 hour.
So, I have the following in my Path variable,
C:\Python27;C:\Python27\Scripts;
Is that what you have? Maybe close your command line and open it again, or shut your machine off and turn it on again--the system variables do not update instantly.
where have you saved your script?
what is the output of this?
cd C:\Python33; dir; python script1.py
First of all, make sure the file you saved as ".py" has the right ext instead ".txt", otherwise you can easly download "notepad ++" to save as the right extension".
If the file you saved is right, then you have to execute the "cmd.exe", and go to the "python33" folder using "cd c:\Python33" or where you have it, Once you're in the right directory, write "python PATH.py", where the PATH is the right path of the file.py, or you can just move the file into the cmd.
Hope it helps
I am having some trouble with opening a .py file. I have a program that calls this .py file (i.e. pathname/example.py file.txt), but instead of running the python program, it opens it in Notepad. How to I get it to run?
The program itself takes in a file, and creates an output that is more readable.
Edit: The operating system is Windows 7. And the file that is calling the python is a .bat file.
Edit 2: It looks like I had to reinstall python for some reason... but it looks like it is finally working. Why reinstalling never comes to mind in the first place... And then I had to change how the file extention was opened. Thanks guys
This happened because most probably you have set notepad as the default program to open a .py file. Go to default programs app in windows. Select choose app by extension. Here search for .py files. Change the option from notepad to python. This should solve your problem.
okay.
1) i tried turning it off and on again.
2) i uninstalled and reinstalled python
still no joy. and then!
in windows explorer there's an open with option that sets the default program that windows is pointed toward if you click on the filename or enter it on the command line. change that from notepad or whatever it is if it's not python. change it to python. then presto. no problem-o.
You need to run it from the command line.
http://docs.python.org/2/faq/windows.html#how-do-i-run-a-python-program-under-windows
Are you trying to run the program like this?
/dirdir/MyPythonScript.py
try the following instead
python /dirdir/MyPythonScript.py
I would like a user on Windows to be able to run my Python program, so I want to convert it to a .bat file. Is there a way to convert it? I've tried searching, but didn't find anything.
Unless your script is trivial it will not be possible to 'translate' it into a batch file. However two options exist:
Create a batch file to run the python script
Attempt to compile the script into an executable
The first option is trivial. Simply create a batch file as so:
#ECHO OFF
PATH_TO_PYTHON\python.exe PATH_TO_SCRIPT.py
If you are in a corporate environment you could put a python installation on a network and create a batch file to run the script from there. Otherwise you will need the user to install python. If python is on their path then the batch file can be simplified to:
#ECHO OFF
python PATH_TO_SCRIPT.py
Alternatively, there are options available that attempt to compile scripts into .exe files. I've never had any success with them, but py2exe seems the most common.
No, I don't think you can reasonably expect to do this.
Batch files are executed by the Windows command interpreter, which is way way more primitive.
Python is a full-blown programming language with a rich and powerful library of standard modules for all sorts of tasks. All the Windows command interpreter can do is act like a broken shell.
On the other hand, Python is available on Windows, so just tell the user to install it and run your program directly.
you can do it in 2 ways :
create a normal text file with an extension .bat and write
#ECHO OFF
"python.exe location" "your file.py location"
create a normal text file with an extension .bat and write
"python.exe location" "your file.py location"
pause
(sorry for my English Im from armenia)
After 10 years I finaly did that without py2exe and some other things
I used openai playground to do that and thats worked:
I just writed "convert python code into .bat file: [my code]"
Just create a batch file that contains this two lines:
yourfilename.py
pause
Then run the batch file by double-clicking it.