relative import error in python and where can find python module source code
Question: I'm quite new to python. Though I understand the basic data types,control flow
,etc, I still feel a lit bit difficult from top-level view.

One of these questions is `relative import`. I have a piece of code from a
book trying to implement queue structure using python. When I run the code I
got error from the import, "ValueError: Attempted relative import in non-
package".

**Here is line of the import:**

    
    
    from ..exceptions import Empty
    

I'm now in my working project folder. My question is how can I make adjustment
to this line to make the whole piece of code work? I guess this "exceptions"
module is made by the author not the built-in module and somehow the author
does not include the module in the current folder. Where can I find python
built-in module source code so I can take a look?

**My system is ubuntu**

Thank you.


Answer: These are _explicit relative imports_. That syntax means that the file where
that line of code resides is trying to `import` the `Empty` module (I'm making
an educated guess about that, not seeing the actual dir structure, although
it's unusual for a module to have a capital leading letter -- generally that's
reserved for classes) from the `exceptions` subfolder which resides in its
parent directory.

See the [Module: Packages
doc](https://docs.python.org/2/tutorial/modules.html#packages) for more info,
including a specific folder-structure example with relative imports.

The book really should've presented the code in a self-contained directory
structure -- say, a _git_ repo you could clone -- where these intra-package
dependencies will just work. It's unlikely to be related to your system
install if it's stock (although certainly one can set _PYTHONPATH_ and other
such methods that could affect the environment).

What does the module structure look like?

I'd also recommend you look into
[_virtualenv_](http://virtualenv.readthedocs.org/en/latest/) in order to
sandbox your Python environments. (Although for custom code, you _may_ also
need to run a local _Pypi_ server or similar, although there are other options
- see @abarnert's comment below). It wouldn't on its own help this particular
issue, but it's a good idea in general for keeping projects and their various
package requirements isolated.

