本文介绍了在一个python脚本中加载环境模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python脚本中加载环境模块

以上解决方案在python交互式shell和可执行python文件&中的行为不同.我需要一些帮助来了解如何使其在exe.py设置中工作,在该设置中,导入语句似乎看不到环境变量PYTHONPATH.

The above solution behaves differently in the python interactive shell and within an executable python file & I need some help in understanding how to get it to work in the exe.py setting, where import statements appear not to be seeing the environment variable PYTHONPATH.

在python shell中,该解决方案允许加载修改PYTHONPATH的环境模块;随后,我可以从修改后的PYTHONPATH导入python模块.这是很棒的功能&正是我希望它在可执行的python脚本中执行的操作.

In python shell the solution allows loading of an environment module which modifies PYTHONPATH; I can subsequently import a python module from that amended PYTHONPATH. This is great functionality & exactly what I want it to do in an executable python script.

在python脚本(以#!/usr/bin/env python等为标题)中,它可以工作到PYTHONPATH并包含

In a python script (headed #!/usr/bin/env python etc), it works OK up to and including amendment of PYTHONPATH

if 'PYTHONPATH' in os.environ: print 'PYPATH:', os.environ['PYTHONPATH']
# nothing prints

execfile('/usr/local/Modules/default/init/python.py')
module('list')
# No Modulefiles Currently Loaded.

module('load', 'my_module')
print 'loaded my_module'
# loaded my_module
module('list')
#   1) /my_module
if 'PYTHONPATH' in os.environ: print 'PYPATH:', os.environ['PYTHONPATH']
# /home/me/py/my_module

但这已在python.exe中起作用

But that's as far as it works in a python.exe

尝试从my_module导入(在python shell中工作正常)会导致Traceback报告'ImportError:No module named module_1

Attempts to import from my_module which work OK in the python shell result in Traceback reports 'ImportError: No Module named module_1

据此,我想我可以得出结论,当我在python exe中运行此命令时,python未使用或看到"已修改的PYTHONPATH(但在交互式python shell中却看到了).

From this I think I can conclude that python is not using or 'seeing' the amended PYTHONPATH when I run this in the python exe (but does see it when in the interactive python shell).

这就是我被卡住的地方!有任何想法吗?帮助非常感谢.我敢打赌,有一个非常简单的解决方案,我已经忽略了&听到这个消息我会很高兴.

That's kindof where I get stuck! Any ideas? Help much appreciated. I bet there's a really simple solution I've overlooked & I'll be delighted to hear about it.

感谢&祝你有美好的一天

thanks & have a great day

垫子

更多有关阅读的内容表明python本身将PYTHONPATH的内容添加到sys.path中,但这既没有在交互式python shell中运行,也没有在我运行python.exe时发生.

Some more reading around suggests that python itself adds the content of PYTHONPATH to sys.path, but this is happening in neither the interactive python shell nor when I run the python.exe.

如果我使用sys.path.insert(1,os.environ ['PYTHONPATH'])在exe中手动执行此操作,那么我将获得所需的功能

If I use sys.path.insert(1,os.environ['PYTHONPATH']) to do this manually in the exe then I get the functionality I want

推荐答案

我认为pythonpath是在初始化期间在sys.path中添加路径时读取的,请参见.

I think that the pythonpath is read during the initialization for adding paths in sys.path, see some examples in sys.path() and PYTHONPATH issues .

因此,您需要更新的变量实际上只是sys.path-当python已经初始化时,现在更新os.environ ['PYTHONPATH']

So the variable you need to update is actually only sys.path - when python is already initialized it is too late for updating os.environ['PYTHONPATH']

这篇关于在一个python脚本中加载环境模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:59
查看更多