Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon Lisp: Launch subprocess with different working directory than lisp process
    primarykey
    data
    text
    <p>Suppose I have a directory A, and subdirectory B. I cd into A and launch lisp. In that lisp process, I would like to launch a Python subprocess where Python sees B as its current working directory. The lisp process needs to have cwd in A, and the python process should have cwd in B. How do I do this in a cross-platform, simple way? </p> <p>I'm looking for a solution that works with CCL and SBCL (probably using 'run-program function), and works for Windows, Linux, and OS X. </p> <p>I looked at the CCL run-program documentation, and I didn't see a way to change the cwd of the launched process.</p> <p>I looked at Python command-line arguments, and I didn't see one that would change the cwd of the python process.</p> <p>I thought about a run-program call for 'cd B; python ...', but I'm not sure how that would work, since it's really running two programs; cd, and then python.</p> <p>The Python code is being provided as input (as a file), so I cannot change any of that code (by adding an os.chdir() call or similar).</p> <p>A python wrapper file that launches the python input file as a subprocess isn't ideal, because I'm sending stdin and listening to stdout of the python process launched by lisp. Adding another subprocess in between lisp and the python process that evals the input file means I'd need to do a lot of stout/stdin relaying, and I have a feeling that this would be brittle.</p> <p>krzysz00's approach worked very well. Since the directory change is handled in lisp, before the python process is launched, this approach will work for launching other processes in different subdirectories (not just python).</p> <p>For documentation, here's my code using krzsz00's approach that worked for SBCL &amp; CCL. Note that it uses Hoyte's defmacro! macro, from <a href="http://letoverlambda.com" rel="nofollow">Let Over Lambda</a>, to easily avoid unwanted variable capture:</p> <pre><code>#+:SBCL (defun cwd (dir) (sb-posix:chdir dir)) (defun getcwd () #+SBCL (sb-unix:posix-getcwd) #+CCL (current-directory)) (defmacro! with-cwd (dir &amp;body body) `(let ((,g!cwd (getcwd))) (unwind-protect (progn (cwd ,dir) ,@body) (cwd ,g!cwd)))) </code></pre> <p>Usage:</p> <pre><code>(with-cwd "./B" (run-program ...)) </code></pre>
    singulars
    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. 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