Repository: spyder-ide/spyder

Issue:
Spyder executable file opened on launch
## Problem Description

When I launch spyder from the command line, the spyder window launches with the previous state as is expected, then also opens the spyder executable file, in my case `~/anaconda3/bin/spyder`.


### What steps reproduce the problem?

1. type `spyder` at the command line, hit enter


### What is the expected output? What do you see instead?

this file not to be opened at launch



## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.0.0b6 (was also happening with beta5)
* Python version: Python 3.7.0 64-bit 
* Qt version: Qt 5.9.6
* PyQt version: PyQt5 5.9.2
* Operating System name/version: Darwin 19.0.0 


### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
cloudpickle >=0.5.0          :  1.2.2 (OK)
pygments >=2.0               :  2.4.2 (OK)
qtconsole >=4.5.5            :  4.5.5 (OK)
nbconvert >=4.0              :  5.6.0 (OK)
sphinx >=0.6.6               :  2.2.0 (OK)
pylint >=0.25                :  2.4.2 (OK)
psutil >=0.3                 :  5.6.3 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
zmq >=17                     :  18.1.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
numpydoc >=0.6.0             :  0.9.1 (OK)
spyder_kernels >=1.5.0;<2.0.0:  1.5.0 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
atomicwrites >=1.2.0         :  1.3.0 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
watchdog                     :  None (OK)
keyring                      :  None (OK)
pexpect >=4.4.0              :  4.7.0 (OK)
pympler                      :  None (OK)
sympy >=0.7.3                :  1.4 (OK)
cython >=0.21                :  0.29.13 (OK)
IPython >=4.0                :  7.8.0 (OK)
matplotlib >=2.0.0           :  3.1.1 (OK)
pandas >=0.13.1              :  0.25.1 (OK)
numpy >=1.7                  :  1.17.2 (OK)
scipy >=0.17.0               :  1.3.1 (OK)
pyls >=0.28.2;<0.29.0        :  0.28.3 (OK)
rtree >=0.8.3                :  0.8.3 (OK)
```



Discussion:
> then also opens the spyder executable file

I don't understand what you mean by this.
When I have spyder open with a few files open

<img width="637" alt="Screen Shot 2019-10-17 at 2 11 40 pm" src="https://user-images.githubusercontent.com/14357794/66974952-8ff83c80-f0e8-11e9-9408-dc00a4bf23d2.png">

I quit spyder, then relaunch by typing `spyder` at the terminal

<img width="577" alt="Screen Shot 2019-10-17 at 2 12 44 pm" src="https://user-images.githubusercontent.com/14357794/66974965-a4d4d000-f0e8-11e9-8b63-365d5453ff7e.png">

All my previous files are opened in tabs as expected, plus the spyder executable

<img width="820" alt="Screen Shot 2019-10-17 at 2 13 02 pm" src="https://user-images.githubusercontent.com/14357794/66974990-b4ecaf80-f0e8-11e9-8566-4ab567ee1c88.png">




Looks like another ugly issue with macOS Catalina.

@goanpeca, @steff456, are you seeing this behavior?
I only updated to Catalina yesterday and I was getting this problem before then.
I've also been seeing it on my linux machine

Is `zsh` your default shell on Linux too?
nope, zsh is not my terminal on mac either. That message is just a new thing saying the default have changed.
Then I really don't know what's happening here, sorry.
I mean, it seems you're using a special script to launch Spyder (because i haven't seen the file that's opening after every restart), so this looks related to your setup and not to Spyder itself.
Ok, I updated all my machines to beta7 and tested on them all.
Can't reproduce on linux. I may be misremembering that, I switch machines a lot.
On my laptop, running macOS mojave, I get the same behaviour.

if I do `which spyder` it points be to the `spyder` file in the anaconda3/bin which is the executable I was talking about. I'm  not using any special script, just what is installed by conda. The behaviour I'm seeing is consistent with the file being called as the executable, but also given as an argument (standard "open file" behaviour) suggesting that during the argument parsing, the spyder executable is not being removed as an argument.

Possibly something to do with the hashbang? I don't really understand calling bash then pythonw, maybe that does something weird on mac?

Is that re.sub command supposed to be removing the executable from the arguments? if that is failling that would explain what's happening. Is the script named something else on mac that doesn't meet the regex anymore?



I downgraded to spyder3 and tested. Same launch script in the anaconda bin, but it doesn't get opened as a file.
Reinstalled beta7 and it's getting opened again.
I assume that script is a standard template installed by conda, so that probably isn't the issue.
I inserted some print statements into that script and sys.argv returns
`['Users/shannon/anaconda3/bin/spyder']`

So my assumption here is that something about the newer betas (starting from either 4 or 5), seemingly on mac only, is causing `sys.argv[0]` to be opened as a file on launch.

@impact27, I think this can be caused by your PR #10067. Could you install beta7 with conda in a conda env and check if you get the same behavior?
@impact27, or maybe your PR #10049 is the cause of this issue.
Looks like apple sends a file open event with `sys.argv[0]`. I'll see how to filter this out.
Thanks @impact27!

Resolving patch:
diff --git a/spyder/utils/qthelpers.py b/spyder/utils/qthelpers.py
--- a/spyder/utils/qthelpers.py
+++ b/spyder/utils/qthelpers.py
@@ -65,7 +65,11 @@ def __init__(self, *args):
     def event(self, event):
         if event.type() == QEvent.FileOpen:
             fname = str(event.file())
-            if self._has_started:
+            if sys.argv and sys.argv[0] == fname:
+                # Ignore requests to open own script
+                # Later, mainwindow.initialize() will set sys.argv[0] to ''
+                pass
+            elif self._has_started:
                 self.sig_open_external_file.emit(fname)
             elif MAC_APP_NAME not in fname:
                 self._pending_file_open.append(fname)


Test patch: