Python 在 Netbeans 中引用外部模块

发布于 2024-08-22 01:13:22 字数 481 浏览 1 评论 0原文

我正在使用 Netbeans 进行 Python 开发,我有许多项目(其中有许多模块)。我主要想知道的是,如何将这些模块之一导入到新项目中?我尝试在netbeans中编辑python路径,但没有成功。这是我的设置:

Netbeans projects
=================
ProjectA
   ModuleA
       ClassA.py (Assume a class called TestClass exists in this file)
   ModuleB
       ...
ProjectB
   ...
ProjectC
   ...

现在我想做的是在“新项目”中执行以下操作:

from ProjectA.ModuleA.ClassA import TestClass

我是否必须将每个项目的 src 文件夹添加到 pythonpath 中?我 已经尝试过,但我仍然得到“没有名为...的模块”

I'm working with a Netbeans for Python development, I have a number of projects (which have a number of modules). What I basically want to know is, how do I import one of these modules into a new project? I have tried editing the python path in netbeans, but to no avail. Here's my setup:

Netbeans projects
=================
ProjectA
   ModuleA
       ClassA.py (Assume a class called TestClass exists in this file)
   ModuleB
       ...
ProjectB
   ...
ProjectC
   ...

Now what I want to do is in a "new project" is the following:

from ProjectA.ModuleA.ClassA import TestClass

Do I have to add the src folders for each of the projects to the pythonpath? I
have tried this but still I get "No Module named ..."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-08-29 01:13:22

Hadji,您可能想放弃 Netbeans 的 Python 开发默认结构。

首先,Python代码文件(.py)是一个模块。一个包包含多个模块。

您应该做的是像下面这样构造您的文件:

Netbeans projects
=================
PackageA
   __init__.py (This file is crucial for Python to recognise the folder as a package.)
   ClassA.py (Assume a class called TestClass exists in this file)
   ...
PackageB
   ...
PackageC
   ...

然后,在您的 ProjectC (现在基本上是一个文件夹)中,您可以执行

from PackageA.ClassA import TestClass

同样的操作,请记住 Python 模块是 .py 文件。您也不需要 src 文件夹,并且所有模块都应该位于适当的包内。 =]

更多参考:

Python 项目的文件系统结构

Hadji, you may want to discard Netbeans' default structures for Python development.

First of all, Python code file (.py) is a module. A package contains a number of modules.

What you should do is structure your files like the following:

Netbeans projects
=================
PackageA
   __init__.py (This file is crucial for Python to recognise the folder as a package.)
   ClassA.py (Assume a class called TestClass exists in this file)
   ...
PackageB
   ...
PackageC
   ...

Then, in your ProjectC (which is now basically a folder), you can do

from PackageA.ClassA import TestClass

Again, please remember a Python module is a .py file. You also don't need the src folder, and all of your modules should be inside appropriate packages. =]

More references:

Filesystem structure of a Python project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文