Python 3.3.3 Running Files with Command Line - python

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

Related

Python script won't run from the command line. It shows no error

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"

How to import a file into Spyder

To learn python, I'm attempting the infamous baby names exercise.
I'm having trouble getting going. How do I import the file babynames.py from the folder C:\Users\user1\Desktop\google-python-exercises\babynames?
The directory is set to cd C:\Users\user1\Desktop\google-python-exercises\babynames, but the commmand import babynames is invalid.
I searched around for an answer, but most of the Google results I've found solve much more complex variations of this simple example.
Thanks for your time!
You shouldn't be importing the file - the instructions want you to edit the babynames.py directly. What you'll need to do is (assuming you're using spyder due to the tag on your question):
find and open babynames.py from Spyder
read through the file, follow the instructions, and edit where it is marked with your code here
run the file to test it. If you need to enter args (eg. for the default babynames code you need to give the output file with --summaryfile <file>.html, write them in the Command line options box in the run configuration dialog in Spyder.
If you're using the command prompt instead, you will need to edit the file using any text editor, and to run it, enter a command like: python babynames.py --summaryfile some-file.txt where the working directory is C:\Users\user1\Desktop\google-python-exercises\babynames and python is on your path. (you may need to give a different command to run python itself from cmd - it will depend on your setup).

Code won't write a file when using Python 3

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.

How to run python script on terminal (ubuntu)?

I'm new with python, I've been learning for a few weeks. However now I've just changed my OS and I'm now using ubuntu and I can't run any script on my terminal.
I made sure to have the #!/usr/bin/env python
but when I go to the terminal and type, for example python test.py
the terminal shows an error message like this
python: can't open file 'test.py': [Errno 2] No such file or directory
what do I do?
I must save the file in any specific folder to make it run on terminal?
This error:
python: can't open file 'test.py': [Errno 2] No such file or directory
Means that the file "test.py" doesn't exist. (Or, it does, but it isn't in the current working directory.)
I must save the file in any specific folder to make it run on terminal?
No, it can be where ever you want. However, if you just say, "test.py", you'll need to be in the directory containing test.py.
Your terminal (actually, the shell in the terminal) has a concept of "Current working directory", which is what directory (folder) it is currently "in".
Thus, if you type something like:
python test.py
test.py needs to be in the current working directory. In Linux, you can change the current working directory with cd. You might want a tutorial if you're new. (Note that the first hit on that search for me is this YouTube video. The author in the video is using a Mac, but both Mac and Linux use bash for a shell, so it should apply to you.)
Set the PATH as below:
In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python" and press Enter.
In the bash shell (Linux) − type export PATH="$PATH:/usr/local/bin/python" and press Enter.
In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python" and press Enter.
Note − /usr/local/bin/python is the path of the Python directory
now run as below:
-bash-4.2$ python test.py
Hello, Python!
Save your python file in a spot where you will be able to find it again. Then navigate to that spot using the command line (cd /home/[profile]/spot/you/saved/file) or go to that location with the file browser. If you use the latter, right click and select "Open In Terminal." When the terminal opens, type "sudo chmod +x Yourfilename." After entering your password, type "python ./Yourfilename" which will open your python file in the command line. Hope this helps!
Running Linux Mint
Sorry, Im a newbie myself and I had this issue:
./hello.py: line 1: syntax error near unexpected token "Hello World"'
./hello.py: line 1:print("Hello World")'
I added the file header for the python 'deal' as #!/usr/bin/python
Then simple executed the program with './hello.py'
First create the file you want, with any editor like vi or gedit. And save with a .py extension. In that the first line should be
#!/usr/bin/env python

Problems executing python script in notepad++

I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave

Categories