IPython与Spyder的cd命令 [英] cd command in IPython vs. Spyder

查看:752
本文介绍了IPython与Spyder的cd命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道许多在IPython中工作的工作目录提示也可以在Spyder中工作,只要它们以%开头。

I know that many of the working directory prompts that work in IPython also work in Spyder as long as they're prefaced by %.

例如, pwd ls 在IPython中工作,但要在Spyder中运行相同的命令,他们需要以%开头,例如:%pwd %ls

For example, pwd and ls work in IPython, but to run the same commands in Spyder they need to be prefaced with a % such as: %pwd and %ls.

要更改IPython中的目录,我可以运行cd命令,如: cd C:\ Users \ ...但是,这似乎在Spyder中不起作用,即使以%开头。有什么建议?

To change the directory in IPython, I can run the cd command like: cd C:\Users\... HOWEVER, this doesn't seem to work in Spyder, even when prefaced with a %. Any suggestions?

我知道 os.chdir('C:\\Users\\')有效,只是想了解为什么%cd C:\ Users \ 不...

I know that os.chdir ('C:\\Users\\') works, just trying to understand why %cd C:\Users\ doesn't...

推荐答案

运行%foo 时,该命令在作为自己的进程运行的新shell实例中运行。当shell退出时,对其状态(例如其工作目录)的更改将随之丢失;它们不影响产生它的父Python进程。

When you run %foo, that command is run in a new shell instance running as its own process. When that shell exits, changes to its state (such as its working directory) are lost with it; they don't effect the parent Python process that spawned it.

这与运行 sh -c'cd /'的方式相同不会更改shell中的当前工作目录。 (实际上,以 sh -c$ some_command运行新进程正是标准C库调用 system(some_command),及其Python等价物 os.system(some_command)有效。)

This is the same as how running sh -c 'cd /' doesn't change your current working directory in shell. (Indeed, running a new process as sh -c "$some_command" is exactly how the standard-C library call system(some_command), and its Python equivalent os.system(some_command) works).

这篇关于IPython与Spyder的cd命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