Alexander-Miller / treemacs

GNU General Public License v3.0
2.11k stars 155 forks source link

Python3 detection failed on windows #411

Closed zijianyue closed 5 years ago

zijianyue commented 5 years ago

There is no python3 executable file on windows python folder, only python3.dll, so treemacs-only-during-init will fail. image

Alexander-Miller commented 5 years ago

Does the following evaluate to t?

(-when-let (py-binary (executable-find "python"))
  (->> (concat py-binary " --version")
       (shell-command-to-string)
       (s-trim)
       (s-replace "Python " "")
       (version<= "3")))
zijianyue commented 5 years ago

nil

Alexander-Miller commented 5 years ago

Then it seems like the detection is correct and you are not using python3 as default. If that's the case then I do not know what I can do.

@duianto You're on windows, right? Do you know how to find and get a hold of a python 3 binary on windows?

duianto commented 5 years ago

Yes I'm on Windows 10 (but also have access to Ubuntu 18.04.02 in Virtualbox).

The official python windows 64 installer: https://www.python.org/ftp/python/3.7.3/python-3.7.3-amd64.exe from this page: https://www.python.org/downloads/release/python-373/ suggests the default installation path as: C:\Users\username\AppData\Local\Programs\Python\Python37

There's also the Chocolatey package manager: https://chocolatey.org/ where the latest 3.x version of python can be installed: https://chocolatey.org/packages/python/3.7.3 (after installing chocolatey: https://chocolatey.org/install) from a administrator command prompt, with the command: choco install python It installs to the directory: c:/python37/

I've installed it with chocolatey.

The cmd command: where python lists:

C:\Users\username\Applications\emacs-26.1-x86_64\bin\python.exe
C:\Python37\python.exe

Spacemacs reports: (executable-find "python")

"c:/Users/username/Applications/emacs-26.1-x86_64/bin/python.exe"

(shell-command "python --version")

Python 2.7.14

(executable-find "c:/python37/python")

"c:/python37/python.exe"

(shell-command "c:/python37/python --version")

Python 3.7.3

zijianyue commented 5 years ago

Now I add python3 in PATH, and eval your expression return t.But I still get the error:

[Treemacs] Python3 not found, advanced git-mode and directory flattening features will be disabled.

The python version on mode-line image BTW, the git-mode and directory flattening features are still enabled by my config in init.el.

Alexander-Miller commented 5 years ago

The cmd command: where python lists:

Great, that should be enough to reliably find it.

But I still get the error:

It's not an error, but a warning (to turn it off see the second-to-last FAQ entry). All it means is that those 2 features will be off by default. You can still enable them if you set treemacs-python-executable to a python 3 path. In fact directory flattening should work even with python 2, it's non-simple git-mode that has a hard python 3 requirement.

Alexander-Miller commented 5 years ago

@zijianyue What does the following output?

