Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simple answer: You work out the absolute path based on the environment.</p> <p>What you really need is a few pointers. There are various bits of runtime and environment information that you can glean from various places in the standard library (and they certainly help me when I want to deploy an application on windows).</p> <p>So, first some general things:</p> <ol> <li><code>os.path</code> - standard library module with lots of cross-platform path manipulation. Your best friend. "Follow the os.path" I once read in a book.</li> <li><code>__file__</code> - The location of the current module.</li> <li><code>sys.executable</code> - The location of the running Python.</li> </ol> <p>Now you can fairly much glean anything you want from these three sources. The functions from os.path will help you get around the tree:</p> <ul> <li><code>os.path.join('path1', 'path2')</code> - join path segments in a cross-platform way</li> <li><code>os.path.expanduser('a_path')</code> - find the path <code>a_path</code> in the user's home directory</li> <li><code>os.path.abspath('a_path')</code> - convert a relative path to an absolute path</li> <li><code>os.path.dirname('a_path')</code> - get the directory that a path is in</li> <li>many many more...</li> </ul> <p>So combining this, for example:</p> <pre><code># script1.py # Get the path to the script2.py in the same directory import os this_script_path = os.path.abspath(__file__) this_dir_path = os.path.dirname(this_script_path) script2_path = os.path.join(this_dir_path, 'script2.py') print script2_path </code></pre> <p>And running it:</p> <pre><code>ali@work:~/tmp$ python script1.py /home/ali/tmp/script2.py </code></pre> <p>Now for your specific case, it seems you are slightly confused between the concept of a "working directory" and the "directory that a script is in". These can be the same, but they can also be different. For example the "working directory" can be changed, and so functions that use it might be able to find what they are looking for sometimes but not others. <code>subprocess.Popen</code> is an example of this.</p> <p>If you always pass paths absolutely, you will never get into working directory issues.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload