New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Imports, ImportError and sys.path #1207

Closed
mixman opened this Issue Oct 8, 2014 · 6 comments

Comments

Projects
None yet
4 participants
@mixman

mixman commented Oct 8, 2014

I ran into an ImportError due different PYTHONPATH's, decided to look into the matter, and wanted your input on expectations -- should the last case work with Fabric, or is it user responsibility to mend paths? Example code in https://github.com/mixman/fabpath (checkout; 'bash maybe.bash') and output:

fabfile.py

from fabric.api import task

@task
def hello():
    from project.idea import Implementation

bash maybe.bash

No ImportError due 'python -m' adding the current directory (='') to sys.path:
python -m fabric hello

Done.

No ImportError because we add current path to sys.path using PYTHONPATH
PYTHONPATH= fab hello

Done.

No ImportError, because directory *of script being run* is automatically added to sys.path
python hello.py

ImportError follows during *task execution* because current directory is not in sys.path.
The current directory is not in sys.path, as 'fab' is the script being run, and its path is there.
The import would work outside the function. Only affects runtime imports.
fab hello
Traceback (most recent call last):
  File "/opt/fabpath/venv/lib/python2.7/site-packages/fabric/main.py", line 743, in main
    *args, **kwargs
  File "/opt/fabpath/venv/lib/python2.7/site-packages/fabric/tasks.py", line 424, in execute
    results['<local-only>'] = task.run(*args, **new_kwargs)
  File "/opt/fabpath/venv/lib/python2.7/site-packages/fabric/tasks.py", line 174, in run
    return self.wrapped(*args, **kwargs)
  File "/opt/fabpath/fabfile.py", line 5, in hello
    from project.idea import Implementation
ImportError: No module named project.idea

References: #1004

mixman pushed a commit to futurice/fabric-deployment-helper that referenced this issue Oct 8, 2014

@bitprophet

This comment has been minimized.

Show comment
Hide comment
@bitprophet

bitprophet Oct 8, 2014

Member

This and #1004 sound like recurrences of #256 to me.

As per #256 (comment) I think this behavior is misguided and expect it to change in 2.0, but I'm also not sure it's worth changing before 2.0 as it feels like breaking backwards compatibility (not strongly, but it's still changing previous, and somewhat documented, behavior.)

Member

bitprophet commented Oct 8, 2014

This and #1004 sound like recurrences of #256 to me.

As per #256 (comment) I think this behavior is misguided and expect it to change in 2.0, but I'm also not sure it's worth changing before 2.0 as it feels like breaking backwards compatibility (not strongly, but it's still changing previous, and somewhat documented, behavior.)

@bitprophet bitprophet added this to the 2.0 milestone Oct 8, 2014

@bitprophet bitprophet added the Core label Oct 8, 2014

@deronnax

This comment has been minimized.

Show comment
Hide comment
@deronnax

deronnax Jan 12, 2015

I ran into it, and that's very annoying. When is supposed to come version 2 ?

deronnax commented Jan 12, 2015

I ran into it, and that's very annoying. When is supposed to come version 2 ?

@bitprophet

This comment has been minimized.

Show comment
Hide comment
@bitprophet

bitprophet May 11, 2015

Member

At present the Invoke loader (which will load fabfiles in Fabric 2) does add the parent directory of the tasks file to sys.path and does not remove it afterwards, so we should be good once 2.0 hits the streets. (It is in development right now.)

Furthermore, the loading system is set up to be more replaceable/swappable, so it will be possible to have different loading "behaviors" if users need it.

Member

bitprophet commented May 11, 2015

At present the Invoke loader (which will load fabfiles in Fabric 2) does add the parent directory of the tasks file to sys.path and does not remove it afterwards, so we should be good once 2.0 hits the streets. (It is in development right now.)

Furthermore, the loading system is set up to be more replaceable/swappable, so it will be possible to have different loading "behaviors" if users need it.

@bitprophet bitprophet closed this May 11, 2015

@morgante

This comment has been minimized.

Show comment
Hide comment
@morgante

morgante Sep 21, 2015

Is there even a temporary workaround for this?

As of now, it's making it impossible to work with non-core packages and Fabric.

morgante commented Sep 21, 2015

Is there even a temporary workaround for this?

As of now, it's making it impossible to work with non-core packages and Fabric.

@bitprophet

This comment has been minimized.

Show comment
Hide comment
@bitprophet

bitprophet Sep 22, 2015

Member

That seems pretty unlikely, @morgante, as myself and many others routinely import non-stdlib packages into fabfile code on recent versions of Fab. There might be something specific in your environment that's exacerbating things though :(

Member

bitprophet commented Sep 22, 2015

That seems pretty unlikely, @morgante, as myself and many others routinely import non-stdlib packages into fabfile code on recent versions of Fab. There might be something specific in your environment that's exacerbating things though :(

@morgante

This comment has been minimized.

Show comment
Hide comment
@morgante

morgante Sep 22, 2015

@bitprophet But are those packages globally installed or inside a virtualenv?

I did find a work around though, by adding this to my fabfile:

import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), ".venv/lib/python3.4/site-packages"))

morgante commented Sep 22, 2015

@bitprophet But are those packages globally installed or inside a virtualenv?

I did find a work around though, by adding this to my fabfile:

import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), ".venv/lib/python3.4/site-packages"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment