I have installed asterisk version 1.4.44 and using Python for agi scripts. I have context "9999" than I trying to call while executing this I am getting below errors.
is there any dependency to be installed to get this working?
-- Launched AGI Script /var/lib/asterisk/agi-bin/incident/SetCommonVariables.py
Traceback (most recent call last):
File "/var/lib/asterisk/agi-bin/incident/SetCommonVariables.py", line 5, in <module>
from asterisk import agitb
ImportError: No module named asterisk
-- AGI Script incident/SetCommonVariables.py completed, returning 0
-- Executing [s#IncidentInitiation:4] Goto("SIP/9999-00000000", "CheckAuthorization|1") in new stack
-- Goto (IncidentInitiation,CheckAuthorization,1)
-- Executing [CheckAuthorization#IncidentInitiation:1] AGI("SIP/9999-00000000", "incident/CheckAuthorization.py") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/incident/CheckAuthorization.py
Traceback (most recent call last):
File "/var/lib/asterisk/agi-bin/incident/CheckAuthorization.py", line 7, in <module>
from asterisk import agitb
Edit 1 :
Asterisk 1.4.44
CentOS 6.5(Final)
Python version (2.4,2.6.6,2.7)
Edit 2 :
i have already "from asterisk import agitb" in above issues file here code snipes of file
File "/var/lib/asterisk/agi-bin/incident/SetCommonVariables.py
#!/usr/bin/python
from IncidentConstants import *
import sys # system stuff
from asterisk import agitb
agitb.enable(display = False, logdir = '/var/log/asterisk')
# Global variables
from asterisk.agi import * # our agi stuff
agi = AGI()
agitb.enable(agi, False, '/var/log/asterisk')
in every file i have above content
My guess is that CGI uses the system python. Not the python that has the Asterisk module.
Check that the failing CGI script does not have
#!/usr/bin/python
And if does, change this to use the proper Python.
For each python you have try doing:
from asterisk import agitb
Then you will find which one has asterisk
Related
My directory looks like this
When I start directly with PyCharm it works.
But when I try to start the script with a commandline I get this error messsage
> python .\PossibilitiesPlotter.py
Traceback (most recent call last):
File "C:\Users\username\PycharmProjects\SwapMatrixPlotter\possibilitiesplotter\PossibilitiesPlotter.py", line 7, in <module>
from plotterresources.PlotterProps import PlotterProps
ModuleNotFoundError: No module named 'plotterresources'
This is how the import looks from my main class PossibilitesPlotter.py
import sys
sys.path.append("plotterresources/PlotterProps.py")
from csv import reader
from pathlib import Path
from plotterresources.PlotterProps import PlotterProps
from possibilitiesplotter.PossibilitiesGraph import PossibilitiesGraph
from possibilitiesplotter.PossibilitiesModel import PossibilitiesModel
class PossibilitiesPlotter:
As a workaround, add the following line to PossibilitesPlotter.py:
sys.path.append("../plotterresources/PlotterProps.py")
This will add the directory one level above the commandline pwd to the PATH variable. So this is always relative to the location of the calling script/shell.
Thus in general:
NEVER append to the PATH/PYTHONPATH variable from within modules. Instead restructure your module. For more details, take a look at the documentation on Packaging Python Projects
my files are look like this
-root/
-main.py
-nyaizhel_includes/
-includemain.py
-commands/
-astolfo.py
-naruto.py
...
from main.py, i import includemain.py
from nyaizhel_includes import includemain
it works, includemain.py gets imported,
from includemain.py i import commands
from commands import astolfo
from commands import narutobanner
from commands import rgirl
...
includemain.py gets included but astolfo isn't get included, why?
console log
heroku[main.1]: State changed from starting to up
2021-04-13T09:42:02.840988+00:00 app[main.1]: Traceback (most recent call last):
2021-04-13T09:42:02.841010+00:00 app[main.1]: File "/app/main.py", line 42, in <module>
2021-04-13T09:42:02.841140+00:00 app[main.1]: from nyaizhel_includes import includemain
2021-04-13T09:42:02.841141+00:00 app[main.1]: File "/app/nyaizhel_includes/includemain.py", line 1, in <module>
2021-04-13T09:42:02.841258+00:00 app[main.1]: from commands import astolfo
2021-04-13T09:42:02.841262+00:00 app[main.1]: ModuleNotFoundError: No module named 'commands'
2021-04-13T09:42:02.950174+00:00 heroku[main.1]: Process exited with status 1
2021-04-13T09:42:03.023470+00:00 heroku[main.1]: State changed from up to crashed
You didn't list your __init__.py file in your example, so make sure you add those so these are recognized as standard packages (although it seems you may already have).
The source of your error is likely that you're trying to do a relative import but specifying an absolute import:
If you use from .commands import astolfo (notice the period to signify relative import), that should resolve your issue. It is recommended to use absolute imports as per the PEP 8 Style guide however. In your case this would be from nyaizhel_includes.commands import astolfo, assuming nyaizhel_includes is your root package here.
See the documentation on Python packages for more information.
TLDR : using QGIS, I'm trying to develop a python plugin to update a database. Unfortunately I get immediatly an error : Traceback (most recent call last):
File "", line 1, in
NameError: name 'QgsDataSourceUri' is not defined
More detailed:
I work on QGIS2.18 to develop a plugin to update some data located on a postgres database.
for this, I want to use this kind of code:
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtCore import QSettings
from PyQt4.QtCore import QSettings
from qgis.core import QgsVectorLayer, QgsDataSourceURI
uri = QgsDataSourceUri()
# set host name, port, database name, username and password
uri.setConnection(hote_IP, "5432", base_de_donnee, utilisateur, mot_de_passe)
# set database schema, table name, geometry column and optionally
# subset (WHERE clause)
#uri.setDataSource("public", "roads", "the_geom", "cityid = 2643")
uri.setDataSource("", sql, "geom", "", "gid")
vlayer = QgsVectorLayer(uri.uri(), zapm, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)
(I got the code from the net, I'll adapt it later on)
My problem : when I try to run this code on the Python console of QGIS, I immediatly get the error
Traceback (most recent call last):
File "", line 1, in
NameError: name 'QgsDataSourceUri' is not defined
even when I only run the import and the line uri = QgsDataSourceUri(), I get the same error message.
I have not been able to find out how to correct this issue.
problem of installation of QGIS? of python? bad imports?
Config:
qgis 2.18.20
python 3.6.5
If anyone has an idea on how to solve this, I would be really glad.
Thanks,
Erwann
You simply are using the wrong class name. It should be uri = QgsDataSourceURI() instead of uri = QgsDataSourceUri() because you've imported QgsDataSourceURI and not QgsDataSourceUri
QGIS and QT Python classes are case-sensitive. You can confirm the exact syntax looking at QGIS 2.18 related API.
so I had the following file structure:
crawler.py
sources/source1.py
sources/source2.py
crawler.py would import each source and the defined function loadArticles in there. However the function loadActicles is very similar for each source. So now I changed everything to
crawler.py
sources/source1.py
sources/source2.py
functions.py
I copied the function loadArticles to functions.py imported it in each source via "from functions import loadArticles" and am running it from the source via:
loadArticles(source, sourcetype, language, url, dateFormat, textFormat)
The imports contain some configuration data and the two differences in the function, the format of the date (dateFormat) and the expression used to scrape the text (textFormat), therefore the arguments.
From crawler.py I am now running:
source1.loadArticles()
source2.loadArticles()
which exits with an error message that doesn't make sense:
Traceback (most recent call last):
File "crawler.py", line 1, in <module>
from sources import source1
File "C:\***\app\sources\source1.py", line 1, in <module>
from functions import functions
File "C:\***\app\functions.py", line 27
found = datetime.now().strftime("%d.%m.%Y - %H:%M")
^
SyntaxError: invalid syntax
When I start pdb (used it for the first time) following comes up:
PS C:\***\app> python -m pdb crawler.py
> c:\***\app\crawler.py(1)<module>()
-> from sources import source1
(Pdb) l
1 -> from sources import source1
2 import sqlite3
3
4 source1.loadArticles()
[EOF]
(Pdb) n
SyntaxError: invalid syntax
I fail to understand where to start looking for the problem. Might have something to do that I am importing a file that doesn't include a function definition? Maybe also that I am calling "source1.loadArticles()" but should rather just run the file?
I have three database-related classes that I want to combine into a package, in the following structure:
adodb_pyodbc /
__init__.py # empty
PyConnection.py
PyRecordset.py
PyField.py
This package is in my Lib/site-packages folder.
In an earlier iteration of this attempt, I did not use the "Py" prefix, and I got an error complaining that module.__init__() takes only two arguments, and three were being passed into it. Someone suggested that the name "Recordset" might be conflicting with something else, so I changed it.
The classes work when the files are in the same folder as the project that is using them. In that case, I can just use:
PyRecordset.py:
from PyConnection import PyConnection
from PyField import PyField
class PyRecordset: pass
DerivedSet.py
from PyRecordset import PyRecordset
class DerivedRecordset(PyRecordset): pass
But the same files don't work when they are located inside a package. My test program begins with this line:
from adodb_pyodbc import PyConnection as Connection
And when I run it I get this error message:
C:\Python35\python.exe "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py"
Traceback (most recent call last):
File "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py", line 8, in <module>
from Level3_CoilsSet import Level3_CoilsSet
File "C:\Customers\Nucor Crawfordsville\Scripts\64 bit\Testing\Level3_CoilsSet.py", line 1, in <module>
from adodb_pyodbc import PyRecordset as Recordset
File "C:\Python35\lib\site-packages\adodb_pyodbc\PyRecordset.py", line 9, in <module>
from PyConnection import PyConnection
ImportError: No module named 'PyConnection'
But when editing PyRecordset.py inside PyCharm, it appears to be able to find the PyConnection.py file.
I tried using relative addressing inside PyConnection.py:
from . import PyConnection
from . import PyField
But that puts me back to the __init__() error:
C:\Python35\python.exe "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py"
Traceback (most recent call last):
File "C:/Customers/Nucor Crawfordsville/Scripts/64 bit/Testing/cpsa_simulator.py", line 8, in <module>
from Level3_CoilsSet import Level3_CoilsSet
File "C:\Customers\Nucor Crawfordsville\Scripts\64 bit\Testing\Level3_CoilsSet.py", line 3, in <module>
class Level3_CoilsSet(Recordset):
TypeError: module.__init__() takes at most 2 arguments (3 given)
How am I supposed to do this?
Thanks very much for your help. In the meantime, I'm going to take those files out of the package and put them back in my test project. I've wasted far too much time on this question.
When you one to use PyConnection from outside of the package you have to either import it from the module where it was defined:
from adodb_pyodbc.PyConnection import PyConnection as Connection
Or, more conveniently, import it in the package init file adodb_pyodbc/__init__.py:
from .PyConnection import PyConnection
And then, from the outside, you can just do:
from adodb_pyodbc import PyConnection as Connection