Setting Path in Python (for Windows)

A forum for general discussion of the Python programming language.

Setting Path in Python (for Windows)

Postby DragonForest » Tue Jun 04, 2013 3:25 am

Hello,

I've installed python in C:\python27\ and set my PATH variable in the Windows environment, so I can run python in DOS from anywhere:

e.g.,

C:\>python . . .

works fine in running the Python Interpreter.

However, when I want to pass a filename argument to Python, I still need to specify the full file path

e.g.

C:\>python testfile.txt [<- Doesn't work if testfile.txt is in C:\python27\]

I have to specify:

C:\> python C:\python27\testfile.txt

How do I tell the system to look for testfile.txt in the C:\python27\ directory/folder?

Many thanks in advance!
DragonForest
 
Posts: 1
Joined: Tue Jun 04, 2013 3:18 am

Re: Setting Path in Python (for Windows)

Postby metulburr » Tue Jun 04, 2013 4:00 am

python files should be ext .py

C:\>python testfile.txt [<- Doesn't work if testfile.txt is in C:\python27\]

this line indicates your lack of experience with the command line. You should get use to working in it if you plan on programming too.

the idea of setting the PATH is to run python anywhere on your system. AKA so you can run programs outside of that directory. You look like your trying to lose that ability by narrowing it back down to just that directory? In that case there would be no point to setting the PATH variable as all your programs would already be able to run in that directory.

If your running a program from outside its directory, you will need to get use to running it with the fullpath (or at least to its subdirectory) , but most people just change to that directory
Code: Select all
cd <directory>
python <filename>

instead of
Code: Select all
python <path/to/filename>


the way you have it currently set is the way you want it.
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1122
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Setting Path in Python (for Windows)

Postby snippsat » Tue Jun 04, 2013 11:13 am

How do I tell the system to look for testfile.txt in the C:\python27\ directory/folder?

You most add to environment Variables.
http://www.nextofwindows.com/how-to-add ... windows-7/
To Path you add ;C:\python27\;C:\python27\scripts

Start cmd type python,if it work python interpreter start.
Then you can do python <path/to/filename> from anywhere in cmd.
User avatar
snippsat
 
Posts: 91
Joined: Thu Feb 21, 2013 12:04 am

Re: Setting Path in Python (for Windows)

Postby Mike_S » Tue Jun 04, 2013 2:02 pm

Hi DragonForest

metulburr and snippsat pretty much gave you the details to get set up to run Python properly. If I may, can I offer a few tips too, I think their a good idea...

... As pointed out you add Python to the PATH so your system can find it --- C:\Python27
... As well, snippsat mentioned adding --- C:\Python27\Scripts --- this helps to locate scripts that might be needed depending on the packages installed.
... I like to add my own Python scripts to the PATH as well --- C:\MyPy --- short, sweet, and I avoid spaces in the name.

So, this particular PATH addendum would be --- ;C:\Python27;C:\Python27\Scripts;C:\MyPy

But, it does get old typing out the path to your scripts, especially when you have 5 thumbs on each hand like I do, that is why I also use a console enhancement to make using the command prompt a bit easier. Here's 2...
... http://sourceforge.net/projects/console/
... http://www.softwareok.com/?seite=Freeware/ColorConsole

Both are free and portable, Console is my favorite. I like having tabs, and more importantly you can have the default startup directory be your python (.py) scripts directory.

... I start Console in -- C:\MyPy> -- to run a script I type -- >python myscript.py -- and hit enter.
... It's nice to be able to open a second tab -- C:\>, type 'python' and now Python is running too --- >>> print 'Hello World!'

PowerShell may do all this too, not sure.

I have not tested this and I may have misunderstood but, if you add .PY and .PYC to PATHEXT (not PATH), if you have a standard Python install and Python registers these files, you may not have to type the extension only the name of the script --- >python myscript (rather than >python myscript.py). I use portable Python so nothing is registered and I've yet to confirm this.

Anyway, hope some of this is beneficial to you.

Mike
* Portable-Python 2.7.5.1 * Windows XP (x86) *
User avatar
Mike_S
 
Posts: 8
Joined: Sat Feb 09, 2013 4:59 am
Location: >!<


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests