title: Pytest KeyError when attempting to access a command line variable
body: <p>I've re-created the question with a new simpler fake-setup.</p>
  
  <p>I have a framework that needs a command lane variable from pytest. This variable is called environment but when I attempt to access that variable I get a AttributeError: 'module' object has no attribute 'config'.</p>
  
  <p>Here is my tests setup:</p>
  
  <p><a href="https://i.stack.imgur.com/1fAUK.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1fAUK.png" alt="File organization"></a></p>
  
  <p>I know py.test loads in this order:</p>
  
  <ol>
  <li>Pytest plugins</li>
  <li>External Plugins</li>
  <li>conftest.py files in order from outer file to inner file.</li>
  </ol>
  
  <p>I think I'm running into an issue where when I load the inner conftest.py I attempt to import the framework. When I import the framework it attempts to access the py.test variable. This variable, even though pytest has seen it in the pytest_addoption() sections of my outer-conftest.py, isn't ready to be used in pytest.</p>
  
  <p>Contents of outer contest:</p>
  
  <pre><code># content of conftest.py
  import pytest
  
  def pytest_addoption(parser):
      print("First")
      parser.addoption("--cmdopt", action="store", default="type1",
          help="my option: type1 or type2")
  
  
  @pytest.fixture
  def cmdopt(request):
      return request.config.getoption("cmdopt")
  </code></pre>
  
  <p>Contents of framework.py:</p>
  
  <pre><code>import pytest
  
  
  class Environment:
  
      @staticmethod
      def env():
          '''Determine which environment we are operating in,
          if it fails - we assume dca
          '''
          return pytest.config.getoption('cmdopt')
  
  
  class Users:
      __pool = Environment.env()
  </code></pre>
  
  <p>Contents of inner conftest.py:</p>
  
  <pre><code>import pytest
  from testing.framework import Environment
  </code></pre>
  
  <p>Contents of test_sample.py:</p>
  
  <pre><code># content of test_sample.py
  def test_answer(cmdopt):
      if cmdopt == "type1":
          print ("first")
      elif cmdopt == "type2":
          print ("second")
      assert 0 # to see what was printed
  </code></pre>
  
  <p>I run the following command in the testing/ folder:
  py.test -q --cmdopt=type2</p>
  
  <p>I receive the following error:</p>
  
  <pre><code>First
  Second
  Traceback (most recent call last):
    File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 513, in getconftestmodules
      return self._path2confmods[path]
  KeyError: local('/home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/tests')
  
  During handling of the above exception, another exception occurred:
  Traceback (most recent call last):
    File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 537, in importconftest
      return self._conftestpath2mod[conftestpath]
  KeyError: local('/home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/tests/conftest.py')
  
  During handling of the above exception, another exception occurred:
  Traceback (most recent call last):
    File "/usr/local/lib/python3.4/dist-packages/_pytest/config.py", line 543, in importconftest
      mod = conftestpath.pyimport()
    File "/usr/local/lib/python3.4/dist-packages/py/_path/local.py", line 641, in pyimport
      __import__(modname)
    File "/home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/tests/conftest.py", line 5, in &lt;module&gt;
      from testing.framework import Environment
    File "/home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/framework.py", line 14, in &lt;module&gt;
      class Users:
    File "/home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/framework.py", line 15, in Users
      __pool = Environment.env()
    File "/home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/framework.py", line 11, in env
      return pytest.config.getoption('cmdopt')
  AttributeError: 'module' object has no attribute 'config'
  ERROR: could not load /home/damonp/Repos/stuff/&lt;my-name-redacted&gt;/testing/tests/conftest.py
  </code></pre>
  
  <p>Is there a good way to use an external framework that relies on a pytest command line variable?</p>