Cannot import a class from the same folder in python3 - python-3.x

I have been trying all kinds of solutions from the site, but nothing seem to be working for me. My folder structure is:
python_tutorials
linked_list
__init__.py
linked_list.py
queue_using_linked_list.py
In my linked_list.py, I have a class called LinkedList. I am trying to inherit this class in my queue_using_linked_list.py class Queue. So in my queue_using_linked_list.py, I did:
from linked_list import linked_list
from linked_list.linked_list import LinkedList
from linked_list import LinkedList
All these gives me the error "ModuleNotFoundError: No module named 'linked_list'"
from .linked_list import LinkedList
Gives me the error "ImportError: attempted relative import with no known parent package"
I also tried moving the linked_list.py into a new package under linked_list, but still getting one of these error. In the pycharm IDE, all these show no errors, but when I execute they all fail. What am I missing?

The first two import lines are correct, but you have to start the Python interpreter from the python_tutorials directory, not from the linked_list directory.
If you then do import linked_list.queue_using_linked_list it will work as you intended.

Related

using class from different directory

[Python 3.5.2, Docker container]
I have a class BCM in a file called metrics.py which I would like to use in several Jupyter notebooks which are in different directories. When they are in the same file, the following obviously works from metrics import BCM. I would like it to work with the following directory structure.
experiments\
common\
__init__.py
metrics.py
exp1\
glm.ipynb
exp2\
gbm.ipynb
It works if I do the following
import os, sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
sys.path.append(module_path)
from common.metrics import BCM
Based on posts that I have seen here, it would seem that the following should work from ..common.metrics import BCM. I get the following error SystemError: Parent module '' not loaded, cannot perform relative import.
Is there a way to use that class without changing the path as shown above?

Python sub folder modules not able to import other subfolder modules

I have been struggling to figure out how to organize my code effectively with many modules of various types. I have a three layered folder system. The parent directory contains the main.py file which imports and runs the main code. The modules are in a subfolder called lib, and different modules are placed in further subfolders. However, when I import one module from a subfolder, that module itself fails to import modules in the same sub directory. Sorry if this is a bad question, but I cannot figure out why the import statement isn't working, and I have looked at google and stack overflow and have not found any similar problem. All advice is welcome.
Parent Directory
---->lib
|--->module_group
|module_one.py(that tries but fails to import module_one)
|module_two.py
|main.py
In main I run
from lib.module_group.module_one import Module_One
Which works until an error is handled saying that there is no such module as module two. However, when I run module_one by itself, it works fine with the following import statement.
from module_two import Module_Two
Set PYTHONPATH to the directory you want to act as your root. For example, if you want to use the following code:
from lib.module_group.module_one import Module_One
Then set PYTHONPATH to the directorying containing lib. For example:
lib/module_group/module_one.py:
from lib.module_group.module_two import Module_Two
lib/module_group/module_two.py:
class Module_Two:
print('Loaded Module_Two')
Then, to run module_one.py directly and still enable it to use lib.module_group.module_two to load Module_Two, use something like:
$ PYTHONPATH="${PWD}:${PYTHONPATH}" python3 lib/module_group/module_one.py
Loaded Module_Two

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

How can I import a module from a parent directory?

I have the following directory structure for my Python project:
lib
__init__.py
tasks.py
api(directory)
__init__.py
app.py
Now, I want to import the tasks module into app.py. If I just type
import tasks
it magically works in Pycharm, but when executed via bash I get an error message saying "No module named tasks". I also tried
from .. import tasks
which gives me
ValueError: attempted relative import beyond top-level package
What am I doing wrong? Why does import tasks work when executed in Pycharm?

Python 3.5 - Smart module imports in the file tree

I was wondering if it was possible for modules in a project to be smart about their imports...
Say I have the following data structure :
/parent-directory
/package
__init__.py
main.py
/modules
__init__.py
one.py
/submodules-one
__init__.py
oneone.py
onetwo.py
two.py
Files higher in the hierarchy are supposed to import Classes from those lower in the hierarchy.
For instance main.py has
import modules.one
import modules.two
Now I'd like to be able to directly run not only main.py, but also one.py (and all the others)
Except that it doesn't work like I hoped :
If I run from main.py, I need to have in one.py
import modules.submodules-one.oneone
import modules.submodules-one.onetwo
But if I run from one.py, I'll get an error, and I need to have instead
import submodules-one.oneone
import submodules-one.onetwo
I've found a hacky way to get around it using in one.py :
if __name__ == '__main__':
import submodules-one.oneone
import submodules-one.onetwo
else:
import modules.submodules-one.oneone
import modules.submodules-one.onetwo
But isn't there a better solution?
P.S.: I also have an additional complication, I'm using pint,
which to work properly only needs to have a single instance of the unit registry, so I have in the top ____init____.py :
from pint import UnitRegistry
ur = UnitRegistry()
And obviously
from .. import ur
will fail if running from one of the files of the subfolders.
Thank you in advance for your answer.

Resources