can no longer import from ElementTree - import

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.

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

import statsmodels package returns ModuleNotFound despite correct path

I am trying to import statsmodels.stats.weightstats.DescrStatsW.tconfint_mean.
I run
from statsmodels.stats.weightstats.DescrStatsW import tconfint_mean
I get the error
ModuleNotFoundError: No module named
'statsmodels.stats.weightstats.DescrStatsW';
'statsmodels.stats.weightstats' is not a package
I have confirmed that I can import other packages from statsmodels with no problems.
I must be using the wrong path, but the docs don't specify any other path to use.
I just figured it out. I will leave this up for progeny.
from statsmodels.stats.weightstats import DescrStatsW
DescrStatsW.tconfint_mean(...)
Not sure why this behaves differently from other python libraries.

ModuleNotFoundError: No module named 'myfile.py'

So I have trawled through other very similar issues and tried many variations but I cannot get this to work.
I have created code in a separate file under the following structure:
/somefolder/ #this is where my main code resides
/somefolder/src/ this is where my myfile.py resides that I want to import.
My main code starts as follows:
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../src'))
import myfile as mine
but then I get teh following error:
ModuleNotFoundError: No module named 'myfile'
Any assistance would be very welcome.
Assuming your current directory is PythonApplication1, you can follow these steps to access myfile module.
create __init__.py file in both PythonApplication1 and
PythonApplication1\src directories
Now import myfile as from src import myfile as mine

Importing library in Dart

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';

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