Relative imports and package structure in eclipse? - eclipse

I'm having trouble with relative imports, but I think its because i'm not understanding package structure completely.
For example, here is my package structure.
neo_autorig/ Source folder, Top level
__init__.py
basic/ Subpackage for basic utiltites for the script
__init__.py
name.py
name_test.py
module_locator.py
Theres more than this, but this is basically what i'm using for imports
In name.py i'm importing module locator using
from .. import module_locator
But it says
# Error: line 1: Attempted relative import beyond toplevel package
Are top level scripts (like my main script/ui used to execute everything) supposed to go in the top source folder in the eclipse package? Or am i setting this up wrong. There are also other sub packages in the source folder, each with scripts in them.
Edit: If i putanother package in a sub package, I can relative import, its only the case where i cant relative import from a sub package to a top level package, and the scripts source is in my python path.

The python import mechanism works with the __name__ of the file. Executing a file directly gives the file the name "__main__" instead of its usual name. The common answer to questions like this would be to run the program with the -m option. I recommend reading pep 366 and maybe this or this question as well.

Related

Intellij import existing resource into package

I'm following a Udemy course that uses Eclipse for Scala. Using Eclipse, he creates a package, then right-clicks on that package, selects Import, then General, then FileSystem and selects an existing resource that he provides in the course.
I'd like to know how to do this in Intellij. I created a package but see no way to import an existing resource into it.
Many thanks.
You either configure content root in the existing module or add a new module with the source roots, then make your main module depend on it.
IntelliJ IDEA will not copy the files from some other location, so you have two options:
set up a module with the content roots for the existing location with the sources
copy files to the existing module source root using your favorite file manager.

How to refer to generated source files?

How do I refer to files generated by a sourceGenerators sbt task? I know that the source file is deposited to target/scala-<version>/src_managed/path/to/File.Scala but I don't know how to reference it in my project. Any ideas?
/server
/app
/models
Driver.scala (can't use MyGeneratedCode here)
/project
/src/main/scala/
ModelGenerator.scala (the code used to create MyGeneratedCode.scala)
/target/scala-2.12
/classes
/src_managed
/main/generated/
MyGeneratedCode.scala
The generate file works as any normal Scala file but on a fancy folder so it is not tracked by git (or any other VCS).
As any Scala file, it should had a package whatever statement at the beginning, from which you can latter import your classes (import whatever._). Usually the package name matches the directory structure, but they do not have to (specially since it is just generated file).
If it does not have a package, all its members probably resides on the __root__ package. But that probably would cause troubles, I would suggest editing your generator to add a package statement instead.

Katalon external library

I'm trying to use my own code for Katalon as a library.
My own code has imports like this
from com.test.page import Page
from selenium.webdriver.common.keys import Keys
import robot.utils.asserts as asserts
This file is totally executable on its own when I'm using IDE, but when I tried to import it to Katalon, I got exception message like this
ImportError: No module named test
In order to use external references, you should go to the IDE and select
Project -> Settings -> External Libraries
On the window add your jar file.
Remember that you also need to check if the .classpath file is updated accordingly if you use git (it's on the .gitignore list, so needs to be updated manually)
Do you put your .jar in Drivers folder inside your project?
Write import com.test.page.Page instead of yours
The best place to keep all jar files is to create a folder in Katalon folder structure and store all jar files in it. When you commit Katalon project into a source repositories like Github and SVN, don't forget to commit jar files as well so that the references given in the code will no throw an error.
Try this:
put additional jars in lib directory like "C:\Users[ME].katalon\8.2.0\Katalon_Studio_Engine_Windows_64-8.2.0\configuration\resources\lib\jsch-0.1.54.jar".
restart katalon then in your script, use import com.jcraft.jsch.*;

Java import directories

I want to ask smaller question about Java VM and thing about it.
If I understand import process correctly, then when I request contents of packages by import keyword in Java, packages are being imported from it's specified import directory. Then when I'm running Java application from a directory in computer and some of the imported packages could not be found in JVM import directory, JVM is looking for them in current directory. But in that case something must be wrong, because when I run my project from Eclipse and I request some package from the project, it's found. I think that Eclipse is changing directory where packages are being searched. Am I right? Is this all way of importing in Java?
The import statement is just syntactic sugar so you don't have to specify the fully qualified name everywhere in your classes. It doesn't actually import anything in terms of code.
Classes are looked up in the classpath.
In eclipse you can configure the build path (for each project) option project - - > build path.
There you can either add a directory, jar file, external jar files, etc.
Also you can check what's already defined.

VCS and Python project structure: How to setup the PYTHONPATH automatically?

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