简体   繁体   中英

How can I add a default path to look for python script files in?

I've always had a bit of trouble figuring out how to get Python set up properly in Windows.

I've already set up path=%path%;C:\\python27 , so I'm able to open .py files with python. I'm just having trouble figuring out how to change the save directory.

For instance, I save all of my custom scripts in the directory Documents/Python. It's Win7, so no My Documents. I would like to be able to type "HelloWorld.py" into IDLE and have it search this folder for any matching script names. I haven't been able to figure out how to add this directory to the default Python search path though.

Any ideas?

Here's one attempt.

>>> import sys 
>>> sys.path 
['C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] 
>>> sys.path.append('C:\Users\Jimmy\Documents\Python') 
>>> HelloWorld.py 
Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> HelloWorld.py NameError: name 'HelloWorld' is not defined `
import sys
sys.path.append(YOUR_PATH)  # or .insert(0, YOUR_PATH) may give higher priority

or set your $PYTHONPATH environment variable

That's not how running scripts works. Modify your %PATH% environment variable to contain the directory that contains the script in question, then run the script from the command prompt, not IDLE.

I put this in a comment, but I'll put in in an answer to be a little more thorough. It's not clear if you want to run HelloWorld.py as a script, or if you want to import something inside it. They are 2 separate things though.

If you just want to run HelloWorld.py from cmd or Powershell, then you'll need to modify the PATH environment variable. In Windows, you do that in My Computer > Properties > Advanced > Environment Variables. Click PATH, and add the path to the folder containing HelloWorld.py and save your changes. You'll need to restart cmd or Powershell to see the changes, and the changes will persist. (It's a permanent change in other words)

If you want to be able to import HelloWorld contents then you have a few options, but the easiest would be to wrap the code you want to import into a function in HelloWorld.py. So say your current HelloWorld.py looks like this:

print "Hello World!"

Change it to this:

def hello_world():
    print "Hello World!"

Then, you just need to add the path to the folder containing HelloWorld.py to sys.path. It sounds like you've already done that. So then you'll be able to import like this:

import HelloWorld

HelloWorld.hello_world()
# Will output: "Hello World!"

If you still want HelloWorld.py to be able to act like a script, then you'll want to add this to the bottom of your script:

if __name__ == 'main':
    hello_world()

That tells Python to import the file without running it if it's being imported. If it's not being imported, it will execute the code in the if block.

Hopefully that clears it up. It's definitely a common source of confusion for people starting with Python.

Please, follow a tutorial

sys.path.append(r'C:\Users\Jimmy\Documents\Python') 

You can't randomly put \\ in a string.

When you look at the error message, notice that all of the System-supplied path elements have \\\\ to escape the meaning of the \\ .

A tutorial will show you how to use r" strings to achieve this easily.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM