Importing modules, when several (sub-)folders have same names - python

When the project directory and a subfolder has the same name, I get an import error when running pytest.
I have tried to rename the subfolder and then there is no problem.
For example with the following folder structure:
project/
project/
tools.py
foo/
foofile.py
bar/
unittest/
test_foofile.py
In project/foo/foofile.py we try to import classes from project/project/tools.py:
from project.tools import ClassA
When then running python -m pytest from the first level directory project/ i get the following error:
ImportError: cannot import name 'ClassA' from 'project.tools' (unknown location)
Is there a way to say to the interpreter that it should look into the subfolder project?

Related

Import .py file from parent directory

I wanted to import a file from another folder into another file in the same root folder.
Directory list:
ROOT/
resource/
console_log/
__init__.py (empty)
file1.py
libs/
__init.py__ (empty)
function.py
__init__.py (empty)
I tried using this code in resource/console_log/file1.py:
from resource.libs.function import function_name
It just doesn't work. Returns this error:
No module named 'resource'
If I use this code instead
from ..libs.function import function_name
It gives me this:
attempted relative import with no known parent package
How can I solve?
Is there any way to fix it without using the sys?
EDIT:
I "fixed" the problem by moving all the files directly to the libs folder thus removing the resource folder. Except that if from libs/file1.py I want to import a function present in the main.py file I get the same error
New folder structure:
ROOT/
libs/
__init__.py
file1.py
file2.py
function.py
logs/
debug.log
__init__.py
main.py
If I use this code in the libs/file1.py file, it works correctly (only if I start it from the main.py file)
# file1.py
from libs.function import function_name
But if in libs/file2.py I want to recall a variable present in the main.py file, it returns me an error
# file2.py
from main import data
# ERROR
No module named 'main'
If he doesn't give me this error he gives me another one
attempted relative import with no known parent package
I think your PYTHONPATH isn't set correctly. You can either export it or run your program with it set as expected for the single command. The following will set the PYTHONPATH for the single command execution:
PYTHONPATH=./:$PYTHONPATH python resource/console_log/file1.py

Importing module from different folder using relative path and __init__.py files

I'm on Windows 10, using Python 3.8.3. Here is my simple folder structure:
root_project_folder\
__init__.py # Empty file
project_1_folder\
__init__.py # Empty file
foo.py
project_2_folder\
__init__.py # Empty file
bar.py
Within foo.py I have the following code:
from ..project_2_folder.bar import Test
Test.test_method()
Within bar.py I have the following code:
class Test:
#staticmethod
def test_method():
print("Test successful")
When I run foo.py, I receive the following error:
ImportError: attempted relative import with no known parent package
I've read a few threads about importing using relative paths. Here are the threads for reference:
Python3 relative imports failing in package
Importing files from different folder
What is init.py for?
I did not append any directories to sys.path. I also did not modify PYTHONPATH either, such as adding directories as an environment variable. From what I have read, I don't need to do any of that. Would I have to add something to one of the __init__.py files?
Any help would be appreciated.

Having trouble making relative imports in sibling directories

Im having trouble importing from sibling directories.
I would like to set up my package so that all of these aspects simultaneously work.
Here is my module setup:
package/
__init__.py
code/
__init__.py
model.py
helper.py
notebooks/
__init__.py
notebook1.ipynb
Imports in model.py:
from helper import *
When running from the command line in package/ I set PYTHONPATH=. and I can run model.py
Imports in notebook1.ipynb:
First scenario:
sys.path.insert(0, os.path.abspath('..'))
from code.model import MetaModel
Results in: ModuleNotFoundError: No module named 'code.model'; 'code' is not a package
Second scenario:
sys.path.insert(0, os.path.abspath(os.path.join('../code')))
from model import MetaModel
This works. Why can I not use the first scenario.

Gitlab CI Python run test - ModuleNotFoundError: No module named xxx

I saw a lot of questions about importing modules error but I could not manage to solve the problem with Gitlab CI pipeline.
My project structure:
├───config
├───docs
├───src
__init__.py
│ ├───dataset
__init__.py
│ ├───exceptions
│ ├───resources
│ └───utils
__init__.py
└───tests
__init__.py
└───resources
__init__.py
I would like to run tests using pytest.
I invoke this command python -m pytest -p no:cacheprovider or using unittest
'python -m unittest discover -v' from root directory and also tried to invoke from test directory. In both cases I have a problem with importing class from dataset module. What's interesting, I have two tests file.
First file imports:
import os import unittest
from src.utils.FileManager import FileManager
Second imports:
from src.dataset.DatasetHelper import DatasetHelper
The first file is passing but the second is failing with error:
from dataset import DatasetHelper ModuleNotFoundError: No module
named 'dataset'
So the thing is that other modules like utils from src are imported correctly, only the dataset has a problem. I am struggling with this a few days and I completely out of ideas. I also tried to change instead of from dataset to from src.dataset. It didn't work. I can run tests in PyCharm by clicking right mous button and just run tests but not on CI environment.
What I tried:
Add modules to $PYTHONPATH like
sys.path.insert(0, "/builds/USER/PROJECT/src/dataset")
sys.path.insert(0, "/builds/USER/PROJECT/src")
sys.path.insert(0, "/builds//USER/PROJECT/tests")
The content of PYTHONPATH before adding it was:
Current $PYTHONPATH: ['/builds/USER/PROJECT/config', '/usr/local/lib/python36.zip', '/usr/local/lib/python3.6', '/usr/local/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/site-packages']
The first module in list is config because I run script from this directory to add above modules to path. Doesn't Help
Run test command from root directory and add prefix src to imports in tests directory. Doesn't Help
from dataset import DatasetHelper
ModuleNotFoundError: No module named 'dataset'
Either in src.__init__ or more proabably in src.dataset.__init__ there is the import statement from dataset import DatasetHelper. You have to rewrite it as from src.dataset import…
You can try using a relative import in your __init__.py file that's inside the tests directory.
The syntax depends on the current location as well as the current location of the module,package, or object you're trying to import. Here are some examples:
from .some_module import some_class
from ..some_package import some_function
from . import some_class
Source: https://realpython.com/absolute-vs-relative-python-imports/

Imports don't work using java like packaging

I am developing a project which has a directory structure like this:
projectName
package1
module1.py
package2
module2.py
I am developing in Eclipse-PyDev and in module1.py i import module2 using this statement:
import projectName.package2.module2
when i want to execute module1 I do:
cd projectName
python package1.module1
but it can not import module2 giving this error:
ImportError: No module named projectName.package2
I'm new to Python and want to know the best practices for packaging and importing.
what i'm doing know is to put module2 in projectName directory and to remove projectName from all imports. This way i can execute the modules. But i have to change the file everytime i copy the development program into the deployment server.

Categories