-->

Python agent newrelic installation

2019-09-02 20:51发布

问题:

im trying to install python agent of newrelic on my webapp but i just cant. im using a file named code.py to start my app. I add at the begging:

import newrelic.agent
newrelic.agent.initialize('newrelic.ini')

and i also tryed:

 import newrelic.agent
 newrelic.agent.initialize('/root/web/newrelic.ini')

but i get some error like

 Traceback (most recent call last):
 File "code.py", line 2, in <module>
import newrelic.agent
  File "/usr/local/lib/python2.7/dist-packages/newrelic-        2.44.0.36/newrelic/agent.py", line 1, in <module>
 from .config import initialize, extra_settings
 File "/usr/local/lib/python2.7/dist-packages/newrelic-2.44.0.36/newrelic/config.py", line 34, in <module>
  import newrelic.console
File "/usr/local/lib/python2.7/dist-packages/newrelic-2.44.0.36/newrelic/console.py", line 5, in <module>
  import code
File "/root/web/code.py", line 3, in <module>
 newrelic.agent.initialize('newrelic.ini')
 AttributeError: 'module' object has no attribute 'agent'

can someone help?? thanks

回答1:

You need to rename your script to something other than code.py. (And, also delete code.pyc.) The problem is your script name conflicts with a standard Python module called code, so when the agent tries to import code, it winds up importing your script, not the standard module.

See this description of the "name shadowing trap" for more details: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-name-shadowing-trap