Where do I save python files ?

A forum for general discussion of the Python programming language.

Where do I save python files ?

Postby M4RTIN » Tue Mar 19, 2013 8:10 am

I made a python file with notepad++ called ex1.py then i went to command prompt and typed in python ex1.py but I got "python: can't open file 'ex1.py': [Errno 2] No such file or directory" so where do I need to save the ex1.py ? or what do I need to do ? I spent about 4 hours yesterday looking for a way to do it. I made
M4RTIN
 
Posts: 3
Joined: Tue Mar 19, 2013 8:05 am

Re: Where do I save python files ?

Postby micseydel » Tue Mar 19, 2013 8:58 am

Hello and welcome to the forum, and learning Python!

You should find a tutorial on using the command line on whatever OS you use, namely for commands like cd, ls (Unix [Linux/OS X]) or dir (Windows), pwd (Unix, not sure about Windows though it may be the same). If you're not familiar with file hierarchies yet, as a general computer skill, then you will want to pay attention to those from now on.

Alternatively, there are IDEs that will run your code for you. Since learning to use a command line is arguably harder than basic programming, you may want to do this and then go back to learning the command line later. Tackling both at once could certainly be frustrating. If you're on Windows, then your Python distribution came with an IDE called IDLE* which might show up under your programs as "Python GUI" (I believe). You can get this IDE in OS X as well (and Linux, which I suspect you are not using). IDLE uses two windows, one with your code and one with its output and whatever input you give it, when the main window is the one with your code in it, push F5 on you keyboard and it will run you program in the other window. The one with your input and output also allows you to interactively use Python instead of saving it in a script.

I hope I've provided a fair amount of information here, if you have more questions, feel free to post again and make sure you've read this so you don't accidentally bother anyone into not helping you. On top of what's there, sometimes screenshots help with people who are very new to programming and use of the command line, but you can use your judgment about whether it's worth it.

* Some people utterly hate IDLE. I'm not one of them, and I think it can be great for people new to the language. I use Sublime Text as my editor, and it runs my code for me when I push F7, but the last time I tried it, it couldn't get user input, and I don't care for the interface. When I use Sublime Text and run my programs, I use the command line.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1357
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Where do I save python files ?

Postby metulburr » Tue Mar 19, 2013 1:32 pm

you could also read Executing Python and if you still have questions that means we need to add what answers your question to this tutorial. So please give advice where it requires, and learn where it teaches.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where do I save python files ?

Postby M4RTIN » Tue Mar 19, 2013 3:56 pm

I went to the environment variables and made a PYTHONPATH and set the value to C:\My_python_lib but still when I go to cmd and type in python ex1.py it says this "python: can't open file 'ex1.py': [Errno 2] No such file or directory" :x
M4RTIN
 
Posts: 3
Joined: Tue Mar 19, 2013 8:05 am

Re: Where do I save python files ?

Postby setrofim » Tue Mar 19, 2013 4:15 pm

PYTHONPATH is what Python uses to look for modules that you import inside your scripts. It does not use it to look for scripts you specify on the command line.

Did you read any of the replies to your previous post? metulburr linked to the post that explains how to run Python scripts in great detail. Particularly this part (emphasis added):
metulburr wrote:Then you open a Terminal/DOS prompt and change directory to your test.py and write this:
Code: Select all
python test.py



micseydel has explained what you need to look into to find out how to do that:
micseydel wrote:You should find a tutorial on using the command line on whatever OS you use, namely for commands like cd, ls (Unix [Linux/OS X]) or dir (Windows), pwd (Unix, not sure about Windows though it may be the same). If you're not familiar with file hierarchies yet, as a general computer skill, then you will want to pay attention to those from now on.


@micseydel:

by the way, pwd equivalent on Windows is just
Code: Select all
cd

On Unix, this would change directory to your home; on Windows (in cmd.exe), it prints out you current working directory.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Where do I save python files ?

Postby metulburr » Tue Mar 19, 2013 4:36 pm

"python: can't open file 'ex1.py': [Errno 2] No such file or directory"

