Python home directory in Windows / Problem with multiple versions - python

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.

Related

Migrating from PyCharm to VS Code: setting paths

I want to switch over to VS Code. I did the following to enable python with the packages I typically use, including a local utilities package on this machine:
Searched for the setting python.pythonPath and made it PycharmProjects/Project/venv/Scripts/python.exe for both this workspace and for my user
Changed my Windows user PYTHONPATH to PycharmProjects/Project/venv/Scripts/python.exe
Tried PycharmProjects/python.exe;local-utilities-directory
However, the folder I'm trying to manually set won't show up as a possible option when I click on my interpreter in the bottom right. Instead I'm defaulting to an older interpreter that doesn't have all the packages I've amassed.
While I can access some packages, while trying to pull in my personal utilities library, pip fails (the term pip is not recognized...)
I see that there are a few other ways to change the python path, however, to my understanding, whether you do it in a .json or via the IDE UI, shouldn't actually matter.
Any help would be appreciated.
So, I think I messed things up by offering too many paths.
I had to double check that the exact same path showed up first in:
Work setting pythonPath
User setting pythonPath
Windows profile path
Windows profile PYTHONPATH
I deleted the other "backup" paths, and it ended up working.

Running Python from Virtual environment?

Created virtual environment in ~/python/venv/venv_python2 folder.
I can run python in a regular way (for venvs):
source ~/python/venv/venv_python2/bin/activate
python
or I can run python with:
~/python/venv/venv_python2/bin/python
What is a difference between 2 above?
There is no difference.
You can check where python is running from by typing which python in bash. You'll see that it points to your virtual env when you're in it.
The purpose of the virtual environment is to isolate the dependencies inside it so you don't have to worry about knowing the precise path to the correct Python interpreter or the libraries you have installed. So while using the absolute path is technically not incorrect, it completely bypasses the facilities of the virtualenv, and could eventually end up pointing to the wrong location if you hardcode it in a script today, and decide to move, migrate, or refactor your virtualenv tomorrow.
There is a very slight, almost irrelevant difference between the two:
~/python/venv/venv_python2/bin/python specifies exactly which Python interpreter you want to use.
python runs which ever interpreter is found first when examining your path.
One of the things that source ~/python/venv/venv_python2/bin/activate does is that it puts ~/python/venv/venv_python2/bin at the front of your path, so that when you run /-free command, the first place the shell looks is that directory, meaning python will resolve to the one in your virtual environment.
So, the only way your two ways would differ is if something else modified your PATH in the meantime.

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

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.

Windows environment variables change when opening command line?

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

Categories

Resources