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

Doesn't load correct python on Mac OS X #1

Closed
kartikkumar opened this issue Dec 17, 2014 · 20 comments
Closed

Doesn't load correct python on Mac OS X #1

kartikkumar opened this issue Dec 17, 2014 · 20 comments

Comments

@kartikkumar
Copy link

Hi there,

I was hoping this might be the easy answer to my problems of deal with virtualenvs within ST3. I followed all the instructions and it doesn't seem to work on Mac OS. When I try to activate a virtualenv from the command palette, it correctly lists the environments. There don't seem to be any issues with running the activate command; however even when I choose the Python + Virtualenv build system specifically, it doens't seem to run python within the activated environment.

Any suggestions as to how to diagnose the issues? Would be great to get this working.

Thanks!

@AdrianLC
Copy link
Owner

Hi,

I don't have a Mac so it hasn't been tested very well with that OS.

If you save your project (command Project: Save as) and then activate a virtualenv with the plugin, the file path to the current virtualenv should show up in your *.sublime-project file. E.g: "virtualenv": "/home/user/.virtualenvs/myvenv".

You might also want to enable debug logging. You can do so from sublime's console executing this: import logging; logging.basicConfig(level='DEBUG') . Then try again and some messages will be written to the console. Here is what I'm getting:

DEBUG:Virtualenv.hooks:Running postactivate hooks.
/home/adrian/.virtualenvs/venv
DEBUG:Virtualenv.hooks:Hook execution finished.
INFO:Virtualenv.commands:Current virtualenv set to "/home/adrian/.virtualenvs/venv".
INFO:Virtualenv.commands:Command executed with virtualenv "/home/adrian/.virtualenvs/venv".
Running python -u "/home/adrian/script.py"

Hope that helps.

@AdrianLC AdrianLC reopened this Dec 19, 2014
@kartikkumar
Copy link
Author

Thanks for the feedback. So I tried a few different things. Firstly, I created a completely new environment to make sure I wasn't messing things up somehow. I've installed numpy via pip and I just wrote a simple script that imports numpy. When I switch to working on the test environment through the Terminal, it runs fine.

In Sublime Text, I created a new project and activated the virtualenv. As a result, my project file looks as follows:

{
    "virtualenv": "/Users/kartikkumar/.virtualenvs/test"
}

I then did what you suggested and imported logging from the console, and when I run the script using the Python + Virtualenv build system, this is what appears:

>>> import logging; logging.basicConfig(level='DEBUG')
INFO:Virtualenv.commands:Command executed with virtualenv "/Users/kartikkumar/.virtualenvs/test".
Running python -u "/Users/kartikkumar/Desktop/test_project/test.py"

The build itself generates the following error:

Traceback (most recent call last):
  File "/Users/kartikkumar/Desktop/test_project/test.py", line 1, in <module>
    import numpy as np
  File "/usr/local/lib/python2.7/site-packages/numpy/__init__.py", line 170, in <module>
  File "/usr/local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
  File "/usr/local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
  File "/usr/local/lib/python2.7/site-packages/numpy/core/__init__.py", line 6, in <module>
ImportError: cannot import name multiarray
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/kartikkumar/Desktop/test_project/test.py"]
[dir: /Users/kartikkumar/Desktop/test_project]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

So I'm not sure what's going on. Looks like things are configured, but when running the shell_cmd, it still seems to be reverting to the system Python (which in my case is my brew Python).

Any idea what's going on?

PS: As you can see, I didn't get the debug messages that you did either. Not sure why though.

Thanks!

@AdrianLC
Copy link
Owner

Try this in your script, and see what you get:

import sys

print(sys.executable, sys.version, sys.path)

Update: Sorry, forgot the most important (the path).

@kartikkumar
Copy link
Author

So from ST3, this is the output:

('/usr/local/opt/python/bin/python2.7', '2.7.9 (default, Dec 10 2014, 23:46:09) \n[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]', ['/Users/kartikkumar/Desktop/test_project', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-7.0-py2.7.egg', '/usr/local/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg', '/usr/local/lib/python2.7/site-packages/setuptools-7.0-py2.7.egg', '/Users/kartikkumar/Desktop/test_project', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages'])

Same output from the Terminal:

('/Users/kartikkumar/.virtualenvs/test/bin/python', '2.7.9 (default, Dec 10 2014, 23:46:09) \n[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]', ['/Users/kartikkumar/Desktop/test_project', '/usr/local/lib/python', '/Users/kartikkumar/Desktop/test_project', '/Users/kartikkumar/.virtualenvs/test/lib/python27.zip', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/plat-darwin', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/plat-mac', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/lib-tk', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/lib-old', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/lib-dynload', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/kartikkumar/.virtualenvs/test/lib/python2.7/site-packages'])

As you can see, the Terminal picks up the right Python, whereas ST3 isn't. And I can confirm that the right environment is still activated in ST3, since the project file includes the virtualenv parameter pointing to the right path.

Hoping it's something small, as this would be an awesome plugin to have functioning on my Mac.

Thanks!

@filipeximenes
Copy link

I'm experiencing the same problems. I'm running ST3 in Mac OS 10.9, and also using homebrew python as the default.

