Python project structure error while importing - python

I just started coding in python and I encountered some errors. My text editor (vscode) doesn't show any error while importing the module but whenever I run the code I have encountered it.
So basically, my directory tree looks like this
lib/
|
|-- core/
| |-- module.py
| |-- __init__.py
|
|
|-- python/
|-- server.py
|-- worker.py
|-- __init__.py
When I import module.py from python/server.py and use
import lib.core.module
I got an error: No module name lib.core. I tried adding "." but it doesn't work

Try:
from .lib.core.module import *

Related

What's the best practice to make a directory an importable package?

I created a few utility functions, all put into a folder, and later I created some scripts which import the functions in that folder, please see the following the structure of files,
root
|-- myutils (folder with util python files)
| |-- __init__.py (empty)
| |-- utils1.py
| |-- utils2.py (utils1 is imported by utils2)
|
|-- myscripts
| |-- hello.py (try to import utils1 and utils2)
Now the problem is I cannot import utils1/utils2 inside hello.py, here is what I tried
# This is hello.py
import sys
sys.path.append("root") # so that we can search root and find myutils
import myutils # import myutils, this is OK and no errors
x = myutils.utils1.foo() # ERROR: module 'myutils' has no attribute 'utils1'
How could I fix the error and what's the best practice for my use-case here?

How to import modules in Python framework with multiple subfolder

My question might have discussed in earlier posts too, but couldn't get proper answer applicable for my scenario. Hence posting it as a new query.
I have multiple sub-folders where module tries to import modules present across multiple subfolders.
Folder Structure:
main
|-- lib
| |-- lib1.py
| |-- lib2.py
| `-- lib3.py
|-- common
| |-- lib4.py
|-- tests
| |--folder1
| |-- script1.py
| |-- folder2
| |-- script2.py
| |--scriptA.py
| `--scriptB.py
Use case/Requirements:
script1 & script2 import functions from module lib1.py.
lib1 wants to import functions from lib2.py & lib3.py
lib4.py import funtions from lib1 & lib2
I tried adding blank __init__.py in root folder (main) and the all other subfolders. But couldn't get this working. Ending up with 'ModuleNotFound' error.
You need blank __init__.py files, not init.py. See the python documentation for more info.

PyGTK Builder add_from_file() different path approach with PyCharm

Well, in my app/view.py there's the a function call to GTK.Builder().add_from_file("AppView.glade"), however I observed it has been interpreted differently depending of how I execute the code.
project
|-- app
| |-- __init__.py
| |-- model.py
| |-- view.py
| |-- ...
|
|-- test
| |-- __init__.py
| |-- model.py
| |-- ...
|
|-- ui
|-- AppView.glade
When I regularly execute (python3 -m app) or test (python3 -m unittest test) the project, the add_from_file function expect the relative path from project root, that is ui/AppView.glade.
However, when I run or debug from PyCharm project's test or execution it expect a relative path from the file is calling the function, for this case `../ui/AppView.
Could I change this PyCharm behavior?

Python unittest import problems

I'm having a tough time understanding packages, and specifically how to use unittest with packages. I looked at this question () but the correct answer to that question didn't solve my problem. I have the following structure:
model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
With the following files/imports:
model/__init__.py:
import model.exceptions.exceptions
import model.boardmodel
model/exceptions/__init__.py:
contains nothing
model/test/__init__.py:
contains nothing
imports inside boardmodel.py::
from model.exceptions.exceptions import ZeroError, OverlapError, ArgumentError, ProximityError
imports inside test_boardmodel.py:
import unittest
from model.boardmodel import Board, Ball, Wall
from model.exceptions.exceptions import ProximityError
I place myself in the model-directory and I run python -m unittest test.test_boardmodel.py. I get the following message:
ERROR: test_boardmodel (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_boardmodel
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
module = __import__(module_name)
File "/Users/sahandzarrinkoub/Documents/Programming/pythonfun/BouncingBalls/balls/src/model/test/test_boardmodel.py", line 3, in <module>
from model.boardmodel import Board, Ball, Wall
ModuleNotFoundError: No module named 'model'
I'm a bit lost with how the imports work and what location the modules/packages are looked for when an import statement is executed. Why isn't model found?
I shall add that if I remove model. from all the imports listed, the tests work, but I can't use the package from "outside" anymore:
src
|-- visual.py
|
|-- model
|-- __init__.py
|-- boardmodel.py
|
|-- exceptions
| |
| |-- __init__.py
| |-- exceptions.py
|
|-- test
|-- __init__.py
|-- test_boardmodel.py
inside visual.py:
import model
from model.boardmodel import Board
I was facing the same issue, being able to import some modules from several files, but not from a test file, so I saw this solution:
If you have test/my_test.py, tests should be run as:
python -m test.my_test
After that, I imported what I wanted and got no errors.
Try adding the following above your import statements:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
I believe the standard package structure is
myproject
├── myproject
├── tests
└── scripts
If you want to run the test without installing the package, run them from the top myproject folder so that import myproject will succeed in your tests. (Similarly for the scripts.) For this to work, use absolute imports or explicit relative imports in myproject.

not able to import a file from an app to another

I have a django project with srtucture like this:
main_project
----main_project
<------libs
<<---------exceptions.py
----project_a
----project_b
In the views.py of project_a I am trying to import a folder named libs of main_project and a file from libs called exceptions.py, but I am getting the error
ImportError: No module named libs.exceptions
My code is :
from main_project.libs.exceptions import (
APIException400,
APIException405,
APIException403,
exception_handler_dispatcher,
)
Can someone tell me what am I missing here?
With reference to https://stackoverflow.com/a/31407131/5080347 answer I even tried :
from main_project.main_project.libs.exceptions import (
APIException400,
APIException405,
APIException403,
exception_handler_dispatcher,
)
but doesn't work.
It seems like you forgot to add __init__.py to libs directory.
The __init__.py is used to initialize Python packages. Check the documentation to better understand how things are working.
Your structure should looks as follow:
project/
|
|-- __init__.py
|
|-- module_a/
| |-- __init__.py
| |
| |-- file1.py
| |
| |-- file2.py
|
|-- module_b/
| |
| |-- __init__.py
| |
| |-- file1.py
| |
| |-- submodule/
| | |--__init__.py
| | |
| | |-- file1.py
When you import using from main_project.libs.exceptions, python expects that main_project is the package and libs and exceptions are submodules. So there must be a file named __init__.py in those directories. The init.py files are required to make Python treat the directories as containing packages. For further reading please refer to here.

Categories