I have a small python module with some scripts that I run in a virtual environment. My Makefile looks like this:
default: build
.DEFAULT:
./setup.py $@
build:
./setup.py build
/path/to/venv/bin/activate:
virtualenv --system-site-packages /path/to/venv
install: /path/to/venv/bin/activate
/path/to/venv/bin/pip install .
Now if I have a fresh install and run make install everything works great, and at the top of all of my scripts in $VENV/bin I have the following shebang:
#!/path/to/venv/bin/python
but if I first run make build and then make install then I get the system python in the shebang:
#!/usr/bin/python
I guess I would have expected the setup process to check and replace the shebang to the virtual environment's python.
make build makes no reference to the virtualenv, so it's probably making a build of the project using the system python environment, probably creating the scripts at this step.
Then by invoking make install, you're invoking pip on the current directory. My guess is that the setup.py build is causing the scripts to be generated and shebangs set then. Still, it's not obvious to me why build artifacts in the build directory would affect a pip install.
I did try to replicate your findings, but when I used Python 3 and venv, the issue did not occur.
I think I'm going to need you to dig in a bit more and put together a description that I can replicate. Can you create a Github repository that experiences this issue?
The setup is very simple. Here is an example package:
~/foo/foo.py
#!/usr/bin/env python
print("Hello world!")
~/foo/setup.py
#!/usr/bin/env python
from setuptools import setup
setup(name='foo', version='1.0', scripts=['foo.py'])
Then, to reproduce the issue, create a virtual environment (but don't activate it):
$ virtualenv ~/myvenv
And then run:
$ cd foo
$ python setup.py build
$ ~/myvenv/bin/pip install .
I think you are right that the build step uses the system python environment. Not sure if this is really a bug or not, but it caught me off guard.
And if you skip the last python setup.py build step, the script gets built with the expected shebang?
Yep.
I think we had the same issue. A developer had run python setup.py build and took us a while to understand what was going on.
Maybe an alternative here would be if we could define somewhere in setup.py that it should always use a fresh build directory.
I ran into this with Python 3.7, venv, and pip 19.1.1., while installing a local package into a virtual environment. Steps to reproduce:
python3.7 -m venv cat cat and run python setup.py build./build directory; its script shebangs pointing to the cat environmentcat, then create another called dogdog and run pip install .dog, but its internal shebangs point to cat
Most helpful comment
I ran into this with Python 3.7, venv, and pip 19.1.1., while installing a local package into a virtual environment. Steps to reproduce:
python3.7 -m venv catcatand runpython setup.py build./builddirectory; its script shebangs pointing to thecatenvironmentcat, then create another calleddogdogand runpip install .dog, but its internal shebangs point tocat