haskell import problem - haskell

I have a module "Dictionary", declared in a file "Dictionary.hs".
in the same directory i have a file "Def.hs" which imports Dictionary
here's the error i get
... /edsl/Def.hs:4:7:
Could not find module `Dictionary':
locations searched:
Dictionary.hs
Dictionary.lhs
... /edsl/Dictionary.hs is there. it's permissions are such that it can be written to or read from by anyone.
i really have no idea why i can't import. i'm using ghc 6.12.1 on mac os x 10.5.8
edit
here's the relevant code
in Dictionary.hs
module Dictionary where
...
and in in Def.hs
module Def where
import Control.Exception
import Data.Dynamic
import Dictionary
...
am i just defining the module incorrectly? i want to export all symbols.

i just found the problem. i'm using komodo edit, and the command the i created to load a file into ghci doesn't change the working directory.
doh.

Related

PyDev: Python: Unresolved Import or ImportError: attempted relative import with no known parent package

tl;dr How should I correctly use relative modules in PyDev?
If I use:
from . import myModule
I get the following error:
ImportError: attempted relative import with no known parent package
However...
If I just use:
import myModule
the project will run, but PyDev flags the line and file with
Unresolved Import: myModule
The package has an __init__.py although its contents are empty
Edit: screenshot of PyDev Explorer structure added as requested by #Fabio Zadrozny
PyDev Explorer
The problem is that it depends on how you run the script... by default PyDev will run the file directly, but in this case you want to run the file with -m <module-name>.
PyDev can do that automatically if you go to PyDev > Run and enable the related setting:
p.s.: either you have to use the relative import and launch with -m or you can just use the full import (which in this particular case would be from pkg_BootMeUPY import in_constrains_ini as const -- in which case it'd work regardless of how you launch the file).

How to write the correct path for importlib in my pip installed python package?

I am trying to create a package where when I call a module a certain class from a python file is automatically loaded, but I can not figure out how to set the correct path with importlib. I try to load the package after installing it with pip install.
In the subfolder pkg_sub, I have __init__.py and testfile.py.
The __init__.py contains this code:
import importlib
pkclass = getattr(importlib.import_module('testfile'), 'pkclass')
And 'testfile.py' contains this code:
class pkclass:
# Some generic print commands for testing.
When I import pkg_sub, I get the error that importlib can not find testfile.py.
I am still confused with how pathing works. I also tried this line in __init__.py:
import importlib
pkclass = getattr(importlib.import_module('.testfile'), 'pkclass')
But then importlib wants to have a package directory in addition to testfile.py. But there are no additional subfolders that I could point importlib to.
Overall, I am just confused how I have to write the path here so that I can make the class directly accessible when I load the module. Any help would be appreciated.

python module import error - python-mode [VIM]

I am trying to import a module from another directory and run the script using python-mode. I am encountering, module not found error, but the module is present in the location and my sys.path shows the module path has been added successfully. I am having hard time troubleshooting/fixing. Could some one shed some light on this?
import numpy as np
import sys
sys.path.append('./extnsn/')
from extnsn import FX
The error stack is:
Feat/mFeat/feat_Xt_v2.py|7 error| in from
extnsn import FX ImportError: No module named 'extnsn'
My directory structure is:
Feat
|
|--mFeat
|
|--feat_Xt_v2.py
|
|--extnsn
|
|--__init__.py
|--FX.py
The extnsn directory has a __init__.py with the following:
from extnsn import FX
FX.py is the module name, for information.
sys.path contains the appended path as ./extnsn/ as the last entry in the list.
What makes me conclude this is not path issue is that, the program runs fine, if executed from atom with script plugin.
Any help is much appreciated.
EDIT:
This doesn't seem to be an issue with just python-mode, rather the way how vim invoke the python interpretor and execute the buffer. I tried with the following command without python-mode and the issue is the same.
To import a module or a package you have to add to sys.path its parent directory. In your case if you've added ./extnsn/ to sys.path you cannot import extnsn (it cannot be found in sys.path) but you can import FX directly:
import FX
But as FX seems to be a module in a package extnsn you better add to sys.path the parent directory of extnsn, i.e. Feat:
sys.path.append('../Feat')
from extnsn import FX

ModuleNotFoundError: No module named '__main__.xxxx'; '__main__' is not a package

Currently trying to work in Python3 and use absolute imports to import one module into another but I get the error ModuleNotFoundError: No module named '__main__.moduleB'; '__main__' is not a package. Consider this project structure:
proj
__init__.py3 (empty)
moduleA.py3
moduleB.py3
moduleA.py3
from .moduleB import ModuleB
ModuleB.hello()
moduleB.py3
class ModuleB:
def hello():
print("hello world")
Then running python3 moduleA.py3 gives the error. What needs to be changed here?
.moduleB is a relative import. Relative only works when the parent module is imported or loaded first. That means you need to have proj imported somewhere in your current runtime environment. When you are are using command python3 moduleA.py3, it is getting no chance to import parent module. You can:
from proj.moduleB import moduleB OR
You can create another script, let's say run.py, to invoke from proj import moduleA
Good luck with your journey to the awesome land of Python.
Foreword
I'm developing a project which in fact is a Python package that can be installed through pip, but it also exposes a command line interface. I don't have problems running my project after installing it with pip install ., but hey, who does this every time after changing something in one of the project files? I needed to run the whole thing through simple python mypackage/main.py.
/my-project
- README.md
- setup.py
/mypackage
- __init__.py
- main.py
- common.py
The different faces of the same problem
I tried importing a few functions in main.py from my common.py module. I tried different configurations that gave different errors, and I want to share with you with my observations and leave a quick note for future me as well.
Relative import
The first what I tried was a relative import:
from .common import my_func
I ran my application with simple: python mypackage/main.py. Unfortunately this gave the following error:
ModuleNotFoundError: No module named '__main__.common'; '__main__' is not a package
The cause of this problem is that the main.py was executed directly by python command, thus becoming the main module named __main__. If we connect this information with the relative import we used, we get what we have in the error message: __main__.common. This is explained in the Python documentation:
Note that relative imports are based on the name of the current module. Since the name of the main module is always __main__, modules intended for use as the main module of a Python application must always use absolute imports.
When I installed my package with pip install . and then ran it, it worked perfectly fine. I was also able to import mypackage.main module in a Python console. So it looks like there's a problem only with running it directly.
Absolute import
Let's follow the advise from the documentation and change the import statement to something different:
from common import my_func
If we now try to run this as before: python mypackage/main.py, then it works as expected! But, there's a caveat when you, like me, develop something that need to work as a standalone command line tool after installing it with pip. I installed my package with pip install . and then tried to run it...
ModuleNotFoundError: No module named 'common'
What's worse, when I opened a Python console, and tried to import the main module manually (import mypackage.main), then I got the same error as above. The reason for that is simple: common is no longer a relative import, so Python tries to find it in installed packages. We don't have such package, that's why it fails.
The solution with an absolute import works well only when you create a typical Python app that is executed with a python command.
Import with a package name
There is also a third possibility to import the common module:
from mypackage.common import my_func
This is not very different from the relative import approach, as long as we do it from the context of mypackage. And again, trying to run this with python mypackage/main.py ends similar:
ModuleNotFoundError: No module named 'mypackage'
How irritating that could be, the interpreter is right, you don't have such package installed.
The solution
For simple Python apps
Just use absolute imports (without the dot), and everything will be fine.
For installable Python apps in development
Use relative imports, or imports with a package name on the beginning, because you need them like this when your app is installed. When it comes to running such module in development, Python can be executed with the -m option:
-m mod : run library module as a script (terminates option list)
So instead of python mypackage/main.py, do it like this: python -m mypackage.main.
In addition to md-sabuj-sarker's answer, there is a really good example in the Python modules documentation.
This is what the docs say about intra-package-references:
Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute imports.
If you run python3 moduleA.py3, moduleA is used as the main module, so using the absolute import looks like the right thing to do.
However, beware that this absolute import (from package.module import something) fails if, for some reason, the package contains a module file with the same name as the package (at least, on my Python 3.7). So, for example, it would fail if you have (using the OP's example):
proj/
__init__.py (empty)
proj.py (same name as package)
moduleA.py
moduleB.py
in which case you would get:
ModuleNotFoundError: No module named 'proj.moduleB'; 'proj' is not a package
Alternatively, you could remove the . in from .moduleB import, as suggested here and here, which seems to work, although my PyCharm (2018.2.4) marks this as an "Unresolved reference" and fails to autocomplete.
Maybe you can do this before importing the moduleļ¼š
moduleA.py3
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from moduleB import ModuleB
ModuleB.hello()
Add the current directory to the environment directory
Just rename the file from where you run the app to main.py:
from app import app
if __name__ == '__main__':
app.run()
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
will solve the issue of import path issue.

Could not find module, it is a member of the hidden package haskell98

When I try to compile a simple source file with import IO or import Random, the build fails with an error message like this:
Could not find module 'IO'
It is a member of the hidden package 'haskell98-2.0.0.1'
Use -v to see a list of the files searched for
The module names changed at some point. You probably want import System.IO and import System.Random instead.
Here is the module hierarchy for the standard libraries in GHC 7.6.1.

Resources