Python module not recognizing files in the same folder - python-3.x

I've made (at least tried) a package using setuptools and attempted to use it from another python-file. However, the modules within the packages don't seem to recognize each other.
Tree
pkg
|-- pkg
| |-- __init__.py
| \-- module.py
\-- setup.py
__init__.py:
import module
#code
pyfile.py
import pkg
#code
When I attempt to run pyfile.py, I get
Traceback (most recent call last):
File "/.../py/pyfile.py", line 1, in <module>
import pkg
File "/.../pkg/pkg/__init__.py", line 1, in <module>
import module
ModuleNotFoundError: No module named 'module'
It works fine if I write import pkg.module, but I don't see why self.referential code would be practical.

Change the import in your __init__ to
from . import module
You can read more about intra-package references in the python documentation.
(BTW, as far as I can tell, setuptools is not involved here.)

Related

How to import from a module in the root directory?

I have the following project structure:
root
|--pkg1
| |--__init__.py
| |--other_stuff.py
|--stuff.py
In the real project, I also have a tests package in the root directory.
If I run unit tests with python -m unittest at root, it works seamlessly.
I want to make the other_stuff.py module also executable as a script.
However, if I run it from either root or pkg1 directories, the Python interpreter returns me this error:
Traceback (most recent call last):
File "pkg1/other_stuff.py", line 1, in <module>
from stuff import Dummy
ModuleNotFoundError: No module named 'stuff'
Here is the code:
# file pkg1/__init__.py
# (empty)
# file pkg1/other_stuff.py
from stuff import Dummy
if __name__ == '__main__':
# do other stuff
pass
# file stuff.py
class Dummy:
pass
I also tried the package relative imports from .stuff and from ..stuff, but it gives the error:
Traceback (most recent call last):
File "pkg1/other_stuff.py", line 1, in <module>
from ..stuff import Dummy
ImportError: attempted relative import with no known parent package
I'm not used to the Python3 import system.
What is the right way to import stuff.py in other_stuff.py to make it work both in the tests and as a script?
I have found a solution that might not be the best, but it worked.
To run other_stuff.py as a script, I just need to open the terminal in the root directory and run python3 -m pkg1.other_stuff.

Can't import relative module

The file structure for a module im creating goes as follows:
PIV
| __init__.py
| base.py
| core.py
| exceptions.py
.gitignore
LICENSE
requirements.txt
But whenever I run a file like core.py, I get the following error:
Traceback (most recent call last):
File "c:/Users/ghub4/OneDrive/Desktop/Python-Image-and-Video-tools/PIV/core.py", line 33, in <module>
from . import base
ImportError: attempted relative import with no known parent package
The same thing happens when I run the __init__.py file. I'm not sure on what went wrong because all of the python files are in the same folder. Can someone clarify what's the problem and explain how I should fix it?
Import code for core.py file:
from __future__ import absolute_import
import sys
import os
from PIL import Image
import io
from . import base
from . import exceptions
(The __init__.py folder has the same relative imports as in the core file but also including: from . import core)
Based upon the two links you will given below, here is what needed for the problem to solve:
Relative Imports error rectified
No module named base
You need to import the package as this
from mymodule import some_useful_method
Sometimes, we get the no module error, in this case, we can import like this
from module_name.classname import some_useful_method

can't import a script from the same module

This situation may be related to python's configuration. My OS is OSX 10.14.6
Here is my directory tree:
code
|--- main.py
|--- module
|--- __init__.py
|--- core.py
|--- util.py
In main.py
from module import core
In core.py
import util
This works (python2):
python main.py
And this does not:
python3 main.py
Error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from module import core
File "/code/module/core.py", line 1, in <module>
import util
ModuleNotFoundError: No module named 'util'
The solution I can give you is replace
import util
with
from . import util
I think this has something to do with python3's Implicit Namespace Packages basically making . it's own module as it were. That's my guess, I haven't found anything in the docs explicitly saying that.

Python3 Relative Import Is not Working

I'm new in python 3. I'm trying to run lark examples http://github.com/lark-parser/lark in a development mode, but was blocked on relative import problem.
lark
|examples
| |
| |conf_lalr.py
|
|lark
| |
| |lark.py
|
|tools
| |
|common.py
In conf_lalr.py, there's a line:
from lark import Lark
Since I want use relative import, then I updated it with below methods:
1, from ..lark.lark import Lark
Traceback (most recent call last):
File "conf_lalr.py", line 16, in <module>
from ..lark.lark import Lark
ValueError: attempted relative import beyond top-level package
2, from .lark.lark import Lark
Traceback (most recent call last):
File "conf_lalr.py", line 16, in <module>
from .lark.lark import Lark
ModuleNotFoundError: No module named '__main__.lark'; '__main__' is not a package
I searched lots of answers from internet, including stackoverflow. However, none is working.
Need someone tell why.
You have missing init.py files to make the folders as python packages.
Also for the first part , see this or this
To run the examples, you should do the following:
~$ cd lark
~/lark$ python -m examples.conf_lalr

How to create package in python 3.5.1?

I have tried to create a package using python 3.5.1 , but got the error when i import the package.
Traceback (most recent call last):
File "Pack.py", line 2, in
import Com
File "C:\Users\admin\Document\Python\packages\Com__init__.py",
line 2, in
from Algebra import *
ImportError: No module named 'Algebra'
To import a package you created named Algebra, your folder structure should look similar to this:
C:\Users\admin\Document\Python\packages\
Com__init__.py
Algebra\
__init__.py
.
.
.
Alternatively, you can put the Algebra packages parent directory on python's sys.path.
You'll likely run into another issue once you resolve this, which is you cannot import * from the Algebra package without specifying the all trait in Algebra's init.py file. Specifying this done like the following, but replacing the values of the list with your module names:
__all__=['add', 'subtract', 'multiply']
Source: https://docs.python.org/3/tutorial/modules.html#packages

Resources