- 1 year ago
Python : How to set environment variables on VM windows
To set environment variables on a Windows Virtual Machine (VM), you can use the following methods:
Using Command Prompt or PowerShell:
- Open Command Prompt or PowerShell on the Windows VM.
- To set a temporary environment variable for the current session, use the
set
command:bashset MY_VARIABLE=example_value
- To set a permanent environment variable for the user, use the
setx
command:
Note thatbashsetx MY_VARIABLE example_value
setx
will not affect the current session but will be available for future sessions. You might need to restart the applications or terminals to see the changes.
Using System Properties:
- Right-click on "This PC" or "My Computer" on the desktop and select "Properties."
- In the System window, click on "Advanced system settings" on the left-hand side.
- In the System Properties window, click on the "Environment Variables" button.
- In the Environment Variables window, you can add or edit user-level or system-level environment variables.
Using Python:
- You can use the
os.environ
dictionary in Python to set environment variables programmatically. Here's an example:pythonimport os
os.environ['MY_VARIABLE'] = 'example_value'
- You can use the
Remember that when you set environment variables programmatically using Python, they will only be available within the Python process or any subprocesses launched from it. To set environment variables for the entire system, you should use the methods mentioned above, either using Command Prompt/PowerShell or through System Properties.
Also, note that if you are working with a VM hosted on a cloud platform (e.g., Azure, AWS, Google Cloud), some cloud providers may have specific ways to set environment variables on their VMs, so it's always a good idea to check the documentation of your specific cloud provider for any platform-specific instructions.