Python/Django: import weirdness - python

I've the following project setup
....
├── lira
│   ├── __init__.py
│   ├── admin.py
│   ├── ajax.py
│   ├── authentication.py
│   ├── context_processors.py
│   ├── fencoder
│   │   ├── __init__.py
│   │   ├── encoder.py
│   │   ├── ffmpeg_commands.py
│   │   ├── frames.py
│   │   ├── utils.py
│   │   └── video.py
│   ├── models.py
....
And when I try to import from lira.fencoder import encoder I get an error ImportError: cannot import name encoder.
What is wrong with the above project setup though this morning it was the same and it worked?
Sultan

If you want to debug import problems, sometimes a simple launch of
python -v -m path/to/python/module/to_start
will help you, it will show you all the imports done by the python interpreter and help detect cyclic imports.

Related

Module not found error in pytest when tests folder contains __init__.py

My tests were working a few hours before. But suddenly tests located in root folder/tests are giving me ModuleNotFoundError: No module named 'tests.test_* I imported the tests.test_a in a top level python file and it worked. Any guesses why cant pytest load them?
I am asking this because this is weird that the test folder located inside root folder/some folder/tests also contains __init__.py and they work fine. I read this question and deleted the __init__.py and it worked. But it is confusing as to why the top level tests would not work.
The folder structure is along the lines of:
.
├── authenticate
│   ├── exceptions.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── exceptions.cpython-38.pyc
│   │   ├── __init__.cpython-38.pyc
│   │   ├── repository.cpython-38.pyc
│   │   └── use_case.cpython-38.pyc
│   ├── Readme.md
│   ├── repository.py
│   ├── tests
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   └── test_authenticate.py
│   └── use_case.py
├── tests
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── test_authenticate.cpython-38-pytest-5.4.2.pyc
│   ├── test_authenticate.py
As weird as it sounds, I had forgotten to add __init__.py in one of my packages. I don't know why it was causing tests in my top level directory to fail

Django, error importing view from another app

I'm trying to import a view from one app to another in my project.
When using this:
from ..from ..KnownLocation.views import KnownLocationView
I get the following error:
ValueError: attempted relative import beyond top-level package
When trying to use:
from triangulationapi.KnownLocation.views import KnownLocationView
It's raising the following error.
ModuleNotFoundError: No module named 'triangulationapi.KnownLocation'
my Project tree:
├── find_second_gdt
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── __pycache__
│   ├── second_GDT_finding
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── __init__.py
├── KnownLocation
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── __pycache__
│   ├── serializers.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── manage.py
├── requirements.txt.
└── triangulationapi
├── asgi.py
├── __init__.py
├── __pycache__
├── settings.py
├── urls.py
└── wsgi.py
And, what's the diffrence between using .. and project.app.view...
I thought it is the same up until now.
Try this:
from .KnownLocation.views import KnownLocationView

ModuleNotFoundError: No module named 'merlin.tradelogic' and problems around pyhv and deap modules

