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

PR: Show conda and pyenv environments in Python interpreter (Preferences) #13950

Merged
merged 21 commits into from
Nov 3, 2020

Conversation

steff456
Copy link
Member

@steff456 steff456 commented Oct 8, 2020

Description of Changes

  • Wrote at least one-line docstrings (for any new functions)
  • Added unit test(s) covering the changes (if testable)
  • Included a screenshot or animation (if affecting the UI, see Licecap)

Add two miscellaneous methods that are able to detect conda and pyenv environments. This is the first PR for enhancing Spyder's environment handling.

image

image

Issue(s) Resolved

Fixes #13903
Fixes #12200

Affirmation

By submitting this Pull Request or typing my (user)name below,
I affirm the Developer Certificate of Origin
with respect to all commits and content included in this PR,
and understand I am releasing the same under Spyder's MIT (Expat) license.

I certify the above statement is true and correct: Steff456

@steff456 steff456 added this to the v4.2.0 milestone Oct 8, 2020
@steff456 steff456 self-assigned this Oct 8, 2020
@steff456 steff456 changed the title Add methods for detecting conda and pyenv environments PR: Add methods for detecting conda and pyenv environments Oct 8, 2020
@steff456
Copy link
Member Author

steff456 commented Oct 9, 2020

/show binder

@github-actions
Copy link

github-actions bot commented Oct 9, 2020

Binder 👈 Launch a Binder instance on this branch

@steff456
Copy link
Member Author

As discussed previously, we are going to move the selection of environments in preferences, and the user will be redirected to the preferences if they click on the current environment

image

image

@steff456
Copy link
Member Author

Now the combobox in the preferences with other interpreter has all the environments found in the device.

image

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @steff456! This looks pretty good!

I only left than some minor suggestions and comments for you.

spyder/utils/conda.py Outdated Show resolved Hide resolved
spyder/utils/misc.py Outdated Show resolved Hide resolved
spyder/utils/misc.py Outdated Show resolved Hide resolved
spyder/widgets/status.py Outdated Show resolved Hide resolved
spyder/widgets/status.py Show resolved Hide resolved
@pep8speaks
Copy link

pep8speaks commented Oct 30, 2020

Hello @steff456! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-11-02 22:55:17 UTC

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @steff456 for your hard work on this one! I think it's almost ready.

spyder/utils/conda.py Outdated Show resolved Hide resolved
spyder/utils/misc.py Outdated Show resolved Hide resolved
spyder/widgets/tests/test_status.py Show resolved Hide resolved
spyder/utils/conda.py Outdated Show resolved Hide resolved
Comment on lines 344 to 348
def get_list_pyenv_envs():
"""Return the list of all pyenv envs found in the system."""
try:
out, err = subprocess.Popen(
['pyenv', 'versions', '--bare', '--skip-aliases'],
Copy link
Contributor

@mrclary mrclary Oct 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steff456 , this will not work for Spyder.app for the same reason as above in the conda case. To my knowledge, on macOS pyenv will only ever be installed at /usr/local/bin

Suggested change
def get_list_pyenv_envs():
"""Return the list of all pyenv envs found in the system."""
try:
out, err = subprocess.Popen(
['pyenv', 'versions', '--bare', '--skip-aliases'],
def get_list_pyenv_envs():
"""Return the list of all pyenv envs found in the system."""
pyenv = 'pyenv'
if running_in_mac_app():
old_path = os.environ['PATH']
os.environ['PATH'] = os.pathsep.join([old_path, '/usr/local/bin'])
pyenv_path = find_program('pyenv')
os.environ['PATH'] = old_path # restore PATH
if pyenv_path:
pyenv = pyenv_path
try:
out, err = subprocess.Popen(
[pyenv, 'versions', '--bare', '--skip-aliases'],

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steff456 @ccordoba12, I wonder if we could simplify this by instead enhancing is_program_installed (called by find_program). What if is_program_installed checked that certain paths were already in PATH and temporarily added them if not present, based on platform. This could do 2 things

  1. Other developers could use find_program without having to consider the macOS application case.
  2. Even outside of the macOS application, when searching for a program on the system, we don't have to assume that user profiles have been sourced (e.g. ~/.bash_Profile, etc)

What do you think?

spyder.utils.programs.py

def is_program_installed(basename):
    """
    Return program absolute path if installed in PATH.

    Otherwise, return None

    On macOS systems, a .app is considered installed if
    it exists.
    """
    if (sys.platform == 'darwin' and basename.endswith('.app') and
            osp.exists(basename)):
        return basename

    home = get_home_dir()
    req_paths = []
    if sys.platform == 'darwin':
        req_paths.extend([
            osp.join('/usr', 'local', 'bin'),
            osp.join(home, 'opt', 'anaconda3', 'condabin'),
            osp.join(home, 'opt', 'miniconda3', 'condabin'),
            osp.join('/opt', 'anaconda3', 'condabin'),
            osp.join('/opt', 'miniconda3', 'condabin')
        ])
    elif sys.platform.startswith('linux'):
        pass  # Set required linux paths
    elif os.name == 'nt':
        pass  # Set required Windows paths

    old_path = os.environ['PATH']
    for path in req_paths:
        if path not in old_path:
            os.environ['PATH'] += os.pathsep + path

    abspath = None
    for path in os.environ["PATH"].split(os.pathsep):
        abspath = osp.join(path, basename)
        if osp.isfile(abspath):
            break
    os.environ['PATH'] = old_path

    return abspath

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a great idea! In this way we can guarantee this type of tooling to work inside the macOS application. @ccordoba12 do you think we need to add this in a different PR or in this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave this for another PR. But I agree it's a really good idea.

Copy link
Member

@ccordoba12 ccordoba12 Nov 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it'd be great if you could give us a hand with that @mrclary.

Copy link
Member

@ccordoba12 ccordoba12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @steff456 for all your hard work on this one! Great improvement for 4.2!

@ccordoba12 ccordoba12 changed the title PR: Add methods for detecting conda and pyenv environments PR: Show conda and pyenv environments in Main interpreter entry (Preferences) Nov 3, 2020
@ccordoba12 ccordoba12 merged commit 0dfbe89 into spyder-ide:4.x Nov 3, 2020
ccordoba12 added a commit that referenced this pull request Nov 3, 2020
@ccordoba12 ccordoba12 changed the title PR: Show conda and pyenv environments in Main interpreter entry (Preferences) PR: Show conda and pyenv environments in Main interpreter (Preferences) Nov 3, 2020
@ccordoba12 ccordoba12 changed the title PR: Show conda and pyenv environments in Main interpreter (Preferences) PR: Show conda and pyenv environments in Python interpreter (Preferences) Nov 3, 2020
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.

4 participants