I am trying to implement following hierarchy.
My final goal is myscpt.sh should run from any folder
But I am getting import errors.
What is the best approach to implement such type of hierarchical architecture?
FooSoft
|
|-- foo/
|
|-- test.py
|
|-- common/
|
|-- utils/
|
|-- api/
|
|-- scripts/
|-- myscript.py
|
|-- bin/myscpt.sh
|
|-- etc/foo.conf
bin/myscpt.sh
/usr/bin/python /path/FooSoft/foo/script/myscript.py /path/FooSoft/etc/foo.conf
foo/script/myscript.py
from ..common import *
from ..utils import *
from ..test import *
.
.
<Doing some stuff>
I am using .. import in most of modules to avoid absolute path.
Typically I resolve import errors by always using the package root as a reference.
First, I would include a setup.py file in the project root, and add in a minimal setuptools wrapper.
python setup.py develop
Then you don't have to worry about where you run the script from. Your imports become as follows:
from foo.common import *
from foo.utils import *
from foo.test import *
Explicit relative imports with leading dots like from ..common import anything can be used only from a code that has been imported as a submodule of the package e.g. foo.scripts, but not from the the code imported as __main__ script, even if the script path contains .../foo/scripts/.... The main script must use absolute imports or it can be used as module by something like python -c "from foo.scripts import myscript; myscript.run()". (install the foo or use PYTHONPATH=/path/FooSoft). See Intra-package References in the docs or a similar question.
Related
there is my project hierachy:
#__init__.py are empty files
folder
|-- globalFunctions.py
|-- __init__.py
|-- monitor
|-- file.py
|-- __init__.py
I'm tring to import functions from globalFunctions.py, when I'm in the file.py file.
I have tried to import it with
from .. import globalFunctions
but I'm getting
ImportError: attempted relative import with no known parent package
Do you know how to make it work?
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?
I'm making a lambda function in python.
Here is the current structure of my project.
lambda/
|-- datas/
| |-- d.json
|
|-- package_name/
| |-- __init__.py
| |-- lambda_function.py # this is using d.json
| |-- a.py # this is some classes used on lambda_function
| |-- b.py # this is some basic time functions that a.py need
| |-- utils.py
|
|-- tests/
| |-- __init__.py
| |-- test_main.py
|-- setup.py
|-- README
I have some issues with imports.
# lambda_function.py files
from a import *
from utils import *
# a.py files
from b import *
# b.py files
from a import *
It's working locally, but not in aws lambda console. To make it working in aws lambda console, I need to change this :
# lambda_function.py files
from package_name.a import *
So my first question is : why ?
And my second question is : If I want to import package_name/a.py in tests/tests_main.py, what should I do ?
I tried
from a import *
from package_name import *
But it doesn't work
I'm still a little bit lost with how imports work, even after reading what internet had to say about it.
Moreover, I'm not sure about my project files structure (but it's an other subject I guess)
use
# lambda_function.py files
from .a import *
from .utils import *
# a.py files
from .b import *
# b.py files
from .a import *
this will tell that from the current directory read the module and import all
functions, classess, variables.
You can try adding your current working directory path to the python libraries path
import sys
sys.path.append('../')
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.
I am having problems importing anything into my testing files that I intend to run with py.test.
I have a project structure as follows:
/ProjectName
|
|-- /Title
| |-- file1.py
| |-- file2.py
| |-- file3.py
| |-- __init__.py
|
|-- /test
| |-- test_file1.py
I have not been able to get any import statements working with pytest inside the test_file1.py file, and so am currently just attempting to use a variable declared in file_1.py and print it out when test_file1.py is run.
file1.py contains:
file1_variable = "Hello"
test_file1.py contains:
import sys
import os
sys.path.append(os.path.abspath('../Title'))
import file1
def test_something():
assert file1.file1_variable == "Hello"
print(file1.file1_variable)
The import statement from my testing file is taken from https://stackoverflow.com/a/10272919/5477580 which works by editing the PYTHONPATH variable. This allows me to run the test_file1.py script and successfully execute the print statement.
However, attempting to run py.test from the /ProjectName directory gives me an error saying ImportError: No module named 'file1'
Is there some way in which I can structure things better so that pytest will be possible? Do I need to add something to my __init__.py file?
No you don't need to add anything to __init__.py. This file tells python to treat the parent directory as module which can be imported as described here.
Just add it to your Title directory and import it like
from ..Tiltle.file import file1_variable
here we are moving 1 hierarchy above in the directory just like cd ..
Note .. is for reaching the Title package from test directory. If you need to run your file from ProjectName directory you will have to do
from Tiltle.file import file1_variable