RB206
RB206

Reputation: 11

Setting environment variable in Python without OS module

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?

Upvotes: 1

Views: 1514

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

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.

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)")

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)

On ubuntu I have export PYTHONSTARTUP=$HOME/.startup.py in my .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.

Upvotes: 2

DmitrySemenov
DmitrySemenov

Reputation: 10315

solution:

windows

cmd /c "set app_env=development && python /path/to/python/script.py"

linux

21:14 development [~]% export APP_ENV=development && python
Python 2.7.6 (default, Jan 14 2015, 21:37:15) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ.get('APP_ENV');            
development
>>> 

so that way you can set PYTHONPATH as well

Upvotes: 1

Related Questions