如何添加默认路径来查找 python 脚本文件?
我总是在弄清楚如何在 Windows 中正确设置 Python 方面遇到一些麻烦。
我已经设置了 path=%path%;C:\python27 ,所以我可以使用 python 打开 .py 文件。我只是不知道如何更改保存目录。
例如,我将所有自定义脚本保存在 Documents/Python 目录中。因为是Win7,所以没有我的文档。我希望能够在 IDLE 中输入“HelloWorld.py”并让它在该文件夹中搜索任何匹配的脚本名称。不过,我还不知道如何将此目录添加到默认的 Python 搜索路径。
有什么想法吗?
这是一次尝试。
>>> 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 `
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 `
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或设置您的
$PYTHONPATH
环境变量or set your
$PYTHONPATH
environment variable这不是运行脚本的工作原理。修改
%PATH%
环境变量以包含包含相关脚本的目录,然后从命令提示符(而不是 IDLE)运行该脚本。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.请遵循教程
您不能随机将
\
放入字符串中。当您查看错误消息时,请注意所有系统提供的路径元素都有
\\
来转义\
的含义。教程将向您展示如何使用
r"
字符串轻松实现此目的。Please, follow a tutorial
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.我把这个放在评论中,但我会放在一个答案中,以便更彻底一些。目前尚不清楚您是否想要将 HelloWorld.py 作为脚本运行,或者是否想要在其中导入某些内容。但它们是两个不同的东西。
如果您只想从 cmd 或 Powershell 运行 HelloWorld.py,则需要修改 PATH 环境变量。在 Windows 中,您可以在“我的电脑”>“我的电脑”中执行此操作。属性>高级>环境变量。单击 PATH,添加包含 HelloWorld.py 的文件夹的路径并保存更改。您需要重新启动 cmd 或 Powershell 才能看到更改,并且更改将持续存在。 (换句话说,这是永久性的更改)
如果您希望能够导入 HelloWorld 内容,那么您有几个选择,但最简单的是将要导入的代码包装到 HelloWorld.py 中的函数中。假设您当前的 HelloWorld.py 如下所示:
将其更改为:
然后,您只需将包含 HelloWorld.py 的文件夹的路径添加到 sys.path 即可。听起来你已经这么做了。因此,您将能够像这样导入:
如果您仍然希望 HelloWorld.py 能够像脚本一样运行,那么您需要将其添加到脚本的底部:
这告诉 Python 导入文件如果正在导入,则无需运行它。如果没有被导入,它将执行 if 块中的代码。
希望这能澄清一切。对于刚开始学习 Python 的人来说,这绝对是一个常见的困惑源。
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:
Change it to this:
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:
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:
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.