title: How do I construct a python import statement to check for the presence of a module alias in the calling namespace?
body: <p>I'm trying to write a small package that adds a function to the <code>plotly.express</code> package. I'm trying to get this to work:</p>
  <pre><code>&gt;&gt;&gt; import plotly.express as px
  &gt;&gt;&gt; import plotly_ecdf
  &gt;&gt;&gt; px.ecdf()
  'ECDF plot here!'
  </code></pre>
  <p>This actually works as is with this in <code>plotly_ecdf.py</code>:</p>
  <pre><code># plotly_ecdf.py
  
  import plotly.express as px
  
  
  def ecdf():
      print(&quot;ECDF plot here!&quot;)
  
  
  px.ecdf = ecdf
  </code></pre>
  <p>What's weird is that if I haven't already imported <code>plotly.express</code>, the <code>plotly_ecdf</code> import also fails:</p>
  <pre><code>&gt;&gt;&gt; import plotly_ecdf
  &gt;&gt;&gt; px.ecdf()
  NameError: name 'px' is not defined
  </code></pre>
  <p>I tried making use of <a href="https://stackoverflow.com/a/62885157/2680824">this response</a>. I can access and write variables when called in a function, but I can't it to work during an import statement.</p>
  <p>So how do I modify <code>plotly_ecdf.py</code> to check if <code>plotly.express</code> is already imported so I can throw an error if it isn't? Bonus points if this can modify whatever alias <code>plotly.express</code> has been assigned to. Thanks in advance!</p>