3

If I understand correctly, When typing sys.argv[0] it shows the name of the .py file one is working on. But when I do this, it shows me a different name:

sys.argv[0]
'C:\\Anaconda\\lib\\site-packages\\spyderlib\\widgets\\externalshell\\start_ipython_kernel.py' 

The real name of the file is 'Ra x 13 arima.py' and its located in 'C:\Users\infantjo\Documents\Python Scripts\Projects\Work in Progress'

The current directory is:

os.getcwd()
'C:\\Users\\infantjo\\Documents\\Python Scripts\\Projects'

What am I doing wrong?

(Im using Python 2.7.11 in Spyder 2.3.8 installed with Anaconda 2.3.0, 64bit).

Gabriel
  • 3,737
  • 11
  • 30
  • 48
  • What do you mean by *'working on'*? Do you mean running? `sys.argv[0]` gives the name of the script which was run as an entry point to the running process. See [the docs](https://docs.python.org/2/library/sys.html#sys.argv) – Peter Wood Jun 15 '16 at 20:18
  • sorry, yes I mean the code I'm running. – Gabriel Jun 15 '16 at 20:19
  • `getcwd()` is the working directory. That's a completely different thing from the directory your code is in. Why do you expect them to be the same? – Charles Duffy Jun 15 '16 at 20:21
  • Are you in an interactive prompt? `ipython` is Interactive Python. The script to start the prompt process is the one you quote above. It looks as though you aren't running your script. – Peter Wood Jun 15 '16 at 20:22
  • *nod*. If your question is where the current module's source came from, that's a **completely** different question. – Charles Duffy Jun 15 '16 at 20:22
  • How You run the code? – Tomasz Jakub Rup Jun 15 '16 at 20:23
  • I'm running the code in Spyder. – Gabriel Jun 15 '16 at 20:25
  • 2
    Looks like `ipython`/`spyder` is wrapping your script. You could fall back to `__file__`, but it gives you the filename of the current module. Each py file will give you its name, so you would need to only reference your main module's `__file__`. – rrauenza Jun 15 '16 at 20:31
  • thanks @rrauenza but I didn't understand your comment – Gabriel Jun 15 '16 at 20:52
  • 1
    http://stackoverflow.com/questions/9271464/what-does-the-file-wildcard-mean-do/9271617 – rrauenza Jun 15 '16 at 20:53

1 Answers1

2

argv contain a list of command line arguments passed to python script. argv[0] is the script name. doc

IDE like Spyder / PyCharm doesn't run the script direct. It's run the wrapper that run Your script.

In this case Spyder run the start_ipython_kernel.py script that get a Your script name as a parameter and run it.

If You need a script name use a __file__ var.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49