0

I've got a Python project which uses a virtual environment. In my project folder, there is a bin/ folder which contains all the executables needed by the project. To use these, you run source bin/activate to insert the path to that bin/ directory to your PATH variable.

Now, most of the time this works as I'd expect. I run the source command and when I run which python, it shows the one I'd expect from the virtual environment. Sometimes however, it doesn't list that one. It lists a different one on my PATH. Here's an example of what I see:

We can see we are in the project directory:

$ pwd
/Users/dalemy/Projects/my_project

We can also see that the python and python3 binaries (actually symlinks) are in the bin/ directory:

$ ls -al /Users/dalemy/Projects/my_project/bin/python*
lrwxr-xr-x  1 dalemy  staff    7 10 Feb 15:16 /Users/dalemy/Projects/my_project/bin/python -> python3
lrwxr-xr-x  1 dalemy  staff   44 10 Feb 15:16 /Users/dalemy/Projects/my_project/bin/python3 -> /usr/local/Cellar/python/3.7.2_1/bin/python3

Here we can see that this is the first entry in the PATH variable:

$ echo $PATH
/Users/dalemy/Projects/my_project/bin:/Users/dalemy/.poetry/bin:/usr/local/opt/python3/bin:/usr/local/opt/ruby/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/opt/X11/bin:/Users/dalemy/Config/scripts:/Users/dalemy/.poetry/bin:/Users/dalemy/.local/bin

But when we ask about where Python is located, it doesn't find it:

$ which python
/usr/local/bin/python

$ which python3
/usr/local/opt/python3/bin/python3

Running type -a shows the same thing:

$ type -a python
python is /usr/local/bin/python
python is /usr/bin/python
python is /usr/local/bin/python
python is /usr/bin/python

I've got no idea why this is happening. The bin/activate command runs hash -r and I've run it manually to be sure that it's clearing out anything cached. If I remove every single entry on my PATH except for the first one, I just get that it can't be found.

Once it starts doing this, the only solution I've found is to remove the virtual environment completely and recreate it.

What could possibly be going wrong here?

  • Does /usr/local/Cellar/python/3.7.2_1/bin/python3 exist? If you recently updated your Homebrew stuff it would be /usr/local/Cellar/python/3.7.3/bin/python3. – nohillside May 19 at 13:28
  • They do. It finds the Homebrew stuff just fine. But I don't want it to find that. I want it to find the binaries at the start of the PATH. – Velox May 19 at 13:51
  • What does running type -a python and type -a python3 outputs? – Nimesh Neema May 19 at 13:57
  • 1
    The only way I found to reproduce what you are seeing is if the symlink points to a non-existing file. Can you please run ls -l /usr/local/Cellar/python/3.7.2_1/bin/python3 (and subsequently for all further symlinks) to verify that all files and links actually exist? – nohillside May 19 at 14:17
  • 1
    @nohillside You were totally correct. It was a different version that existed at that path, not an identical one. As you said, it seems that a Homebrew upgrade caused the version to change, and therefore the path, breaking the symlinks. If you want to write this as an answer, I'll happily accept! – Velox May 19 at 14:32
1

The only way I found to reproduce what you are seeing is if the symlink points to a non-existing file. So best to use ls -l to verify that all the symlink targets actually exist, and fix the links if necessary.

More generally speaking: Unless you really need a specific version of python3 it might be better to just run

cd /Users/dalemy/Projects/my_project/bin
ln -s /usr/local/bin/python3 python3

or just rely on PATH.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .