从 pydev 中的另一个项目导入

发布于 2024-12-13 02:35:14 字数 548 浏览 2 评论 0原文

我已经四处寻找很长一段时间了,但我就是找不到答案。类似的问题涉及第三方库等的一些棘手案例,但我的情况似乎很简单。尽管如此,我还是不明白这是如何工作的。

我正在使用 Eclipse 3.5.2、Pydev 2.2.0,在 Ubuntu 11.04 机器上运行。

我的情况看起来很简单:我想编写一个包作为自己的项目,然后我想在另一个项目中使用这个包编写一个应用程序。

我应该如何设置 pydev 以便我可以拥有这两个项目“核心”(包)和“应用程序”(使用核心包),并从“应用程序”中执行以下操作:

import core

  • 我已将核心添加到应用程序的项目参考中,但它不起作用 任何事物。
  • 我已将 /core/ 添加到源文件夹(我必须强制这样做,因为它不是 显示在列表中),它不起作用(所以我将其删除)。
  • 我已将 /full/path/to/core 添加到外部库,但它仍然不起作用。

我所说的“不起作用”,是指我总是得到“未解决的导入:核心”。

有想法吗?

I've been looking around for quite some time but I just can't find the answer. Similar questions relate to some tricky cases with third party libraries and all, but my situation seems to be textbook-simple. Nevertheless I can't figure out how this works.

I'm using Eclipse 3.5.2, Pydev 2.2.0, running on an Ubuntu 11.04 machine.

My case seems simple: I want to write a package as its own project, and then I want to write an application using this package in another project.

How should I setup pydev so that I can have this two projects "core" (the package) and "app" (which uses the core package), and from within "app" do:

import core

?

  • I have added core to the Project References of app but it doesn't do
    anything.
  • I have added /core/ to the Source folders (I had to force for that because is was not
    showing in the list), it didn't work (so I removed it).
  • I have added /full/path/to/core to the External Libraries, it still doesn't work.

By "doesn't work", I mean that I get an "unresolved import: core", always.

Ideas?

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

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

发布评论

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

评论(3

素手挽清风 2024-12-20 02:35:14

如果您有 2 个项目的 PYTHONPATH 正确设置(通过源文件夹),那么只需添加项目引用就应该可以工作。

也许一个例子可以帮助...

例如:

project1
project1/src <-- this should be set as the source folder
project1/src/core
project1/src/core/__init__.py

project2
project2/src <-- this should be set as the source folder
project2/src/app
project2/src/app/__init__.py

然后编辑project2属性>项目引用并选择“project1”以便能够从项目2 访问项目1。

Provided you have 2 projects with the PYTHONPATH properly set (through source folders), it should work just adding a project reference.

Maybe an example can help...

E.g.:

project1
project1/src <-- this should be set as the source folder
project1/src/core
project1/src/core/__init__.py

project2
project2/src <-- this should be set as the source folder
project2/src/app
project2/src/app/__init__.py

and then edit the project2 properties > project references and select 'project1' to be able to access the project1 from the project2.

好久不见√ 2024-12-20 02:35:14

我有一段时间遇到同样的问题了。
现在我弄清楚了如何从其他项目中的模块和包导入类或函数。
我发现自己很愚蠢,因为这很容易。

结论!
您所要做的就是在“项目引用”、文件/属性/项目引用中检查项目名称。
就导入用法而言,引用其他项目与将所有文件或模块放入当前项目目录中相同。
我在 Windows 7 上使用 liclipse_1.0.0_win32.x86_64。

让我展示一下示例。
有两个项目 A 和 B,项目 A 有模块 a1.py 和包“M”。
您正在处理 B 项目并检查以下项目树中的引用 A 项目。

|-- A
|   |-- M
|   |   |-- __init__.py
|   |   `-- m1.py
|   `-- a1.py
`-- B
    `-- b1.py

# a1.py
def say_a():
    print "I'm A"

# m1.py
def say_m():
    print "I'm M"

现在您可以像下面一样访问项目 A。

#b1.py
import a1
from M import m1   
a1.say_a()
m1.say_m()

事实上,python import 语句对我来说有点奇怪,你可以找到其他方法来做同样的事情。
此外,如果你想从 Linux 命令行运行 b1.py 请参阅此。

~/tmp/pydemo/B$ PYTHONPATH=../A python b1.py

I had been in same problem for a while.
Now I figured it out how I can import class or function from module and package in other projects.
I found myself dumb from this because it is ever easy.

Conclusion!
All you have to do is just to check the project name in 'Project References', File/Properties/Project References.
In terms of import usage, referencing other project is same as you put all files or modules in your current project directory.
I am using liclipse_1.0.0_win32.x86_64 on Windows 7.

Let's me show examples.
There are two project A and B and the project A has module a1.py and package 'M'.
You are working on B project and check reference A project in following project tree.

|-- A
|   |-- M
|   |   |-- __init__.py
|   |   `-- m1.py
|   `-- a1.py
`-- B
    `-- b1.py

# a1.py
def say_a():
    print "I'm A"

# m1.py
def say_m():
    print "I'm M"

Now you can access project A like following.

#b1.py
import a1
from M import m1   
a1.say_a()
m1.say_m()

In fact python import statement is little bit odd for me that you can find other way to do same thing.
In addition, if you want to run b1.py from Linux command line see this.

~/tmp/pydemo/B$ PYTHONPATH=../A python b1.py

聽兲甴掵 2024-12-20 02:35:14

我没有通过添加项目引用来实现此功能。但是,我通过首先删除然后将 pydev 项目性质添加到项目中,然后设置项目引用来使其工作。

I did not get this working by adding a project reference. However, I got it working by first removing and then adding the pydev project nature to the project, and then, set the project reference.

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