because your getting this error. Python is known, the file is not. So PYTHONPATH to get python to be known is moot. You get this error for example when you run a file that does not exist No such file or directory or your ex1.py. So then you have to think back. What is in my current directory? What directory am i in? If your not in the directory that the file is in. It will give you this error. So you need to change to that directory to run the file, or you need to run the fullpath of the file.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where do I save python files ?

Postby M4RTIN » Tue Mar 19, 2013 5:59 pm

"To make python execute in any directory in Windows. You have to add python to the PYTHONPATH environmental variable. To get there: My Computer > Properties > Advanced Systme Settings > Environmental Variables > create new variable PYTHONPATH and set C:\My_python_lib. Where My_python_lib is the directory of python." I want to be able to just make python file then save it and open it in cmd i've been looking for a way to do that for over 5 hours and i still cant find it.
M4RTIN
 
Posts: 3
Joined: Tue Mar 19, 2013 8:05 am

Re: Where do I save python files ?

Postby metulburr » Tue Mar 19, 2013 6:41 pm

forget about PYTHONPATH. IF you had a problem with that the error would be:
python is not recognized as an internal or external command,
operable program or batch file.


but you do not get that error. So Python is working fine. It looks like you are executing python from a directory that your file is not in.

copy and paste the commands you are giving, the prompt, everything!
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where do I save python files ?

Postby setrofim » Tue Mar 19, 2013 6:49 pm

M4RTIN wrote:"To make python execute in any directory in Windows. You have to add python to the PYTHONPATH environmental variable. To get there: My Computer > Properties > Advanced Systme Settings > Environmental Variables > create new variable PYTHONPATH and set C:\My_python_lib. Where My_python_lib is the directory of python."

Ah yeah, it does say that. Please accept my apologies; looks like it is I who needs to learn read more carefully...

@metulburr: you might wanna fix that in the post, as PYTHONPATH is used to look for imported Python modules; see: http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH. I'm assuming that text talks about finding the Python executable, in which case it should be "PATH", not "PYTHONPATH" (which is still not what OP is looking for). Might be also worth changing the text to make it more obvious that this will make the Python executable runable from anywhere, not any script.

M4RTIN wrote:I want to be able to just make python file then save it and open it in cmd i've been looking for a way to do that for over 5 hours and i still cant find it.

OK, to run your script you can either do
Code: Select all
C:\>python.exe C:\my_python_lib\test.py

(the "C:\>" in the example represents the prompt you see when you start commandline -- you don't need to type that)
i.e. specify the full path to your Python file, or
Code: Select all
C:\>cd C:\my_python_lib
C:\my_python_lib>python.exe test.py

i.e. change directories to the location of your script before executing Python.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Where do I save python files ?

Postby metulburr » Tue Mar 19, 2013 6:53 pm

@metulburr: you might wanna fix that in the post, as PYTHONPATH is used to look for imported Python modules; see: http://docs.python.org/2/using/cmdline. ... PYTHONPATH. I'm assuming that text talks about finding the Python executable, in which case it should be "PATH", not "PYTHONPATH" (which is still not what OP is looking for).


My apologies. It has been changed.

Might be also worth changing the text to make it more obvious that this will make the Python executable runable from anywhere, not any script.

I didnt think that would of needed to be said. But will change also.

Not that the path had any thing to do with this question, i think its just he is not in the directory of the file. But I am horrible at windows, because i spend almost all my time in linux. So maybe some of you that use windows a lot could review the tutorials that i wrote make sure its up to par with windows.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Where do I save python files ?

Postby Mikey Boy » Thu Mar 21, 2013 4:39 pm

M4RTIN wrote:"To make python execute in any directory in Windows. You have to add python to the PYTHONPATH environmental variable. To get there: My Computer > Properties > Advanced Systme Settings > Environmental Variables > create new variable PYTHONPATH and set C:\My_python_lib. Where My_python_lib is the directory of python." I want to be able to just make python file then save it and open it in cmd i've been looking for a way to do that for over 5 hours and i still cant find it.


What directory are you saving your Python file in? Can't you simply cd to that directory and then run Python from there?
Mikey Boy
 
Posts: 3
Joined: Wed Mar 13, 2013 7:54 pm


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 2 guests