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?
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.
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
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.
Related
I use Eclipse as IDE for Go (I guess it's called "Goclipse"), developing a web application. All went good and nice until I started to split functionality in smaller packages. Initially I had a package gitserver/user/project/portal. File main.go imported it and everything was working. I suddenly realized, that it should be called gitserver/user/project/webserver instead, and some other files and functionality should go into .../portal. So I did "Rename", by right clicking in Project Explorer.
main.go:
package main
import(
"gitserver/user/project/webserver"
)
func main(){
webserver.DoSomeStuff()
}
Compiler complains that
imported and not used "gitserver/user/project/webserver" as portal
undefined: webserver
[...]
How to untie the "webserver" package from its old name?
GoClipse doesn't actually do any tracking of the old name after the rename is done. Based on what you describe and the error imported and not used "gitserver/user/project/webserver" as portal you probably have an import line along the lines of portal "gitserver/user/project/webserver" somewhere, and probably in the same file it uses webserver.. Double check all your imports because these errors absolutely sound like the rename just got a little mixed up (something that's not uncommon when doing a rename and reusing the old name).
Goclipse may not find every kind of reference to the package when renaming; the change might not be cascaded throughout the project, or even the package files. When you rename a folder, check that the package line has been changed in the package files, and update them if not.
I'm a CS student and am in way over my head for an end of the year project, for which I need to find the dimensions and locations of a separate window. I've found that I need to use JNA libraries and understand how to do that but I don't understand how to import the JNA libraries through the manifest. Here is my file structure so far (I'm working in netbeans):
Project Folder
dist Folder
nbproject Folder
src Folder
Test Folder
Build.xml
Game.exe <-- This is the exe I run from the java file itself. I need to find this window's size and location for use later in the project
jna.jar <-- need to implement this as a classpath
Manifest.mf <-- I supposedly need to edit this.
The reason I show you this is to ensure that I've put the JNA Jar in the correct place.
Next, here is my manifest:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Class-Path: C:\Users\justi_000\Google Drive\4. APCS\JZMinesweeper\jna.jar
So I need to edit the bit after the java -cp but I don't know what to put there, and in what order. The java tuts, etc aren't of much help.
Additionally, are these imports correct? They may be the problem and I have correctly imported everything, but I doubt that.
import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.*;
And, turns out, that it was a stupidly easy solution that my general incompetence caused me to miss.
Turns out it is actually possible to directly import libraries in IDEs...probably why they all include a tool for it. Blind old me has only ever used them once before (the perils of being a student) but we're all set. Simple as right click libraries>add jar.
Figured i would stick this here for future reference.
I'm trying to create a library which could be used in other projects. I've written one class with several static methods to do some stuff. I wanted to try it out but I am not able to use the imported JAR file.
I have compiled my code as an artifact and took the JAR file from "out" folder and then copied it to another project. After that I went to "Project structure", tab "Libraries" and I pressed the plus button. I've found the JAR file and selected it, afterwards IDEA asked me to specify dependencies so I did, but when I want to use it in code I am not able to do so. It can't even be imported.
Any ideas why it ignores my library? Thanks!
What should I do in order to create a JAR library with IntelliJ IDEA, that is usable in other projects?
You are running into a very common dependency management problem.
IMO the real answer is to use a build system like Maven, Ant, or Gradle (I'd go Gradle myself). What you are trying to do is manual, hard to reproduce, and brittle.
Every time you make a change you will have to go through manual steps to create a new JAR. And you really don't understand your dependencies.
To go all out with best practices you would be to have real build system that publishes to a continuous integration server, which compiles and runs tests. On successful completion of the tests, the JARs are published to an artifact server (Nexus/Artifactory).
The people you are sharing with would consume the JARs via the build system by declaring dependencies on your JAR.
I figured out what my problem was. When I created the library I was trying to make it simple. Too simple, unfortunately. I had a package with a class in it that was compiled into a JAR. Structure shown below:
foo
|
|_ MyLib.java
However in order to use classes from a created JAR library they have to be placed in packages. That means if I have:
foo
|
|_bar
| |
| |_MyInnerLib.java
|
|_MyOuterLib.java
I am able to import and use methods from MyInnerLib but MyOuterLib isn't reachable nor importable. This was the error I was making.
There are many suggestions on the web what the structure of the Python project can/shall be, e.g. What is the best project structure for a Python application?.
"proj-dir"
+- doc
+- apidoc
+- scripts
+- "src-dir"
+- tests
It seems that many people in the Python world prefer the "src-dir" and "proj-dir" to be equal (or very similar). However, the scripts and tests will surely have to import some modules from src-dir, so either I have to
run all scripts and tests when proj-dir as my current working dir, or
the PYTHONPATH must contain the proj-dir, right?
Or, is there any other possibility which I overlook now?
Suppose I have such structure and have set up the PYTHONPATH correctly. Now I commit the project into VCS and another developer checks the project out. On his machine the PYTHONPATH will not be set properly and the imports in scripts and in tests will not work.
Is there any possibility to somehow make the PYTHONPATH definition part of the project, so that it can be part of the version control?
Is there a different project structure that would allow me to make checkout and start using the project without modifying the PYTHONPATH?
Is to solution suggested here: Python - How to PYTHONPATH with a complex directory structure? (i.e. to include a short sys.path modifying snippet in every script and test) the right one?
Would a site module http://docs.python.org/2/library/site.html be of any help?
What is your workflow when developing a project with a similar structure?
Even through project structure is complex and testcases uses src in different package, you can add PYTHONPATH in Environmental Variables in Windows
My Computer --> Properties --> System Properties --> Advanced Tab --> Environmental Variables. Add new System Variable PYTHONPATH and append your projects dir to that variable with delimiter ';'
I suggest you to keep readme.txt to your project which clearly says to how to add PYTHONPATH in order to run the scripts. It is as similar as adding PATH of android-sdk to make adb shell to work, adding Java path to make Java to work in command prompt
Thus readme.txt helps to run the script when downloaded in different machine with only one change(adding PYTHONPATH variable with value = projectpath)
No need to add all modules of src in order to make testcases to work which is using src
Here is an example proj structure:
SAMPLEPROJECT
src
com
sample
example
utils
a.py
b.py
testcases
test1.py
test2.py
res
docs
If test1.py test case is using a.py and and b.py:
Dont add utils module to PYTHONPATH, as you already added project path to python path your import statement looks something like this in test1.py
from src.com.sample.example.utils import a.py
from src.com.sample.example.utils import b.py
Question
Is there a way to reliably ensure that both the python source paths and compiled java class paths are added to the Jython PythonPath when running unit tests under eclipse?
If not, is there a better workaround than adding absolute paths to the PyDev PythonPath, External Libraries list?
Background
I have been having a strange problem with running Jython unit tests on a Jython class which inherits from a Java class.
We have an eclipse RCP application with a structure (simplified) like this:
Config project (Pydev nature only)
scripts
src
classes.py
test
classesTest.py
Core project (Both Java & Pydev natures)
scripts
src
baseClass.java
classes
baseClass.java
Library project (Both Java & Pydev natues, but no Jython scripts)
src
interface.java
classes
interface.class
In classes.py I import baseClass, which implements an interface defined in interface.java. I then create a Jython class which inherits from baseClass.
In my classesTest.py, I import classes and define the unit tests.
The problems come when I try to run the unit tests using "Project explorer > classesTest.py > Run as > Jython unit-test":
Sometimes it is unable to find the baseClass (I think it fails with an ImportError, but I can't reproduce this right now).
At other times it finds the baseClass, but then can't find the interface, so it fails with a java.lang.NoClassDefFoundError.
If I pprint the sys.path at the start of the test module, then I can see that:
While core/scripts is on the python path, neither core/classes nor library/classes are on the path.
Both core/scripts and core/classes are on the python path, but library/classes still is not.
Sometimes, very rarely, both classes paths are added to the python path and the unit tests work fine, without the workaround below. Unfortunately, I haven't worked out how to reproduce this and can see no difference in the files in the source tree between it working and it not working.
Current Workaround
My current workaround is to explicitly add in "External Libraries" for library/classes and core/classes explicitly. Unfortunately it appears that these have to be absolute paths, which means I have to set them differently for every trunk, branch or computer that I use. As such, I don't want to commit these into revision control and it is a pain having to set them up every time.
Notes
Note that the Config project only references the core project, but referencing the library project explicitly makes no difference to the class path.
Also, while trying to find a reliable method to reproduce this problem, I had other libraries inexplicably disappear from the python path and then just as mysteriously reappear, the most recent example being Mock!
Update
I haven't seen this problem recently (currently using Eclipse 3.7.1 & PyDev 2.2.2) so it may have been an aberration with the combination of Eclipse/PyDev I was using at the time.
For a java project, you have to explicitly add the folder with the .class files to the PYTHONPATH.
The recommended organization would be:
java_project/bin <- this folder should be set as a source folder within PyDev (to be added to the PYTHONPATH).
java_project/src <- this folder should be added to the jdt classpath, but should not have any reference in PyDev
python_project/src <- this folder should be set as a source folder within PyDev (to be added to the PYTHONPATH).
And the python_project should have a reference to the java_project.
See the last part of http://pydev.org/manual_101_project_conf2.html as a reference.
I had a similar problem. Recently resolved by accordingly modifying the python.path field in the Jython registry file.
It looks like the Pydev plugin cannot modify the sys.path to be able to import the Python module from a Java project in Eclipse.
More details at Adding in a python module to the sys.path for a Java project in pydev, Eclipse