@filipeximenes
Copy link

So... played a bit with it with no success by now.
I always launch sublime from terminal, and I've tried installing the Fix Mac Path plugin.
The path is being correctly prepared in VirtualenvExecCommand run method, but it seems to be ignored by sublime witch keeps using homebrew python /usr/local/opt/python/bin/python2.7 to execute the script instead of the virtualenv one.

@kartikkumar
Copy link
Author

Ah yes, I also launch ST3 from the terminal.

@mcfoton
Copy link

mcfoton commented Mar 13, 2015

I have same issue — wrong python and no modules.
After selecting virtualenv "(venv) ACTIVATED" pops up for several seconds. When I try to run a build error pops up

"from selenium import webdriver ImportError: No module named selenium"

though it's installed in venv. Python shows different version too.
Here's path from error if it helps:

[path: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

@ghost
Copy link

ghost commented Mar 30, 2015

Creates a new ~/.virtualenv just fine and can even be activated from the shell, but build system doesn't seem to reflect the updated sys.path in sublime.

@mayuroks
Copy link

+1 Same with me

Terminal picks correct python virtualenv

$ python -m sys -c 'print(sys.executable)' 
/Users/mrokade/.virtualenvs/gplaces/bin/python

ST3 doesnt. When I run print(sys.executable) from the script, this interpreter is picked up

/usr/bin/python

@OmeGak
Copy link

OmeGak commented Aug 22, 2015

+1

@dashdrum
Copy link

Same issue. Let me know if I can help test.

PS - works great on Linux!!

@snaily
Copy link
Contributor

snaily commented Oct 29, 2015

I believe this is due to how Sublime runs subprocesses on OS X. From exec.py in the Default.sublime-package:

        if shell_cmd and sys.platform == "win32":
            # Use shell=True on Windows, so shell_cmd is passed through with the correct escaping
            self.proc = subprocess.Popen(shell_cmd, stdout=subprocess.PIPE,
                stderr=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, shell=True)
        elif shell_cmd and sys.platform == "darwin":
            # Use a login shell on OSX, otherwise the users expected env vars won't be setup
            self.proc = subprocess.Popen(["/bin/bash", "-l", "-c", shell_cmd], stdout=subprocess.PIPE,
                stderr=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, shell=False)
        elif shell_cmd and sys.platform == "linux":
            # Explicitly use /bin/bash on Linux, to keep Linux and OSX as
            # similar as possible. A login shell is explicitly not used for
            # linux, as it's not required
            self.proc = subprocess.Popen(["/bin/bash", "-c", shell_cmd], stdout=subprocess.PIPE,
                stderr=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, shell=False)

The rationale for using a login shell presumably being that since OS X has multiple sources of PATH, inside a coder's tool like Sublime it should use whatever PATH you have in your .bash_profile. An unfortunate side effect is that it'll clobber whatever supplied PATH the build system passes along with the exec request.

I'll put together a pull request.

snaily pushed a commit to snaily/sublime-text-virtualenv that referenced this issue Oct 30, 2015
Sublime's normal exec routine runs commands in a login shell to ensure the
PATH from .bash_profile et al are respected. In this case, we explicitly do
want the supplied path rather than the shell path, so this patch rewrites
the kwargs to the exe routine to avoid being run in a login shell.
snaily added a commit to snaily/sublime-text-virtualenv that referenced this issue Oct 30, 2015
Sublime's normal exec routine runs commands in a login shell to ensure the
PATH from .bash_profile et al are respected. In this case, we explicitly do
want the supplied path rather than the shell path, so this patch rewrites
the kwargs to the exe routine to avoid being run in a login shell.
@AdrianLC
Copy link
Owner

Someone else can test this fix?

@kartikkumar
Copy link
Author

I'll try to test it out tonight.

@kartikkumar
Copy link
Author

Excellent! Seems to be working on my system (El Capitan). Installed the package via Package Control and then overwrote the commands.py file with the one provided by @snaily and now when I activate a virtualenv from within ST3, I can access the libraries installed via pip. I double-checked by running some test code with the virtualenv activated and deactivated and it behaves as required.

So this seems to be resolved!

AdrianLC added a commit that referenced this issue Jan 8, 2016
Rewrite exec command on OS X. Fixes #1.
@AdrianLC
Copy link
Owner

AdrianLC commented Jan 8, 2016

I just merged the PR and released a hotfix update.
Thank you @snaily and @kartikkumar for the fix and feedback!

@kartikkumar
Copy link
Author

Great! 👍

@snaily
Copy link
Contributor

snaily commented Jan 8, 2016

Woohoo!
On 8 Jan 2016 21:25, "Kartik Kumar" notifications@github.com wrote:

Great! [image: 👍]


Reply to this email directly or view it on GitHub
#1 (comment)
.

@xiaohk
Copy link

xiaohk commented Jul 5, 2016

I have tried the latest version on Mac OS, but I still cannot build my project properly using virtualenv.
The python is using is the system global one, not the one in the virtualenv.

When I do virtualenv:activate, I didn't get anything and the building still does not work.

Any suggestions?

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

No branches or pull requests

9 participants