title: NameError: __file__ is not defined
body: <p>I'm trying to build a relative path. When running the following line in my .py script</p>
  
  <pre><code>this_path = os.path.abspath(os.path.dirname(__file__))
  </code></pre>
  
  <p>I get a name error. It used to work just a few days ago when I set it up but now it doesn't any longer. I am not executing this in my shell, just running my script as usually. </p>
  
  <pre><code> os.path.dirname(sys.argv[0]) 
  </code></pre>
  
  <p>only returns an empty string. When I use <code>"__file__"</code> 
  instead, it returns the home directory but not the path to my file. I thought <code>__file__</code> is set to the filepath as soon as I load a module in my script. Anyone know where I'm going wrong?</p>
  
  <p>More info: my .py script is in a folder in "C:/Users/corr/Documents/Uni/Thesis/Code/" I want to construct a relative path so I can access the data files in folder "C:/Users/corr/Documents/Uni/Thesis/Data/". When I set up the script, the following lines worked:</p>
  
  <pre><code>this_path = os.path.abspath(os.path.dirname(__file__))
  path = os.path.join(this_path, "../Data")
  </code></pre>
  
  <p>Now though, when I run the first line I get a NameError: <code>__file__</code> not defined. When I run the same line with <code>"__file__"</code> instead, it returns "C:\Users\corr". Also, I am executing my script in the Spyder environment, as this is where I usually work.</p>