python package import from the root - python

I have a following directory structure:
main_work/
data_processing/
data_extraction.py
data_selector.py
data_selector.py:
from data_extraction import connect_to_db
If I start working in main_work and want to do
from data_processing import data_selector as ds
it throws me an error:
ModuleNotFoundError: No module named 'data_extraction'
So, the import in data_selector.py takes into account only current folder (main_work) and doesnt go one level deeper into data_processing.
I need to be able to do from main_work following:
from data_processing import data_selector as ds
and from data_processing following:
import data_selector
How do I achieve those two functionalities?

You may want to add your folder to the PYTHONPATH, this way python will know where to look for the modules you want to import.

You need the following structure
main_work/
data_processing/
__init__.py
data_extraction.py
data_selector.py
and in data_selector.py
from .data_extraction import connect_to_db
the . specifies that it should look in the same directory as the data_selector module

Related

ImportError: attempted relative import with no known parent package PYTHON

I am trying to import some functions and variables from the parent directory.
Here is what my dir looks like:
I want the import:
-- send_invoice func from bifatura_methods.py
-- start func from xml_generator.py
-- data_json_1, data_json_2 dicts from inputs.py
TO send_ticari_satis_invoice.py.
My working dir is:
/home/selman/PycharmProjects/15.0/custom_addons/Invoice_Integration/models/tests
When I tried for imports like:
from ..bifatura_methods import send_invoice
from ..xml_generator import start
from inputs import data_json_1, data_json_2
Output:
ImportError: attempted relative import with no known parent package
When I tried this:
from custom_addons.Invoice_Integration.models.bifatura_methods import send_invoice
from custom_addons.Invoice_Integration.models.xml_generator import start
from custom_addons.Invoice_Integration.models.tests.inputs import data_json_1,data_json_2
Output:
ImportError: cannot import name 'models' from 'odoo' (unknown location)
Any advice and help are welcome! Thank you community :)
since your \tests is essentially a module you're importing into, you should add an empty __init__.py inside \tests.
I personally would use explicit imports:
from models.bifatura_methods import send_invoice
from models.xml_generator import start
IMO, this would help you keep your sanity if you end up having a lot more submodules.
You can check the Odoo guidelines that we use from odoo.addons to import from Odoo addons (custom addons should also work)
Example:
from odoo.addons.Invoice_Integration.bifatura_methods import send_invoice

ImportError attempted relative import with no known parent

Based on some answers I try to be more specific.
I want to import the print and the models AND code in my main.py
I know the question gets asked a lot, but still I could not figure out whats wrong with my code!
I have a project directory like this
-project
--__init__py
--main.py
--print.py
--requests
--__init__.py
--models.py
--code.py
i want to import from print.py and * from requests
Therefore I tried to add these lines in main.py
from . import print
#or
import print
#for requests I tried
import os.path
import sys
sys.path.append('./requests')
from requests import *
all of those lines cause the same ImportError attempted relative import with no known parent ,
using Python 39
anyone an idea where the problem is?
I am very confused that this seems not to work, was it possible in older versions?
You should definitely not be doing anything with sys.path. If you are using a correct Python package structure, the import system should handle everything like this.
From the directory structure you described, project would be the name of your package. So when using your package in some external code you would do
import package
or to use a submodule/subpackage
import project.print
import project.requests
and so on.
For modules inside the package you can use relative imports. When you write
i want to import from print.py and * from requests Therefore I tried
it's not clear from where you want to import them, because this is important for relative imports.
For example, in project/main.py to import the print module you could use:
from . import print
But if it's from project/requests/code.py you would use
from .. import print
As an aside, "print" is probably not a good name for a module, since if you import the print module it will shadow the print() built-in function.
Your main file should be outside the 'project'-directory to use that as a package.
Then, from your main file, you can import using from project.print import ....
Within the project-package, relative imports are possible.

Import .py file to main file

I tried writing some commenly used function in a seperate file and import the same into mainApp file, but not able to use import.
I did find many questions regarding the this same question but, the solution was to keep the files in the same folder
I tried without .py as well, but the same error:
Can you please help me how can i fix this issue ?
No '.py'. Just import seperate
Try using this in mainApp.py:
from seperate import *
a()
where seperate.py looks like this:
def a():
print('hi')
Well, sorry, those two files need to be in the same folder. This is not a solution to your problem.
The syntax of a relative import depends on the current location as well as the location of the module, package, or object to be imported. Here are a few examples of relative imports:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Read more about Absolute vs Relative Imports in Python
In your case it should be:
from .seperate import a
Also check this question:
Importing from a relative path in Python
add your project directory into your path variable so that python know from where you want to import file

Import error -- No module Named myfile python2.7

Ok I am new to doing these imports in python and calling python files to be imported.
My directory structure looks like this.
dir1
|-__init__.py
|-file1.py
|-dir2
|---|-__init.py__
|---|-file2.py
|---|-file3.py
|---|-dir3
|-----|-__init.py
|-----|-file4.py
I want to import file 2 from dir3 level
This is what I did
from dir1.dir2.file2 import whatever
I get Import error: No module named dir1.dir2.file2
I tried a lot of options to set my python path, but I am not able to get the import working. It just might be an easy fix but am not able to see it. Your suggestions will be much appreciated.
If you're trying to load a file from dir3 level (e.g. file4), you could use relative paths:
from ..file2 import foo
though this is highly discouraged normally.

Python: Unit Testing Module and Relative Imports

Currently have the following file hierarchy:
\package
__init__.py
run_everything.py
\subpackage
__init__.py
work.py
work1.py
work2.py
\test
__init__.py
test_work.py
test_work1.py
My first question is regarding relative imports. Suppose in \subpackage\work.py I have a function called custom_function(), and I would like to test that function in test_work.py. For some reason I can not figure out how to make this import from one module to another. Trying from .. subpackage.work1 import custom_function() does not seem to work, and yields the error Attempted relative import in non-package Is there any way to resolve this?
2)
I would like to run all test files from run_everything.py with one function, would adding a suite() function in each test_work*.py file, which adds each unit_testing class to suite.addTest(unittest.makeSuite(TestClass)), and finally importing them into the top-level run_everything.py be the most conventional way in Python2.7?
Here is a hack*
Insert the path's to "subpackage" and "test" to your python path in run_everything using:
import sys
sys.path.insert(0, '/path/to/package/subpackage')
sys.path.insert(0, '/path/to/package/test')
And then, you can import all your files using vanilla imports in run_everything:
import work, work1, work2
import test_work, test_work1
*This won't permanently affect your PYTHONPATH.

Categories

Resources