何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了

2022-01-15 00:00:00 python environment-variables

问题描述

所以,我的 PYTHONPATH 定义中缺少一个分号.但这只是让我到目前为止.出于某种原因,直到我将 PYTHONPATH 中的目录显式添加到脚本的顶部,我的脚本才作为计划任务运行(在 WinXP 上).

So, it turned out i was missing a semi-colon from my PYTHONPATH definition. But this only got me so far. for some reason, my script did NOT work as a scheduled task (on WinXP) until I explicitly added a directory from PYTHONPATH to the top of my script.

问题是:我什么时候需要显式地在路径中附加一些东西,什么时候可以简单地依赖环境变量?

Question is: When do I need to explicitly append something to my path and when can I simply rely on the environment variables?


解决方案

也许您没有在正确的凭据(登录名)下运行计划任务.在系统属性"对话框中定义环境变量时,它们可以是用户级或系统级.如果您将 PYTHONPATH 定义为用户级别,那么您的计划任务必须以该用户身份运行才能正确设置.我相信将其设为系统级别意味着它将适用于所有用户,除非他们定义了自己的价值.

Perhaps you're not running the scheduled task under the right credentials (log-in name). When you define environment variables in System Properties dialog, they can be either User-level or System-level. If you defined PYTHONPATH as User-level then your scheduled task must run as that user for it to be set properly. I believe making it System-level would mean it would apply to all users unless they have their own value defined.

下面是一个屏幕截图,显示了设置环境变量的位置.它在 Windows XP 和 Windows 7 中都类似.右侧对话框的上半部分显示当前的用户级设置,下半部分列出所有系统级设置.

Below is a screenshot showing where one sets environment variables. It's similar in both Windows XP and Windows 7. The top half of the right-hand dialog box shows the current User-level settings, and the bottom half lists all the System-level ones.

如果 PYTHONPATH 出现在上层用户级别组的名称列表中,您可以通过首先删除相同名称和关联值中的一个,然后将其添加到下层系统级组中,从而有效地将其移动到另一个下层.为了节省一点打字,您可以编辑用户级变量 before 删除它,以便有机会首先复制其当前值,然后取消操作.这样,当您制作新系统级副本时,您就可以简单地将复制的值粘贴到其中.

If PYTHONPATH appears in the list of names in the upper User-level group, you can effectively move it to the other lower one by first deleting and then adding one of the same name plus associated value to the lower System-level set. To save a little typing, you can Edit the user-level variable before you Delete it to be given a chance to first copy its current value, then Cancel-out of the operation. That way, when you make the New System-level copy you'll be able to simply paste the copied value into it.

相关文章