Skip to content
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

be more careful with PYTHONPATH when invoking flask #161

Closed
wants to merge 1 commit into from

Conversation

mwhudson
Copy link

The Ubuntu package of Flask-Migrate failed in a rather strange way because
the PYTHONPATH that setuptools sets when running the python 2.7 tests via
"python2.7 setup.py test" was causing the /usr/bin/flask to look in various
2.7-specific locations for modules and failing. This patch overrides the
PYTHONPATH setuptools sets to just '..'.

@miguelgrinberg
Copy link
Owner

I don't think it is a good idea to override PYTHONPATH. Basically this change prevents the caller to ever set a custom path. Sounds like you are patching this side because it is easier, when the problem is somewhere else.

Why is the PYTHONPATH wrong in this specific case?

@mwhudson
Copy link
Author

I agree this is a hack.

The problem is a total mess though. Here's what's happening in excruciating detail:

  1. /usr/bin/flask is a python3 program (it starts #!/usr/bin/python3)
  2. as part of building the flask-migrate source package, it runs "python2.7 setup.py test"
  3. setuptools (only since pypa/setuptools@5e4eea7, so actually fairly recently, I didn't realise the behaviour was new) puts the location of all required distributions onto PYTHONPATH while running the tests. In the context of a package build, this is /usr/lib/python2.7/dist-packages
  4. when your tests invoke /usr/bin/flask, this means that even though it is a python 3 program, it looks for things in /usr/lib/python2.7/dist-packages before looking in /usr/lib/python3/dist-packages.
  5. this actually works surprisingly well, but is obviously not guaranteed to (and importing jinja2 when /usr/bin/python3 is 3.6 is a particular case where this fails).

Basically everything is horrible (I realise now that my patch probably means that the flask-migrate code is not really tested under python 2.7 at all in these test cases now -- maybe running sys.executable -m flask instead of just 'flask' would work better...

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Jun 30, 2017

Why is the flask tool configured to run on Python3? When I install Flask on a Python 2.7 virtualenv I get the shebang line set to 2.7.

@mwhudson
Copy link
Author

On a given system, the flask binary on the global PATH is going to be one or the other. If it was 2.7 we'd just have the problem when running the Python 3 tests.

@mwhudson
Copy link
Author

I've pushed what looks like a better fix to me (insanely repetitive, but the idea is clear and it works). Out of time for this week now, sorry.

The Ubuntu package of Flask-Migrate failed in a rather strange way because the
PYTHONPATH that setuptools sets when running the python 2.7 tests via
"python2.7 setup.py test" was causing the /usr/bin/flask to look in various
2.7-specific locations for modules and failing. This runs sys.executable -m
flask and not the global flask, which means flask for the correct version
of python is tested even when not operating in a virtualenv.
@mwhudson
Copy link
Author

mwhudson commented Jan 2, 2018

I was just reminded of this PR and I've just pushed a new version. Any updates from your side?

@miguelgrinberg
Copy link
Owner

miguelgrinberg commented Jan 2, 2018

As I mentioned before, overriding the PYTHONPATH is guaranteed to affect users that need it set in a specific way. Packages are not supposed to rely on this variable, this is for end users to set. As I said, the problem is not in this package, if Ubuntu used virtual environments to run unit tests, or allowed you to set the PYTHONPATH before running the tests, then we wouldn't be talking about this.

@miguelgrinberg
Copy link
Owner

This issue will be automatically closed due to being inactive for more than six months. Please reopen if you need more assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants