在没有OS模块的Python中设置环境变量 [英] Setting environment variable in Python without OS module
问题描述
我正在使用一个由于大小限制而不使用操作系统模块的python,而且我试图让它从一个目录启动时导入一个模块。从我的理解,我可以使用PYTHONPATH设置它,但是如何在没有os模块的情况下设置该变量?
I'm using a build of python that doesn't use the OS module because of size constraints, and I'm trying to have it import a module on startup from a directory. From what I understand I can set this using PYTHONPATH, but how would I go about setting that variable without the os module?
推荐答案
你想要 PYTHONSTARTUP ,您可以在其中创建启动文件,您可以导入任何要启动python shell时导入的模块。
You want PYTHONSTARTUP, where you create a startup file and you can import whatever modules you want which will be imported when you start a python shell.
在我的主目录中,我有一个文件 .startup.py
,看起来像下面这样:
In my home directory I have a file .startup.py
that looks like the folowing:
import datetime, os, pprint, sys, time
print("(modules imported datetime, os, pprint sys, time)")
每次启动shell时,都会导入模块:
Every time I start a shell the modules are all imported:
~$ ipython
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
Type "copyright", "credits" or "license" for more information.
IPython 3.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
(imported datetime, os, pprint, re, sys, time)
在ubuntu我在我的 .bashrc
中导出PYTHONSTARTUP = $ HOME / .startup.py
。
On ubuntu I have export PYTHONSTARTUP=$HOME/.startup.py
in my .bashrc
.
我在名为 my_mods
的目录中保留了几个不同的模块,将其添加到我的pythonpath中我添加了 export PYTHONPATH = $ HOME / my_mods
再次在我的 .bashrc
。
I keep a few different modules in a directory called my_mods
, to add it to my pythonpath I added export PYTHONPATH=$HOME/my_mods
again in my .bashrc
.
这篇关于在没有OS模块的Python中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!