I am a perplexed Python enthusiast. I posted What else can I do to troubleshoot a package not importing in python yet imports in ipython while in a virtualenv? thinking that I was having a problem with my virtualenv. Some more troubleshooting has revealed this is probably not the case.
I would appreciate any insight or troubleshooting tips so that I can get on with package development. Thank you in advance!
Synopsis of my problem on Ubuntu 14.04.1 LTS system with Python 2.7.6.:
- I have a script named test_dummy.py that adds package paths to sys.path and then attempts to import the packages.
- The packages import without error when the script is run with
run test_dummy.py
inside of ipython. - ImportError exceptions are thrown for certain packages when the script is run with
python test_dummy.py
. - What is most frustrating is that the packages that contain code i.e. more than just an
__init__.py
file import without exception. Simple test packages with only a__init__.py
file in them do not import as expected. I cannot determine what is causing the simplest of packages to throw exceptions on import when the script is called from python i.e.python test_dummy.py
.
My pwd is as such:
.
├── browser
├── display
├── hello_world
├── singleton
├── test_a
└── tests
test_a is as such:
test_a
├── __init__.py
└── __init__.pyc
hello_world is as such:
hello_world/
├── hello.py
├── hello.pyc
├── __init__.py
└── __init__.pyc
hello.py is as such:
HELLO = 'Hello, world!'
print HELLO
The other packages ['browser', 'display', 'singleton'] all contain init.py files and other code that throws no known exceptions when run.
tests is as such:
tests
└── test_dummy.py
The code in test_dummy.py:
import importlib
import os
import sys
HOME = os.path.expanduser('~')
PYTHON = os.path.join(HOME, 'development/my_python')
PACKAGES = [
'browser',
'display',
'singleton',
'test_a',
'hello_world',
]
MODULES = [
'',
'',
'',
'',
'hello',
]
IMPORTS = ['.'.join((pkg, module)).strip('.')
for pkg, module
in zip(PACKAGES, MODULES)]
"""
append sys.path with PACKAGES if package exists and not already
in the sys.path
"""
for package in PACKAGES:
package = os.path.join(PYTHON, package)
if os.path.exists(package) is True:
if package not in sys.path:
print "loading package '{0}'".format(package)
sys.path.append(package)
else:
print "package '{0}' already in sys.path".format(package)
else:
message = "Package '{0}' does not exist.".format(package)
raise IOError(message)
"""
import IMPORTS if the package is in sys.path
"""
for item in IMPORTS:
pkg_in_path = [pkg == os.path.basename(path)
for pkg in PACKAGES for path in sys.path]
if any(pkg_in_path):
print "loading '{0}'".format(item)
importlib.import_module(item)
else:
raise AttributeError("{0} not in sys.path".format(item))
ipython results:
$ ipython
# result
Python 2.7.6 (default, Mar 22 2014, 22:57:26)
Type "copyright", "credits" or "license" for more information.
IPython 2.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
Warning: disable autoreload in ipython_config.py to improve performance.
In [3]: run tests/test_dummy.py
loading package '/home/dmmmd/development/my_python/browser'
loading package '/home/dmmmd/development/my_python/display'
loading package '/home/dmmmd/development/my_python/singleton'
loading package '/home/dmmmd/development/my_python/test_a'
loading package '/home/dmmmd/development/my_python/hello_world'
loading 'browser'
loading 'display'
loading 'singleton'
loading 'test_a'
loading 'hello_world.hello'
Hello, world!
results from python tests/test_dummy.py
:
loading package '/home/dmmmd/development/my_python/browser'
loading package '/home/dmmmd/development/my_python/display'
loading package '/home/dmmmd/development/my_python/singleton'
loading package '/home/dmmmd/development/my_python/test_a'
loading package '/home/dmmmd/development/my_python/hello_world'
loading 'browser'
loading 'display'
loading 'singleton'
loading 'test_a'
Traceback (most recent call last):
File "tests/test_dummy.py", line 46, in <module>
importlib.import_module(item)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named test_a
Importing the package 'test_a' in the python interpreter does not throw an exception:
Python 2.7.6 (default, Mar 22 2014, 22:57:26)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/home/dmmmd/development/my_python/test_a')
>>> import test_a
>>> print test_a
<module 'test_a' from 'test_a/__init__.pyc'>
>>>