Importing library in Dart - import

I know that to import some library, which is located in some folder. Example hiearchy:
library1
folder1
library2
Then importing library2 would look like:
import 'folder1/library2';
But how can I import library1 from library2?

import '../library1';
or
import 'my_package:library1.dart';
when the file library1.dart is stored in my_package/lib/library1.dart and your package name in pubspec.yaml is my_package.
If the imported file is deeper down the path relative path import is fine, when it's higher up the absolute import path (package:...) should be preferred.
Only files stored somewhere in the the lib directory of your package can be imported using a package:... path.

You can try this
import '../library1.dart';

Related

how to import local package files in my main package?

I want to import the about.dart file in my main.dart file. But it is showing error while I'm copying its path. I tried many ways to import it, but all fails. Can anyone please help me in this?
if both the files are in the same folder you can import them using this line
import './dialpad.dart';
if you want to import the file from a folder at the same level of the file in which you want to import you can use this line
import '../widgets/some_file.dart';
To use your local package you have to define package and give a package path inside your pubspec.yaml for the package.
To add local package in pubspec.yaml:
sip_up-0.5.6:
path: ./sip_up-0.5.6

A confusion about import in Spark

I read the source code of KMeans.scala in spark and it confused me with the following code:
import org.apache.spark.Logging
import org.apache.spark.annotation.Experimental
import org.apache.spark.mllib.linalg.{Vector, Vectors}
import org.apache.spark.mllib.linalg.BLAS.{axpy, scal}
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import org.apache.spark.util.Utils
import org.apache.spark.util.random.XORShiftRandom
I found the file RDD is in the path "spark-1.4.0\core\src\main\scala\org\apache\spark\rdd" which corresponds to import org.apache.spark.rdd.RDD. But the file MLUtils is in the path "spark-1.4.0\mllib\src\main\scala\org\apache\spark\mllib\util" which corresponds to import org.apache.spark.mllib.util.MLUtils.
Why their import paths start with "org.apache.spark"? It seems they are in the same folder "spark".
Why do their import paths start with "org.apache.spark"?
Path to the source file doesn't determine the package it belongs to, package declarations in it do. Nonetheless, it's standard (and useful in some ways) to put source files in the directory corresponding to the package, under src/main/scala or <subproject(core and mllib in these two cases)>/src/main/scala. Relative to it you can see the directories are org/apache/spark/rdd and org/apache/spark/mllib/util, just as in imports.

can no longer import from ElementTree

For some years alread I have had the following import in my code:
from etree.ElementTree import fromstring, parse, VERSION
Today I made a mistake while moving (in Eclipse/pyDev) a few unrelated sourcefiles to another folder. The folder was not a package and it cost me some cleans, rebuilds and del *.pyc-s to get them found again. That part has been solved, but now, the import above breaks with "unresoved import...". When I remove the etree-prefix, the imports are resolved, but at runtime I get
from ElementTree import fromstring, parse, VERSION
File "C:\Program Files\Python\EPD-7.3-2x64\Lib\xml\etree\ElementTree.py", line 127, in <module>
from . import ElementPath
ValueError: Attempted relative import in non-package
What is going wrong..?
You should not have been able to.
The import normally would be from xml.etree.ElementTree import ...; the top-level package name is xml.etree, not etree.
It looks as if you added the xml.etree package to the Python sys.path module search path. Don't do that. Remove C:\Program Files\Python\EPD-7.3-2x64\Lib\xml\etree from your sys.path (or the PYTHONPATH environment variable) and import from the correct top-level package name instead.

Determine location of jar file from import

If I have an import statement like:
import com.google.android.gms.maps.model.BitmapDescriptor;
is it possible to determine in Eclipse the jar file where this class is located?
try
ctrl-shift-t
type BitmapDescriptor
then show in Package Explorer

Why does importing from a module from the current directory only work when within that directory?

Background
I have a Python project with this directory structure:
py/:
db/ __init__.py run.py
py/db:
handle.py __init__.py util.py
The files are simple enough that I'm not sure I need to post them; nevertheless:
py/run.py
from db.handle import Handle
py/db/handle.py:
import util
class Handle:
def __init__(self, x):
self.x = util.addtwo(x)
py/db/util.py:
def addtwo(x):
return x + 2
If I run handle.py from within the db subdirectory, it imports util without error. However, when I run run.py, handle.py fails with an import error. I can guess that handle.py is being run in the py directory (instead of py/db), and putting a call to os.getcwd() in handle.py confirms this. I can fix this problem using sys.path like so (in run.py):
import sys
sys.path.append("db")
from db.handle import Handle
Question
When importing, from a subdirectory, a module that contains imports to other local modules in that directory, why doesn't Python check the current directory of the module making the import statement? In my example, why doesn't Python check the db first when handle.py contains import statements? Is there a PEP that describes this or is it a behavior with an obvious rationale that I missed?
I thought it might be related to PEP 328:
all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.
but I'm not sure.
Your import is "absolute" and module names is looked for in the PYTHONPATH, and that typically includes the current directory.
If you want to import a module from the same folder that your module is in, you use a relative import:
from . import util
or
from .util import addtwo