1

I'm trying to create a python package installer for myself. I've also given an option to set up environment variable to the user by selecting the directory where python is installed.

What problem I was having was that I had to set it up every time when I restarted the program. So, I did a bit of research & found out that its working scope is only within the process.

Now no user would like to set it up again & again if he/she wants to install multiple packages(Let's assume he doesn't know how to set it up manually). Now this broke my heart(Also, couldn't understand the creating a new environment part & how do I go on about applying it in my case).

Tl;dr:

How to set up a os.environ permanently on user's system?

Community
  • 1
  • 1
Alok
  • 2,629
  • 4
  • 28
  • 40

1 Answers1

1

Well, it looks like your approach would bind user with single interpreter, and if he would like to try another python version it can be nontrivial. May be nice installation page for your project with example of how to set up virtualenv will do?

Anyway, you can try couple of things:

Save simple configuration file in user's home directory and any time user run installed script (with system default interpreter), read this file, get python directory path from there and then subprocess with updated environment.

Or (for systems with shebang support) your also can create simple runner like this:

#!<here goes user defined python directory absolute path>
# -*- coding: utf-8 -*-

import sys
from main_script import main


if __name__ == "__main__":
    sys.exit(main())

Hope it would help.

Community
  • 1
  • 1
Allactaga
  • 193
  • 1
  • 7
  • +1. didn't realise I was binding the user with single interpreter ;) – Alok May 21 '14 at 10:23
  • Regarding your first method: maybe i didn't understand correctly but wasn't that essentially restarting the environment? & that too with a local scope(i guess, or does `/usr/sbin:/sbin:` this "really" changes the environment variable?) – Alok May 21 '14 at 10:29
  • Yes, subprocess.Popen will only use copy of current process environment (by default) and would not change variables out of process scope. Please understand, that python is used by some of system utilities and changing interpreter for every script could lead to unexpected results. I am only suggesting to subprocess specific python script which you KNOW should use user defined interpreter. Also you could read [here](https://help.ubuntu.com/community/EnvironmentVariables) about changing environment variable on user login, but be aware that methods could vary for different shells and systems. – Allactaga May 21 '14 at 15:12