如何 - 导入不在当前目录中的代码 [英] How to - import code not in current directory

查看:61
本文介绍了如何 - 导入不在当前目录中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想测试/调试的python脚本。它包含一个类

,它扩展自其他一些类,它位于另一个目录中的其他

python文件中。


例如:


[要测试的脚本]

c:\ python_code\foo.py


[需要的python文件]

c:\ some \other \ directory> \\ bar.py


bar.py ...但是为了测试foo(在python shell中)我通常将
复制bar.py放到同一个目录中foo.py ...但这很痛苦。

有没有办法让python了解

bar.py所在的目录,就像系统一样变量等?如果是这样,我怎么设置

那个?


谢谢。

解决方案

这个问题似乎经常出现在这个新闻组中,所以通过过去的线程查看

肯定会提供更多细节。


这里来自Re:如何从任意路径导入模块?

由Simon Brunning于2005年5月26日09:20发布到comp.lang.python:


"

我有一个程序,它将从一些任意定义的路径动态加载组件。怎么做?


您可以使用os.walk和fnmatch找到它们。然后你可以暂时将
目录添加到sys.path,然后使用__import __()导入。

"


so这应该适用于你的情况:


import sys

sys.path.append(" C:\ some \\\ other\directory")

进口酒吧


Claudio

" py" <共******* @ gmail.com> schrieb im Newsbeitrag

新闻:11 ********************** @ g14g2000cwa.googlegr psps.com ...我有一个蟒蛇我想测试/调试的脚本。它包含一个类
,它扩展自另一个类,它位于不同目录中的其他一些python文件中。

例如:

[测试脚本]
c:\ python_code \ foo.py

[所需的python文件]
c:\ some \other \ directory \ bar .py

...所以我想测试/调试foo.py,它需要bar.py. foo.py导入
bar.py ...但是为了测试foo(在python shell中)我通常将bar.py复制到与foo.py相同的目录中...但是这个很痛苦。
有什么方法可以让python了解
bar.py所在的目录,比如系统变量等等?如果是这样,我该如何设置呢

谢谢。



这个问题似乎出现在这个新闻组经常出现,所以通过过去的线程查看

肯定会提供更多详细信息。


这里来自Re:如何从任意中导入模块路径?>

由Simon Brunning于2005年5月26日09:20发布到comp.lang.python:


"

我有一个程序,它将从一些任意定义的路径动态加载组件。怎么做?


您可以使用os.walk和fnmatch找到它们。然后你可以暂时将
目录添加到sys.path,然后使用__import __()导入。

"


so这应该适用于你的情况:


import sys

sys.path.append(" C:\ some \\\ other\directory")

进口酒吧


Claudio

" py" <共******* @ gmail.com> schrieb im Newsbeitrag

新闻:11 ********************** @ g14g2000cwa.googlegr psps.com ...我有一个蟒蛇我想测试/调试的脚本。它包含一个类
,它扩展自另一个类,它位于不同目录中的其他一些python文件中。

例如:

[测试脚本]
c:\ python_code \ foo.py

[所需的python文件]
c:\ some \other \ directory \ bar .py

...所以我想测试/调试foo.py,它需要bar.py. foo.py导入
bar.py ...但是为了测试foo(在python shell中)我通常将bar.py复制到与foo.py相同的目录中...但是这个很痛苦。
有什么方法可以让python了解
bar.py所在的目录,比如系统变量等等?如果是这样,我该如何设置呢?

谢谢。



Claudio Grondi写道:

所以这应该适用于你的情况:

import sys
sys.path.append(" C:\ some \\\ other\directory")
导入栏




....这肯定会奏效。唯一的问题是每次我启动时都会在python shell中启动
foo.py我必须重新输入这三行....类似

为什么我希望一个环境变量或路径设置我可以坚持下去。


现在这样做......


I have a python script that I want to test/debug. It contains a class
which extends from some other class which is located in some other
python file in a different directory.

For example:

[script to test]
c:\python_code\foo.py

[needed python files]
c:\some\other\directory\bar.py

....so I want to test/debug foo.py, which needs bar.py. foo.py imports
bar.py ...but in order to test out foo (in the python shell) I normally
copy bar.py into the same directory as foo.py...but this is painful.
Is there some way that I can have python know about the directory where
bar.py is located, like a system variable, etc? If so, how do I set
that up?

Thanks.

解决方案

This question seems to come up in this newsgroup quite often, so looking
through past threads will sure provide more details.

Here from "Re: how to import a module from a arbitraty path?"
posted to comp.lang.python by Simon Brunning on May 26, 2005 09:20 :

"

I have a program which is going to dynamicly load components from some
arbitrary defined paths. How to do that?
You can locate them with os.walk and fnmatch. Then you can temporarily
add the directory to sys.path, and import using __import__().
"

so this should work in your case:

import sys
sys.path.append("C:\some\other\directory")
import bar

Claudio
"py" <co*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.com... I have a python script that I want to test/debug. It contains a class
which extends from some other class which is located in some other
python file in a different directory.

For example:

[script to test]
c:\python_code\foo.py

[needed python files]
c:\some\other\directory\bar.py

...so I want to test/debug foo.py, which needs bar.py. foo.py imports
bar.py ...but in order to test out foo (in the python shell) I normally
copy bar.py into the same directory as foo.py...but this is painful.
Is there some way that I can have python know about the directory where
bar.py is located, like a system variable, etc? If so, how do I set
that up?

Thanks.



This question seems to come up in this newsgroup quite often, so looking
through past threads will sure provide more details.

Here from "Re: how to import a module from a arbitraty path?"
posted to comp.lang.python by Simon Brunning on May 26, 2005 09:20 :

"

I have a program which is going to dynamicly load components from some
arbitrary defined paths. How to do that?
You can locate them with os.walk and fnmatch. Then you can temporarily
add the directory to sys.path, and import using __import__().
"

so this should work in your case:

import sys
sys.path.append("C:\some\other\directory")
import bar

Claudio
"py" <co*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@g14g2000cwa.googlegr oups.com... I have a python script that I want to test/debug. It contains a class
which extends from some other class which is located in some other
python file in a different directory.

For example:

[script to test]
c:\python_code\foo.py

[needed python files]
c:\some\other\directory\bar.py

...so I want to test/debug foo.py, which needs bar.py. foo.py imports
bar.py ...but in order to test out foo (in the python shell) I normally
copy bar.py into the same directory as foo.py...but this is painful.
Is there some way that I can have python know about the directory where
bar.py is located, like a system variable, etc? If so, how do I set
that up?

Thanks.



Claudio Grondi wrote:

so this should work in your case:

import sys
sys.path.append("C:\some\other\directory")
import bar



....that will certainly work. Only issue is that each time I start up
foo.py in the python shell I have to retype those three lines....kind
of why I was hoping for a environment variable or path setting that i
could stick in.

This will do for now...


这篇关于如何 - 导入不在当前目录中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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