Alok
Alok

Reputation: 2689

changing os.environ with global scope

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?

Upvotes: 1

Views: 3237

Answers (1)

Allactaga
Allactaga

Reputation: 193

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.

Upvotes: 1

Related Questions