Edit System Environment Variable Using Python - python-3.x

I was trying to add python path to the system evironment path by python.
I've tried
os.eviron['Path'] += ';C:/Anaconda3/'
However, I found this only make a difference in the script. I checked the system and didn't find ;C:/Anaconda3/ in the environment variable 'Path', which makes command python xxx.py can not be recognized by the system.
Is there any function or method to edit the system evironment path by python?

Could it be the spelling from environ to eviron?
or...
Maybe a quick batch script could help you do the trick as advised here. In the same question, you can find a way to do in python which would not survive a reboot here, but could do the trick.

Related

Why does cmd and powershell recognize "python" but not "py"?

I seem to be having the opposite issue as this user in this question. I can run python successfully, but py does not work for me. Overall, this is not a big deal but when I try to run code in VS Code, I get the error below:
py: The term 'py' is not recognized as a name of a cmdlet, function, script file, or executable program.
How can I either make VScode run python or change my system to accept py?
When I first downloaded Python, it was not placed into the path but I was able to fix that. I also confirmed that VS Code has selected the correct interpreter for Python in the same place.
EDIT: IMAGES
Path directory
Path used in enviroment
When you write "python" on CMD or Powershell, it checks the given keyword("python" here) in all the directories in Environment Variables.
In the case of python, the directory mentioned for python in Environment Variables > Path, you will find "python.exe" and not "py.exe".
So your cmd or PowerShell recognizes python and not py.

Cant Install pipx or Brownie for Python

I am very new with coding and was following along with these youtube videos and quickly realized something wasn't right, so I did some trouble shooting and couldn't figure it out.
When I tried to download pipx on VS Code I get this error
Python3 : The term 'Python3' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
I changed my environment variables, changed app execution aliases, and still nothing
I figured I'd check to see if python was working through VS Code and I get the same error
I tried downloading python through Microsoft store to see if that would help but nope.
Not sure what to do
I was having the same issue for a while even though I added the following directory to the environment variable and system variable. C:\Users\username\AppData\Roaming\Python\Python310\site-packages\pipx
What worked for me was adding the following directory to the environment variable and system variable. (c:\users\username.local\bin)
The directory (c:\users\username.local\bin) will be used to create the virtual environments.
I also found this when typing (python -m pipx ensurepath) which returned:
C:\Users\username\.local\bin has been been added to PATH, but you need to open a new terminal or re-login for this PATH change
to take effect.
C:\Users\username\AppData\Roaming\Python\Python310\Scripts has been been added to PATH, but you need to open a new terminal or
re-login for this PATH change to take effect.
The (C:\Users\username.local\bin) path was the only thing I did not manually enter into my environment variable and system variable, so thought I should give that a try and it worked.

Difference between $PATH, sys.path and os.environ

What is the difference between $PATH variable, sys.path and os.environ? I understand that they both serve as paths where python searches packages. But it'd be nice to have more elaborate response.
Just a working case from my practice is when I used the script with only os.environ before import on Ubuntu 16.04 I got ImportError: No module named XXX. At the same time on MacOS it worked well. After I added sys.path on Ubuntu I could get import module well.
This is actually more complicated than it would seem. It's unclear by the question if you understand the Linux/MacOS $PATH environment variable. Lets start there. The $PATH variable (in Python you're able to access the system environement variables from os.environ) denotes the current users $PATH variable as defined in various shell profile and environment files. It typically contains things like "/usr/bin" and other places where programs are installed. For example when you type "ls" into the system shell, the underlying system searches the $PATH for programs named "ls". So what actually gets executed is probably something like "/usr/bin/ls" I've included additional reading below.
sys.path on the other hand is constructed by Python when the interpreter is started, based on a number of things. The first sentence in the help page is as follows. "A list of strings that specifies the search path for modules. Initialized from the environment variable $PYTHONPATH, plus an installation-dependent default." The installation-dependent portion typically defines the installation location of Python site packages. $PYTHONPATH is another environment variable (like $PATH) which can be added to facilitate the module search location and can be set the same way the system $PATH can
Typically if you have non-installed sources (ie you have Python files that you want to run outside the site-packages directory) you typically need to manipulate sys.path either directly in your scripts or add the location to the $PYTHONPATH environment variable so the interpreter knows where to find your modules. Alternatively, you could use .pth files to manipulate the module search path as well
This is just a basic overview, I hope you read the docs for better understanding
Sources
Linux $PATH variable information
Python sys.path
Python site.py
sys.path
Is a list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
os.environ
Is a mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
Environment variable PATH
Specifies a set of directories where executable programs are located. In general, each executing process or user session has its own PATH setting.

Linux shell commands not found even though their paths are listed in the PATH variable

The google app engine cli commands cannot be found in the python sdk. I've already checked the google_appengine file. The shell commands are there, and I already added the app engine file path to the PATH variable. I echo the PATH variable and the directory to the app_engine file shows up, but still shows not found when I try to use a command that's inside the file. If I reference the command directly by using its whole path, the command works, but otherwise it won't. Is there anything else that needs to be done to reference a command in shell?
More error info please,
If I reference the command directly by using its whole path, the command works
I think your python path maybe wrong, Python 2.6 is not supported.
so are you have mulit-version python?
Thank you for your contributions everyone. It turns out the path I was using was wrong. Instead of /home/Programs/Apps/google_appengine it should have been /home/Programs/google_appengine. I guess the shell doesn't check to see if the path you add to the PATH variable actually exists. Who knew? Not me
Until I run into another problem stack overflow--which may be soon
See ya

How to set a windows env variable from cygwin .bashrc?

Background: I am trying to run a windows-installed version of python from within cygwin, rather than the cygwin version (some packages I am using prefer the non-cygwin version). The following post got me up and running: Using Windows Python from Cygwin. However, I am now having an issue.
Problem: The PYTHONPATH variable does not seem to be working for me. I try "import module1" when I am using python in an arbitrary directory. module1 is located here: c:\cygwin\home\mcsefl\testFolder\module1.py. And my PYTHONPATH variable is set in my .bashrc file in the following way: "export PYTHONPATH=$PYTHONPATH:/home/mcsefl/testFolder". However I still get a "No module named..." error.
Alternate tries: Since I am running the Windows Python, I thought perhaps python did not care about the cygwin environment variables (not sure if this is true..?) So, I also tried to set the windows PYTHONPATH env variable in the control panel of windows with no luck as well.
Thanks in advance
I deleted the line in my .bashrc file that tried to set the PYTHONPATH variable and just let it be set by Windows. This corrects the problem.
Side note: The title for this Question is a misnomer now that I re-read it. My issue was resolved without addressing that overall question

Resources