Can't import mymodule in Python - python

I'm using VSCode and getting warning "search is not accessed in Pylance" when trying to import my own module, search is the name of the module I need to import to my main program
1- I do have __init__.py file on module folder
2- Tried changing interpreter
Importing mymodule as below:
from src import search
folder structure:
Could the reason of this be that 'src' is being treated as a module instead of a folder?
Is there an alternative way to import my modules?
Thanks

The error "search" is not accessed means you are not using search,
If you use search below then this error will go away.

Related

how to import call from another directory? (python)

I am trying to import a class named DataHandler into my test_data_handler file. I am doing this by stating from handles.data_handler import DataHandler. However, when I do this I get a error saying
No module named 'handles'
If anyone knows how to get round this issue it would be greatly appreciated!
You need add __init__.py file inside folder handles.
The __init__.py file lets the Python interpreter know that a directory contains code for a Python module.
import sys
sys.path.insert(1, '/path/to/application/app/folder')
from handles.data_handler import DataHandler
Importing files from different folder

Python module import error without using sys module

I am trying to use my own custom module, but It is unable to load the module.
This is a hierarchy structure of my custom module.
sources\
set1\
module_1
module_2
set2\
module_3
set3\
module_4
I imported these modules like below
from sources.set1.module_1 import *
from sources.set2.module_2 import method
...
And it occurs import error.
The error message is No module named 'sources
I use VScode, and Python 3.7 (I expect that I don't need to use init.py)
I've googled this problem and I've found 2 solutions. However, these weren't helpful.
Using sys.path.append()
This couldn't be a solution for me. Because I am working with teammates, and it is not allowed to add this code just for me
Adding PYTHONPATH environment variable
I've already added PYTHONPATH with "C://directories//sources",but it doesn't soleve import error. However, I found that this solution allows below codes instead of the original codes.
import module_1 #This occurs no error, But I can't use it
...
You have to call as a packages, for that you have to use init,py in each subdirectory https://www.learnpython.org/en/Modules_and_Packages

Import classes/functions from Python files saved in same folder showing "No Module Named" error

As per screen print, import shows error in Python 3.7 version, earlier it was working fine in version Python 2.7 and I am using IntelliJ Idea.
If you see, EOC related .py files are in the same folder and have classes which are being called in Main_EOC.py by passing objects which are inter-related. It's amazing to see the red line while importing files from same folder.
Please help me why it's showing such error
"This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.`"
Also, if you see the line which have full path, is not showing error
from EOC_Module.eoc.script.config import Config
Please help me if there is a way to add this full path on top of the code or other option.
The behavior of import path search changed between python2 and python3. The import path always includes the directory from which the main module was loaded, but it no longer includes directories from which modules were imported.
You need to change your import statement syntax as follows, if you want to import a module that lives in the same directory as the module in which you do the import:
# old way, import works if the named module is in this module's directory
import x
# new (Python3) way:
from . import x
For the second part: adding a path so all code can import from a certain directory: if that directory is (and will always be) relative to your main: you can add a few lines in the main module to make it available. Something like this:
import sys # if you haven't imported it already
import os.path
home = os.path.dirname(sys.argv[0])
sys.path.append( os.path.join(home, "EOC_Module/eoc/script") )
# now, you can import straight from the script directory
import EOC_Intraction
When using pycharm the root directory for your python executable is the same as the root directory of your project, this means that python will start looking for files in the root directory with this files:
.idea/
EOC_module/
logs/
reports/
sql/
This is the reason of why: from EOC_Module.eoc.script.config import Config works.
If you execute your code from the terminal with: python3 Main_EOC.py (not pycharm) the root directory for your python will be the same as the one containing the file, all the other imports will work but from EOC_Module.eoc.script.config import Config not.
So you need to make your imports from project directory if you are using pycharm.

No module named "file1.py"; test1 is not a package

I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.

Python ModuleNotFoundError with __init__.py

I got a strange issue and have no idea about the reason.
As you can see from the picture, folder fuzzier and parser are under the same parent folder, and both of them have the file __init__.py (both empty because I am not using from xxx import *, and code is based on Python 3.6).
And in another module (under the same parent folder with fuzzier and parser), there is a file doing some import like this:
import fuzzier.jison
import parser.annoying_char
The first line is good, but the second line is with an error ModuleNotFoundError: No module named 'parser.annoying_char'; 'parser' is not a package
I wasted hours on this and wish someone can help with this, Thanks!
parser is an in-built library in Python.
Python is trying to find annoying_char inside that library instead of your module.
You should use some other name.
Source - https://docs.python.org/2/library/parser.html

Categories

Resources