(when (eq system-type 'windows-nt)
      (->> "where python"
           (shell-command-to-string)
           (s-trim)
           (s-lines)
           (--first
            (->> (concat it " --version")
                 (shell-command-to-string)
                 (s-trim)
                 (s-replace "Python " "")
                 (version<= "3")))))
zijianyue commented 5 years ago

"g:\Python3\python.exe"

duianto commented 5 years ago

That outputs the path with double back slashes (not sure if it's needed but it's some additional info):

"C:\\Python37\\python.exe"

System Info :computer:

Alexander-Miller commented 5 years ago

"C:\\Python37\\python.exe"

Does git-mode still work if you assign that string to treemacs-python-executable?

duianto commented 5 years ago

Yes, modifying a file or restoring it (undo) and saving it, then refreshing gr the Treemacs window, changes the color of the modified/restored file.

I hadn't tried git-mode before, is that test enough or is there a more thorough test I should preform?

The above steps work for these three values:

    (setq treemacs-python-executable nil)
    (setq treemacs-python-executable "C:\\Python37\\python.exe")
    (setq treemacs-python-executable "C:/Python37/python.exe")

Single backslashes are removed from the variables value:

(setq treemacs-python-executable "C:\Python37\python.exe")

treemacs-python-executable is a variable defined in ‘treemacs-customization.el’. Its value is "C:Python37python.exe" Original value was nil

gr (in Treemacs) closes the git repositories folder, and the messages buffer shows:

apply: Spawning child process: Invalid argument

The Emacs shell-command (S-M-1 or M-!) and the Windows command prompt (cmd.exe), (I'm guessing that shell-command calls cmd.exe) seems to accept all four types of slashes:

C:\\Python37\\python.exe --version
C:\Python37\python.exe --version
C:/Python37/python.exe --version
C://Python37//python.exe --version

Return:

Python 3.7.3

Alexander-Miller commented 5 years ago

We are getting close. How about this, does that output a value treemacs can work with?

(--if-let (executable-find "python3") it
    (when (eq system-type 'windows-nt)
      (->> "where python"
           (shell-command-to-string)
           (s-trim)
           (s-lines)
           (--first
            (->> (concat it " --version")
                 (shell-command-to-string)
                 (s-trim)
                 (s-replace "Python " "")
                 (s-replace "\\\\" "/")
                 (s-replace "\\" "/")
                 (version<= "3"))))))
duianto commented 5 years ago

"C:\\Python37\\python.exe"

Alexander-Miller commented 5 years ago

So much for those backslash replacements. Oh well, I misread anyway. I was worried about this part:

apply: Spawning child process: Invalid argument

but that's due to single backslashes basically escaping whatever's in front of them.

I'll drop a patch in a bit.

csevast commented 5 years ago

I have the same issue. I use Windows 10, and Python is installed in a specific path, on which the treemacs-python-executable is set with backslashes. Of course last version of Treemacs is installed.

Alexander-Miller commented 5 years ago

Is that actually a problem?

When @duianto tried it with "C:\\Python37\\python.exe" it seemed to work for him:

"C:\\Python37\\python.exe"

Does git-mode still work if you assign that string to treemacs-python-executable?

Yes, modifying a file or restoring it (undo) and saving it, then refreshing gr the Treemacs window, changes the color of the modified/restored file.

duianto commented 5 years ago

It seems to still be working for me. I'm not seeing any messages about python when opening Treemacs. Calling (treemacs--find-python3) from the Eval: prompt (M-:) returns: "C:\\Python37\\python.exe"

System Info :computer:

csevast commented 5 years ago

The evaluation of (treemacs--find-python3) returns: version-to-list: Invalid version syntax: ‘The filename, directory name, or volume label syntax is incorrect.’ (must start with a number)

I have to mention that I tried to set the path on treemacs-python-executable variable to both of my Python installations, Anaconda and python.org. When I apply the corresponding environment treatment for Anaconda treemacs--find-python3 function returns nil.

duianto commented 5 years ago

You said:

Python is installed in a specific path

Maybe the OS isn't aware of that location and you need to add it to the PATH environment variable.

What's the output when you call the following from the command prompt cmd.exe?

where python

For me it says:

C:\Users\username>where python C:\Python27\python.exe C:\Users\username\Applications\emacs-26.2-x86_64\bin\python.exe C:\Python37\python.exe

Note about powershell

The paths can also be found from windows powershell, but it requires that .exe is added to where:

where.exe python

PS C:\Users\username> where.exe python C:\Python27\python.exe C:\Users\username\Applications\emacs-26.2-x86_64\bin\python.exe C:\Python37\python.exe

source: https://coad.ca/2017/10/25/finding-which-executable-is-run-in-windows-powershell-with-where-exe/

csevast commented 5 years ago

I have a lot of Python installations and none of their virtual environments is included by default in my system's and user's PATH environment variable. Thus, I add Python paths ad hoc and the execution of where python in CMD it's pointless.

Any way, starting Emacs with a Python directory path in the PATH variable, checked in Emacs by executing (getenv "PATH"), checked that the Python directory path is included in the Emacs exec-path list, the treemacs-python-executable variable is set to the Python executable path, the execution of where python in Emacs returns the expected result, but the execution of (treemacs--find-python3) returns nil.

Of course Python is set correctly in my Emacs for python-mode, elpy and org-mode, as long as a lot of other executables for other packages, and I can't figure out what is going wrong with treemacs.

Alexander-Miller commented 5 years ago

Treemacs needs python 3, so I go out of my way to find it, and not python 2. That inlcudes dealing with windows-specific annoyances like multiple python installation directories. The code is right here:

(->> "where python"
           (shell-command-to-string)
           (s-trim)
           (s-lines)
           (--first
            (when (file-exists-p it)
              (->> (concat (shell-quote-argument it) " --version")
                   (shell-command-to-string)
                   (s-trim)
                   (s-replace "Python " "")
                   (s-left 1)
                   (version<= "3")))))

I pushed a patch today that'll make it grab the right version string when using Anaconda, maybe that will solve your issue. If not I will need your help debugging this because I don't a windows PC to test this on my own.

csevast commented 5 years ago

What is the expected return value of the code you sent?

Alexander-Miller commented 5 years ago

A viable path to a python 3 executable.

csevast commented 5 years ago

The treemacs--find-python3 returns the proper path to the Python 3 executable -I changed the code directly in the treemacs-customization.el file- but the problem remains. I don't understand why the python executable has to found by the the execution of where command as long as it is set in the treemacs-python-executable custom variable. When this variable has been set to a proper Python 3 path, no need for extension of PATH environment variable and exec-path Emacs variable should be expected. But even I have done both of the extensions, and even the treemacs-python-executable return the proper path, Treemacs returns "Python3 not found, advanced git-mode and directory flattening features will be disabled.". Maybe the bug is somewhere else in the code.

csevast commented 5 years ago

I changed in treemacs-async.el file the following

treemacs-only-during-init
 (let ((has-git    (not (null (executable-find "git"))))
       (has-python (not (null (treemacs--find-python3)))))
       ;; (has-python (not (null (executable-find "python3")))))

The recognition of Python 3 is fixed but the PATH environment variable and exec-path Emacs variable have to be extended to the already set path of the Python 3 directory, something that should not be expected when the treemacs-python-executable variable is set to the proper Python 3 executable.

csevast commented 5 years ago

Also, what is the expected treemacs behavior when Python 3 is not found? In my case the treemac-git commands are not available but the treemacs-collapse are.

Alexander-Miller commented 5 years ago

I don't understand why the python executable has to found by the the execution of where command as long as it is set in the treemacs-python-executable custom variable.

Because treemacs tries finding it on its own so users are not required to to it themselves. Sane defaults and all that - ideally you shouldn't have to know that treemacs runs on python internally.

I changed in treemacs-async.el file the following

The find-python function shouldn't have been called there anyway, just a check of the set value will do. I pushed a patch to fix that now.

Also, what is the expected treemacs behavior when Python 3 is not found?

No directory flattening, No git-mode except for the dumb and slow one.

something that should not be expected when the treemacs-python-executable variable is set to the proper Python 3 executable.

If you are using the customize interface then the most recent fix should keep the warning from showing.

Compro-Prasad commented 5 years ago

@Alexander-Miller Are you planning to move away from Python in Treemacs? If you do so then there would be no issues about detecting Python.

csevast commented 5 years ago

I downloaded the new treemacs-async.el file and everything looks to work fine. Thank you.

Alexander-Miller commented 5 years ago

everything looks to work fine.

Great!

Are you planning to move away from Python in Treemacs? If you do so then there would be no issues about detecting Python.

No plans. The only alternative that isn't another language like python is async.el and that's slower to start and awkward to use since the new emacs process starts with no libraries loaded, so there's no dash or pfuture (and loading libraries is also slow in emacs).