w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
script that adds packages to sys.path import as expected when run from ipython but throw exception when the script is run from python

I found a solution although the solution does not solve the problem of why certain package paths added in an ad hoc fashion to sys.path are not imported when the script is run with python test_dummy.py.

I originally ran into this problem while trying to use py.test. In the py.test documentation I saw this tip about "managing your project with virutalenv, pip, and editable mode." I ignored it because I thought it was too advanced for my level of Python knowledge.

I decided to try the tip after reading about "creating your own python project."

In the pwd I created a setup.py file with the following code:

from setuptools import setup, find_packages

setup(name='import_troubleshooting', version='1.0')
packages = find_packages(exclude=[
    'my_django',
    'fake*',
    'tests*'
])

I then executed the following code at the command line:

pip install -e .  # the pip way (which just calls "setup.py
develop")

I then executed the following code with the expected results:

$ python tests/test_dummy.py
# output    
package '/home/dmmmd/development/my_python/browser' already in sys.path
package '/home/dmmmd/development/my_python/display' already in sys.path
package '/home/dmmmd/development/my_python/singleton' already in sys.path
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!

None of this explains to me why I couldn't add the package paths in an ad hoc fashion without first installing my packages with pip install -e .. It is strange, too, that the failure is only when passing the script to python. ipython test_dummy.py works without installing my local packages first.

I am pleased, however, to have learned something about python installation and packaging. The test_dummy.py script now runs as expected when called from python tests/test_dummy.py.

I am now confused as to why three of the packages are already in sys.path when the test_dummy.py script is run:

package '/home/dmmmd/development/my_python/browser' already in
sys.path
package '/home/dmmmd/development/my_python/display' already in sys.path
package '/home/dmmmd/development/my_python/singleton' already in sys.path

I don't recall doing anything that would add those to sys.path but it is possible since I have been doing various tutorials. They are not in sys.path in any other environment where I have not run the setup.py that I created.

My confusion is a symptom of the complexity of Python, not a cause!

Thank you for everybody's input.

NB: This is the output of python test_dummy.py after a fresh virtualenv:

$ pip install -e .
# install output unremarkable
$ python tests/test_dummy.py
# output
    loading package
'/home/dmmmd/development/my_python/automated_browsing/browser'
loading package
'/home/dmmmd/development/my_python/automated_browsing/display'
loading package
'/home/dmmmd/development/my_python/automated_browsing/singleton'
loading package
'/home/dmmmd/development/my_python/automated_browsing/hello_world'
loading package
'/home/dmmmd/development/my_python/automated_browsing/test_a'
loading 'browser'
loading 'display'
loading 'singleton'
loading 'hello_world.hello'
Hello, world!
loading 'test_a'




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.