Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: TypeError while using os.environ.get

I want to access a shell environment variable in my python script. I am trying this

import os
print os.environ.get["HOME"]

I get this error when I am executing in python (I am getting the same error in bash also)

Traceback (most recent call last):
  File "C:\Users\sraparim\Desktop\GitHub issues\issue #1187\test.py", line 54, in <module>
    print os.environ.get["HOME"]
TypeError: 'instancemethod' object has no attribute '__getitem__'
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Users\sraparim\Desktop\GitHub issues\issue #1187\test.py"]
[dir: C:\Users\sraparim\Desktop\GitHub issues\issue #1187]
[path: C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\WebEx\Productivity Tools;C:\Program Files (x86)\Cisco\OSD-ShellApplications;C:\Program Files (x86)\Sennheiser\SoftphoneSDK\;C:\Program Files\PuTTY\;C:\Python27\Scripts;C:\Python27;C:\Python27]

Please help...

like image 385
CuriousTechie Avatar asked May 10 '26 09:05

CuriousTechie


1 Answers

get() is a method on the environ object; use parentheses instead of brackets:

print os.environ.get('HOME')

or brackets without get, which implicitly calls __getitem__():

print os.environ['HOME']
like image 161
Chris Avatar answered May 11 '26 22:05

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!