Windows environment variables change when opening command line? - python

Sometimes when I change my environment variables in Windows, and then use software the depends on those variables, they are not properly updated.
And good example is to change a variable, then open up Windows Command Line and echo the variable and see that it hasn't been changed, even though you properly changed it in the Environment Variables window.
Another example I'm dealing with right now:
I've been using Python 2.4.x for a while for a project, which uses the env var PYTHONPATH who's value has been:
C:\Python24;C:\Python24\lib
Today I installed Python 2.5.x for the project. I changed my PYTHONPATH to be:
C:\Python25;C:\Python25\lib
When I use Python 2.5 to run a script and do this:
import sys
print sys.path
It prints:
'C:\\PYTHON24', 'C:\\PYTHON24\\lib' (and some other Python 2.5 related default installation paths)
So clearly, the old PYTHONPATH environment variable changes aren't really sticking....
Does anyone know why this happens and how to fix it?

When you change an environment variable in the System Properties tab, the new value will propagate to the Windows Explorer, and any apps (such as cmd.exe) opened from the Windows Explorer (or the Run box, Start Menu, etc.) should see the new value.
However, if you're running a program such as an editor or python or some non-Microsoft program launcher, then change an environment variable, and then launch cmd.exe from that program (instead of Windows Explorer) you are likely to see the old value of the environment variable. The reason is that the running program ignored the notification from Windows saying that the environment has changed (not at all unusual), and since the launched process inherits the environment variables, the child process won't see the changes.
The workaround is to make sure you start your app from Windows Explorer or the Run box. Rebooting your machine will work also (if rebooting doesn't solve the problem, then something else is going on).

Related

How to use environment variables in blender

I added a enviroment variable writing in the ~/.bashrc file this two line
var="stuff.."
export var
using the python interpreter in a normal terminal this two lines of code works
import os
print(os.environ['var'])
but in a blender python console it generate a KeyError so printing os.environ list i can see that there isn't a item with 'var' as key
So i think that is a problem with the environment settings in unix system.
Can anyone help me and explain how export the environment variables for the other processes? Thanks and sorry for the english
The .bachrc file (and similar such as .cshrc) is read when your shell is started, similarly when you start a GUI desktop the shell rc files are read at the time of it starting and the variables at that time are then part of the environment passed onto any GUI apps, changes made while running do not get read in as you start a new app. You can find ways of setting environment variables for different desktops.
One way of passing environment variables into blender is to start it from a terminal window. The rc files will be read when you open the terminal, you can also manually set environment variables before starting blender.
Another way to set environment variables for blender is to start it from a script, this may be one called myblender that will be found in your $PATH or it can also be named blender if it will be found before the real blender. In this script you can set variables before starting blender and any changes will be in effect when you run it.
#!/bin/bash
var="stuff.."
export var
exec /usr/local/bin/blender "$#"
After updating ~/.bashrc you either have to source ~/.bashrc in the terminal where you launch blender or log out and log back in to your system, where the variable should then be in the environment.
If you need to get environment variables that may or may not be available, you can also do something like os.getenv('var', 'default value')

In Eclipse I can't get environment variable

Ubunto 14.04
I'd like to keep secret info (passwords etc.) in environment variables of Ubuntu. I placed them in user's variables.
/home/michael/.bashrc
export SECRET_KEY=nh9_j12rx4j_zqiw235_klvm183p5g8bz_s2_fl3auc # Django
In terminal I check:
michael#michael:~$ echo $SECRET_KEY
nh9_j12rx4j_zqiw235_klvm183p5g8bz_s2_fl3auc
In python run under michael:
>>> os.environ['SECRET_KEY']
'nh9_j12rx4j_zqiw235_klvm183p5g8bz_s2_fl3auc'
Now I run Eclipse IDE and try to get os.environ['SECRET_KEY']. Well, no such environment variable. I think, maybe Eclipse can't access michael's variables even if michael started Eclipse.
Could you help me cope with this problem?
Since eclipse does not read your .bashrc, I guess you should specify your environment variables in the context menu where you can set the default interpreter.
Go to Run -> Run Configurations... and Select tab "Environment".
Otherwise, if you want those environment variables available in Eclipse you need to put them in /etc/environment.
You may consider using profile file or bash_profile to store the environment variables. You can refer more here
Another approach would be to start Eclipse from an open terminal window where you can access the respective environment variable and Eclipse should see them.

query windows "CD" dynamic env var from inside python

"CD" is apparently a "dynamic" environment variable in windows. From inside python, it does not show up in os.environ dictionary; nor does it arrive when queried with os.getenv(). Is there a means of accessing this environment variable from inside python?
Before folks tell me to use os.getcwd(), please understand that os.getcwd() does not follow soft links. On linux and mac, I can access the PWD environment variable to get the current directory including the soft linked directories. I'm hoping to do something similar on windows as well, if possible.
Thanks.
The working directory of a process on Windows is not an environment variable. It is read by calling the Win32 API function GetCurrentDirectory. In Python that is wrapped up by os.getcwd().
There is no standard environment variable on Windows named CD. The command interpretor cmd.exe does create the illusion of an environment variable named CD, but it's private to cmd.exe. Since your code runs in the Python executable, that's just not relevant. Note that when you execute the set command in cmd.exe, there is no CD variable listed.
It's easy to verify that what I say is true. Write a simple C program that calls GetEnvironmentVariable passing "CD". You'll see that there is no such variable.
Even if you did happen to be able to hack into the cmd.exe process that started your Python executable, what good would it do you. Your Python process has a working directory that is distinct from that of cmd.exe. And who says that your Python process was even started from cmd.exe?
I'm not quite certain where your confusion lies, but the way to obtain the working directory in Python is to call os.getcwd(). And then if you want to resolve symlinks, you will have to do so yourself.

Python home directory in Windows / Problem with multiple versions

I'm using Python 3 for developing and CollabNet Subversion Edge as versioning software. Subversion Edge comes with a little program called ViewVC which is written in Python 2, which is also directly bundled with it. There is a system environment variable called PYTHONHOME.
If it is set to the Python 2 distribution from Subversion Edge, my Python 3 won't start (not even IDLE), instead giving a runtime error messagebox.
If it is set to Python 3, ViewVC doesn't work.
Is there a way to make both work at the same time?
You shouldn't need to set PYTHONHOME at all. Python uses it (if set) to locate its installation. Typically, it should be able to locate it without this variable, as well: by looking at the path name of the python executable, and, failing that, by looking into the registry.
Write a .bat or cmd file that saves the value of the PYTHONHOME env var, invokes ViewVC and wait for it to finish, then restores PYTHONHOME to the saved value.
Have you considered changing the Subversion Edge services to run as a specific user account, and then move the PYTHONHOME environment variable to a user-level variable for that account only? As opposed to a system-wide variable? It seems like it should work.
BTW, the PYTHONHOME variable is added for mod_python to work properly (which is what serves ViewVC). If you can find another way to get mod_python to work, then you could try that.

pythonpath and omniORB

In README file of omniORBpy-3.4 is written that I have to set PYTHONPATH as
set PYTHONPATH=%PYTHONPATH%;%TOP%\lib\python;%TOP%\lib\x86_win32
Where %TOP% is the top-level omniORBpy directory. (Windows machine)
I have done that and reboot my machine but when I try to run *.py files which have a line like
import omniORB
it gives me an error that no such module omniORB.
What I should do?
I think you will find that the README file of omniORBpy says that TOP must be set to the "the root of your omniORB tree" and not omniORBpy.
Not sure here, but I don't think, that changes made to the environment via a batch script will persist across reboots. Try setting the variable via the Workstation properties (sorry, I have no Windows machine at hand, and cannot give more than a few general directions):
Right click on the Workstation icon on your desktop.
Select "Manage..." (I think it was)
Somewhere in the advanced settings, you can modify environment variables (no need to reboot, but you may have to fire up a new CMD.EXE afterwards, as running apps might not get the change).
Alternatively, you can create a small batch script to start you application, and make it modify the environment before the application is started (I think, this is, what the README actually suggests)

Categories

Resources