-
Install decouple, see https://pypi.org/project/python-decouple/ for details.
pip install python-decouple
-
Create an '.env' file and save it to GitHub root (e.g.
c:/repos/IoT
) directory from your GitHub forked clone, then add your entries as follows:IOTHUB_CONNECTION_STRING="{IoT hub Primary Connection String}" IOTHUB_DEVICE_CONNECTION_STRING="{Your Device Connection String}"
-
Obtain the environment variable by opening a python session and run the following script
cd {github forked clone root} python from decouple import config config('IOTHUB_DEVICE_CONNECTION_STRING')
for example,
cd c:/repos/IoT c:/repos/IoT> python >>> from decouple import config >>> config('IOTHUB_DEVICE_CONNECTION_STRING') >>> 'HostName=HubMsg********p2qwy.azure-devices.net;DeviceId=myDevice;SharedAccessKey=8IrO********ZUkg='
-
Add system environment variables by opening a PowerShell command prompt in Administrative mode and run the following script.
[Environment]::SetEnvironmentVariable('IOTHUB_CONNECTION_STRING','{connection string from previous step}','Machine')
-
Obtain the environment variable by opening a python session in the same directory as the '.env' file where you'll run your scripts.
import os os.getenv('IOTHUB_DEVICE_CONNECTION_STRING')
for example,
c:/repos/IoT python >>> import os >>> os.getenv('IOTHUB_DEVICE_CONNECTION_STRING') >>> 'HostName=HubMsg********p2qwy.azure-devices.net;DeviceId=myDevice;SharedAccessKey=8IrO********ZUkg='
-
Delete your system variable from a PowerShell command prompt in Administrative mode by changing the
{your environment variable}
name and running following script:[Environment]::SetEnvironmentVariable('{your environment variable}',$Null,'Machine')
For example
[Environment]::SetEnvironmentVariable('IOTHUB_CONNECTION_STRING',$Null,'Machine')