1

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?

RB206
  • 11
  • 3
  • you must start your python with some shell code. you just need to change the env var's there. – Jason Hu Mar 26 '15 at 20:46
  • Is there a way to set PYTHONSTARTUP without the OS module? I can set pythonhome and pythonpath just fine but pythonstartup doesn't seem to work. – RB206 Mar 26 '15 at 20:48
  • you can change all these in shell. but by your description, you seem to be using python in low end embedded system, which i personally think that's the worst idea ever existing in the human history. – Jason Hu Mar 26 '15 at 20:50
  • Made a bit of progress, was able to get it to set the variable using export PYTHONSTARTUP=$PYTHONSTARTUP:/path/ but it seems it included : in the path. Now I can't overwrite the value of the variable, it just appends any new values to the end – RB206 Mar 26 '15 at 20:59
  • you can always overwrite env var by directly assigning. `VAR=10`. – Jason Hu Mar 26 '15 at 21:00
  • derp. gotcha. able to set the PYTHONSTARTUP variable to the path I want, but still can't load the module when I boot python, still need to run an export PYTHONPATH=path before I boot python every time. What I'm trying to do is make it skip that step essentially. Just that every time python boots in this environment it already points to that directory for PYTHONPATH. from what I understand this is done using PYTHONSTARTUP correct? – RB206 Mar 26 '15 at 21:05
  • i don't know what you want. try to put your `export`'s to `/etc/profile`. then reboot. – Jason Hu Mar 26 '15 at 21:13
  • You include an enviroment variable for one command in bash by including it right before the command i.e. `X=5 python script.py` – axblount Mar 26 '15 at 21:19

2 Answers2

2

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.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

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

DmitrySemenov
  • 9,204
  • 15
  • 76
  • 121
  • Trying to do this without OS module. I want to set pythonpath in shell, but I'm having to set pythonpath every time I boot up this instance of python. Trying to figure out a way to set pythonpath permanently instead of having to set it every time. – RB206 Mar 26 '15 at 21:26
  • Are you using linux or windows? – DmitrySemenov Mar 26 '15 at 22:10
  • You can put the export PYTHONPATH=value in /etc/rc.local (available to all users) or your ~/.bashrc (so it's available only for your own logged in user) – DmitrySemenov Mar 26 '15 at 23:11
  • Tried adding export PYTHONPATH=path to /etc/rc and still does not change value on boot of shell. Is there anywhere I can check that this is referencing the correct rc file? If it helps I'm using busybox. – RB206 Mar 26 '15 at 23:32
  • Are you using docker by any chance? – DmitrySemenov Mar 26 '15 at 23:37
  • Nope, not as far as I know. – RB206 Mar 26 '15 at 23:39
  • check if your inittab contains the lines for scripts to start and then modify it accordingly it seems you can modify /etc/init.d/rcS in order to define that sys env var you need – DmitrySemenov Mar 27 '15 at 00:27