ModuleNotFoundError: No module named 'module' while importing my own module VsCode MacOs Python 3.10 - python-3.x

Structure:
|--folder/
|--a.py
|--main.py
When loading module 'main' into module 'a'
#a.py
import main
the following error occurs - ModuleNotFoundError: No module named 'main'.
PyCharm copes with this task, but VSCode does not. What's the matter?

Looking at your directory structure, I presume that a.py and main.py are in different directories. If that is the case, then the answers to [this question][1] should be useful. To elaborate a bit further, you can use the sys module to specify the path to the other module (main.py) i.e. if main.py is in a different directory from a.py.
import sys
sys.path.append('/path_to_main_module_directory/')
import main
print("This import is for the main module")
I hope this helps!
[1]: Importing files from different folder

Related

Importing Module issue (name of project and name of module are the same)

So I am having an issue trying to import some modules right now because the name of the project is same as the module name. At least thats what I think.
I am trying to run the test_aws_file.py main file. That file is trying to import my.test.utils.util_file, but it is getting this error:
ModuleNotFoundError: No module named 'my.test.aws', so seems like i can import up to 'my.test' then when i add .aws then it would cause the error
test_aws_file.py import coding:
sys.path.insert(1, os.path.abspath('.'))
print(sys.path)
from my.test.aws.utils.utils_file import UtilsFile
project name (i do have a init.py in all directories): my.test.aws
my/
test/
aws/
utils/
util_file.py
test_aws/
test_aws_file.py
you should have __init__.py files in your directories to make those packages, otherwise the import wont work.

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

"No module named" when importing from another package in python3.6

If I execute demo2.py it works fine, the problem is when I execute main.py
|myPackage
|subPackage
demo.py
demo2.py
main.py
main.py
from ludikDriver.demo2 import demo2_print
demo2_print()
demo2.py
from demo import demoprint
def demo2_print():
print("demo2")
demoprint()
demo2_print()
demo.py
def demoprint():
print("demo")
Error:No module named 'demo'
Just use relative imports as suggested in pep 328.
from .demo import demoprint
You can do for the other package. Just as relative paths.
Your modules need context of themselves. You should have the "__init__.py" file in subPackage and myPackage. Then your import should be relative:
from . import demo
OR more in context of your example:
from .demo import demoprint
Your error is associated with the top line in demo.py:
from demo import demoprint
There is no module named demo

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?

import package from an other package

I have a very simple project architecture:
project/
__init__.py
dira/
__init__.py
a.py
dirb/
__init__.py
b.py
All the __init__.py are empty.
a.py only contains a "hello" function:
def hello(): return "hello world"
I want b.py to use the hello function from a.py.
What I tried so far:
Following answers from this question, I tried:
import ..dira.a as aa
aa.hello
But when running b.py I get a syntax error on ..a.a
I also tried to manually add the project directory to sys.path and then run:
import dira.a as aa
aa.hello
But when running b.py I get:
ImportError: No module named 'a'
What should I do? Why the solution I found doesn't work?
Short answer:
import ..dira is invalid syntax and only from ..dira.a import hello works
Working configuration: b.py:
from ..dira.a import hello
print(hello())
Also, from docs:
"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."
So if you do relative imports you cannot call b.py directly. Apperently, you cannot do it from project level either. (Python interpreter in project folder, call import dirb.b)
An error occurs:
ValueError: attempted relative import beyond top-level package
But calling import project.dirb.b makes proper output:
hello world
EDIT: Why import project.dirb.b can be called only outside of project module?
Let's look at the problem from interpreter's side.
You call import project.dirb.b. It runs dirb/init.py, 'current module' is project.dirb
Interpreter finds command from ..dira.a import hello
It treats double dot ..dira.a as "move one module up and go to dira and import a.hello". 'Current module' is project.dirb, so parent module is project, thus it finds parent.dira
What happens when you call import dirb.b directly? It fails on trying to go level up in module hierarchy and reports error.
Thus, relative import makes project.dirb.b importable only outside of project.

Resources