I have inherited some code that I am trying to execute unsuccessfully. For background I have needed to move the code from an old environment to a new one. The Python code is launched from a folder called mktdata.out via a shell script called ./launch.sh. This seems to be working okay. However the following error is returned to the terminal soon after the ./launch is executed:
[user#localhost mktdata.out]$ ./launch.sh
[user#localhost mktdata.out]$ /usr/local/lib/python3.6/site-packages/deap/tools/_hypervolume/pyhv.py:33: ImportWarning: Falling back to the python version of hypervolume module. Expect this to be very slow.
"module. Expect this to be very slow.", ImportWarning)
/usr/local/lib/python3.6/site-packages/deap/tools/_hypervolume/pyhv.py:33: ImportWarning: Falling back to the python version of hypervolume module. Expect this to be very slow.
"module. Expect this to be very slow.", ImportWarning)
Traceback (most recent call last):
File "strats/merlin.py", line 13, in <module>
File "strats/merlin/tradeopt.py", line 11, in <module>
ModuleNotFoundError: No module named 'merlin.tradelogic'
I think the output is telling me that a module called a script called tradelogic is missing from merlin. However I have looked at the old environment from which all the code was moved from and it's set up the same way as the new environment. So I am unsure as to what I need to do in order to get this code working.
I'm unsure if its related but there is also an error in the terminal output around deap and pyhv.py.
Edit
Please see below for the file structure under strats:
├── dao
│   ├── cythonlib
│   │   ├── cyrandom.cpython-35m-x86_64-linux-gnu.so
│   │   └── __init__.pyc
│   ├── fin
│   │   ├── algorithms.pyc
│   │   ├── __init__.pyc
│   │   ├── pnl.cpython-35m-x86_64-linux-gnu.so
│   │   ├── port_opt.pyc
│   │   └── stats.pyc
│   ├── __init__.pyc
│   ├── iotools
│   │   ├── blbmktdata.pyc
│   │   ├── blpwrapper.py.bak
│   │   ├── blpwrapper.pyc
│   │   ├── datamodel.pyc
│   │   └── __init__.pyc
│   ├── pair
│   │   ├── adfvalues.pyc
│   │   ├── cointegration.pyc
│   │   ├── __init__.pyc
│   │   ├── pairid.pyc
│   │   └── spread.pyc
│   ├── stock
│   │   ├── corpactions.pyc
│   │   ├── filter.pyc
│   │   └── __init__.pyc
│   └── utils
│   ├── concreteid.pyc
│   ├── config.pyc
│   ├── functools.pyc
│   ├── __init__.pyc
│   ├── itertools.pyc
│   ├── roll.pyc
│   └── rw.pyc
├── merlin
│   ├── cmdopt.pyc
│   ├── __init__.pyc
│   ├── instrument.pyc
│   ├── mktdata.pyc
│   ├── overview.pyc
│   ├── pair.pyc
│   ├── portfolio.pyc
│   ├── stratconfig.pyc
│   ├── tradega.cpython-35m-x86_64-linux-gnu.so
│   ├── tradelogic.cpython-35m-x86_64-linux-gnu.so
│   ├── tradeopt.pyc
│   ├── utils.pyc
│   ├── var.pyc
│   └── wrtconfig.pyc
├── merlin.pyc
└── merlin.sh

How to import python function from another file into django views

I am trying to call a function from a python file that is located outside the django project directory from views.py. I have tried importing the python file but running the django server says "no module named xxxx".
Tree structure is given below.
├── auto_flash
│   ├── __init__.py
│   └── test.py
└── fireflash
├── detection_reports
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── __pycache__
│   │   └── __init__.cpython-35.pyc
│   ├── templates
│   │   └── detection_reports
│   │   └── home.html
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── fireflash
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   └── settings.cpython-35.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── ui_cgi.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── __init__.py
└── manage.py
Project name here is "fireflash" and there is an app in that project named "detection_reports". There is another directory named "auto_flash" that has same location as "fireflash". What I want to do is to import test.py file from "auto_flash" in detection_reports.views and call a function from views. Importing like this "from auto_flash import test" throws error "no module named auto_flash".
I have tried all of the above mentioned solutions, but the cleaner solution was to append the path for auto_flash to syspath and the issue was resolved. Thanks to all of you for the efforts.
Move auto_flash to fireflash
In detection_reports add from auto_flash import test.py

Can not import class from custom django package

I am writing a custom Django module but I seem to have something wrong. I cannot import a class that lives in a certain file. I get the error
ValueError: Unable to configure handler 'admins': Cannot resolve 'myPackage.handlers.MyHandlerClass': No module named handlers
This is the directory structure. I believe I can import views and models with no problem.
myPackage
├── CHANGELOG.rst
├── myPackage
│   ├── handlers .py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── myPackage.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── MANIFEST.in
├── README.rst
├── requirements.txt
└── setup.py
There is a space in the filename of handlers .py, so python can't find a module names handlers. Obviously the easiest fix is to correct the filename, but for anyone actually wanting a space in the filename, import name with spaces is a syntax error, so the only way to import such a name is using __import__. But this is really a very bad idea.

Categories

Resources