在当前代码中使用另一个文件夹中的 class
Using a class from another folder in the current code
对于一个看似简单的问题,我深表歉意,我是在 Python.
中使用 classes 的新手
我正在使用 Pycharm
,我的文件夹结构如下所示:
文件夹 constant-contact-python-wrapper
在 __init.py__
和 restful_lib.py
下定义了一些 classes(我从 github
获得了这个库)。我想在 ConstantContact
文件夹中包含的文件 Trial.py
中使用这些 classes。我正在使用以下代码,但无法导入 class.
import sys
sys.path.append('C:\Users\psinghal\PycharmProjects\ConstantContact\constant-contact-python-wrapper')
import constant-contact-python-wrapper
API_KEY = "KEY" #not a valid key
mConnection = CTCTConnection(API_KEY, "joe", "password123")
有人能给我指出正确的方向吗?
constant-contact-python-wrapper
和 ConstantContact
是 python 的不相关包。在与 manage.py
相同的目录中创建一个 __init__.py
,它应该按预期工作。
您试图纠正的部分问题是您有两个库在同一范围内,尽管看起来它们不一定需要如此。
最简单的解决方案是将 ConstantContact 文件夹中的 constant-contact-python-wrapper 简单地放在一个新文件夹下,用于存放您将要导入的代码,而这些代码不是您自己编写的。这样您的项目就可以针对此实例以及您导入来自另一个库的代码的未来实例进行组织
理想的文件夹结构是:
ConstantContact
|___ ConstantContact
|____ExternalLibraries #(or some name similar if you plan on using different libraries)
|___constant-contact-python-wrapper
使用上述模型,您现在拥有一个组织良好的层次结构,可以非常轻松地容纳导入的代码。
为了便于导入,您还需要设置以下内容:
1.Create init.py 外部库中的文件。内容如下:
from constant-contact-python-wrapper import #The class or function you want to use
这将有助于导入,并且可以针对您选择使用的未来库进行扩展。
然后您可以在 ConstantContact 文件夹中编写的代码中使用导入语句:
from ExternalLibraries import #The class or function you chose above
如果您有多个 类 想要导入,您可以在导入语句中用逗号分隔它们。例如:
from Example import foo,bar,baz
由于 ExternalLibraries 中的 init.py 文件是直接导入所有 functions/classes,您现在甚至不必使用点语法就可以使用它们(即。 library.func).
来源和进一步阅读:
"all 并导入 *" Can someone explain __all__ in Python?
"Python Project Skeleton" http://learnpythonthehardway.org/book/ex46.html
"Modules" http://docs.python-guide.org/en/latest/writing/structure/#modules
对于一个看似简单的问题,我深表歉意,我是在 Python.
中使用 classes 的新手我正在使用 Pycharm
,我的文件夹结构如下所示:
文件夹 constant-contact-python-wrapper
在 __init.py__
和 restful_lib.py
下定义了一些 classes(我从 github
获得了这个库)。我想在 ConstantContact
文件夹中包含的文件 Trial.py
中使用这些 classes。我正在使用以下代码,但无法导入 class.
import sys
sys.path.append('C:\Users\psinghal\PycharmProjects\ConstantContact\constant-contact-python-wrapper')
import constant-contact-python-wrapper
API_KEY = "KEY" #not a valid key
mConnection = CTCTConnection(API_KEY, "joe", "password123")
有人能给我指出正确的方向吗?
constant-contact-python-wrapper
和 ConstantContact
是 python 的不相关包。在与 manage.py
相同的目录中创建一个 __init__.py
,它应该按预期工作。
您试图纠正的部分问题是您有两个库在同一范围内,尽管看起来它们不一定需要如此。
最简单的解决方案是将 ConstantContact 文件夹中的 constant-contact-python-wrapper 简单地放在一个新文件夹下,用于存放您将要导入的代码,而这些代码不是您自己编写的。这样您的项目就可以针对此实例以及您导入来自另一个库的代码的未来实例进行组织
理想的文件夹结构是:
ConstantContact
|___ ConstantContact
|____ExternalLibraries #(or some name similar if you plan on using different libraries)
|___constant-contact-python-wrapper
使用上述模型,您现在拥有一个组织良好的层次结构,可以非常轻松地容纳导入的代码。
为了便于导入,您还需要设置以下内容:
1.Create init.py 外部库中的文件。内容如下:
from constant-contact-python-wrapper import #The class or function you want to use
这将有助于导入,并且可以针对您选择使用的未来库进行扩展。
然后您可以在 ConstantContact 文件夹中编写的代码中使用导入语句:
from ExternalLibraries import #The class or function you chose above
如果您有多个 类 想要导入,您可以在导入语句中用逗号分隔它们。例如:
from Example import foo,bar,baz
由于 ExternalLibraries 中的 init.py 文件是直接导入所有 functions/classes,您现在甚至不必使用点语法就可以使用它们(即。 library.func).
来源和进一步阅读:
"all 并导入 *" Can someone explain __all__ in Python?
"Python Project Skeleton" http://learnpythonthehardway.org/book/ex46.html
"Modules" http://docs.python-guide.org/en/latest/writing/structure/#modules