Getting os.environ to work with Python run via NSSM - python

I am stuck on an environment variables mismatch.
I run a Python script on Windows 10 via a program called NSSM.
At runtime, I do the following:
Load in parameters from a text file
Put its contents into the environment using os.environ.setdefault(name, value).
Try to load in environment variables using os.environ[name]
Result:any variables I added do not show up.
I am not sure why the variables I add aren't available. Can you please tell me what I am doing wrong?
A starting point is that NSSM uses environment variables from Windows HKLM registry: source (see bottom). I am not sure if this is the reason os.environ cannot see relevant variables.

I've had trouble using os.environ.setdefault in the past as well. Instead of that, say you were trying to add to the PATH environment variable, do the following:
os.environ['PATH'] += ";" + the_path_to_the_file
EDIT:
Also, for creating a new variable:
os.environ['new_var'] = 'text'

Well it turns out that my problem was outside of the scope of this question. #Recessive and #eryksun thank you both for answering, it put me "onto the scent".
It turns out my problem was using Python pathlib's Path.home().
When running via command prompt, it pulled HOMEPATH environment variable.
When running via NSSM, it pulled USERPROFILE environment variable.
This discrepancy in Path.home() was the real problem. It wasn't finding the environment variables because NSSM was looking in a totally different folder.

Related

Enviroment variables in pyenv-virtualenv

I have created a virtual environment with pyenv virtualenv 3.5.9 projectname for developing a django project.
How can I set environment variables for my code to use?
I tried to add the environment variable DATABASE_USER in /Users/developer/.pyenv/versions/projectname/bin/activate
like this:
export DATABASE_USER="dbuser"
When I tried to echo $DATABASE_USER an empty string gets printed.
Tried to install zsh-autoenv
And now I can echo $DATABASE_USER and get the value set in the .autoenv.zsh file.
But I can't seem to get the environment variable to be available to my django code:
If I try to os.getenv('DATABASE_USER', '') in the python shell inside the virtualenv, I get ''
What could be wrong? Is the zsh-autoenv variables just available for the zsh shell and not python manage.py shell ?
I was wondering a similar thing, and I stumbled across a reddit thread where someone else had asked the same question, and eventually followed up noting some interesting finds.
As you noticed, pyenv doesn't seem to actually use the bin/activate file. They didn't say what the activation method is, but like you, adding environment variables there yielded no results.
In the end, they wound up installing autoenv, which bills itself as directory-based environments. It allows you to create an .env file in your directory, and when you cd to that directory, it runs the .env file. You can use it for environment variables, or you could add anything else to it.
I noticed on the autoenv page that they say you should probably use direnv instead, as it has better features and is higher quality software. Neither of these are Python or pyenv specific, and if you call your python code from outside of the directory, they may not work. Since you're using pyenv, you're probably running your code from within the directory anyway, so I think there's a good chance either one could work.

Python path on windows

I have two installations of Python on a corporate Windows computer. One from the Anaconda distribution:
C:\Users\Me\AppData\Local\Continuum\anaconda3\python3.exe
Another one from a corporate installer:
C:\Users\Me\AppData\Local\Downloaded Apps\WinPython\python-3.4.3\python.exe
If I type "python" in the default "cmd" terminal or in the Git Bash, it says "command not found". Probably for the same reason, Jupyter does not allow me to create a Python 3 notebook. How do I set the paths so that Python is available system-wide? I prefer the Anaconda distribution's Python.
EDIT: Creating a new user variable PYTHONPATH and setting it to C:\Users\Me\AppData\Local\Continuum\anaconda3\ via Windows System Properties does not have an effect.
This answer describes step-by-step an approach that worked for me. However, as eryksun notes in the comment, the additional variable should not be named PYTHONPATH. I renamed it PYTHON, which works.
Strangely, adding the paths directly to the PATH variable did NOT work.
You have to add the path of your installation to the Environment variables.
Simply go to the System Properties / Environment Variables /
From there, create a new system variables and add your python path.
In the system variable section select the Path variable.
Add new variable C:\Users\Me\AppData\Local\Continuum\anaconda3\ as environment variable in your advanced System Settings.
This is from where your system will invoke the python interpreter.
For more details see this answer

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

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.

Categories

Resources