How to find the Django package - linux

I work with ubuntu 12.04 and I want to know whether Django is currently installed. I tried it like this at first:
$ python
>>> import django
>>> django
but I want to do it in the terminal. I don't want to use dpkg because the user might have installed it with python-pip. I already tried find and whereis but that didn't work either.

Write simple script using python:
#!/usr/bin/python
import sys
import os
try:
import django
print "found"
print "path={0}".format(os.path.dirname(django.__file__))
sys.exit(0)
except ImportError:
print "not found"
sys.exit(0)
then run script and check for exit code
$ python script.py
$ echo $?

You should have a look at this question: [How do I find the location of Python module sources?][1]

Related

How to pass script path to Python 3.9 on Windows 10?

From Windows 10 command line , Python 3.9.13, in directory:
C:\Users\foo.bar\wks\df\data
when trying to run :
python unilm\\layoutlm\\examples\\seq_labeling\\preprocess.py --data_dir FUNSD\\training_data\\annotations --data_split train --output_dir data --model_name_or_path microsoft/layoutlm-base-uncased --max_len 510
I get this error:
python: can't open file 'C:\Users\foo.bar\wks\df\data\unilm\layoutlm\examples\seq_labeling\preprocess.py': [Errno 2] No such file or directory
Notwithstanding the path C:\Users\foo.bar\wks\df\data\unilm\layoutlm\examples\seq_labeling\preprocess.py' is correct, really exists.
What's wrong?
There are a few ways to do this:
Use the -m flag:
python -m my_script.py
Use the sys.argv list:
import sys sys.argv.append('my_script.py')
Use the os.environ dictionary:
import os os.environ['PYTHONPATH'] = 'my_script.py'
Use the site module:
import site site.addsitedir('my_script.py')
The problem was in a typo in one of the words of the long path to a script. Thanks for trying to help and sorry for confusion!

Scapy not working with Python 3.6. Import problems with Python?

I am trying to test a simple code that will inspect a .pcap file using scapy. My OS is Ubuntu 18.04 and my Python version is 3.6.
However when I do: from scapy.all import * I get the error message ModuleNotFoundError: No module named 'scapy.all'.
When I change this to be from scapy import *, I get an error later in my code when I try and use the scapy sendrecv sniff function. I get the error NameError: name 'sniff' is not defined. Note if I switch from sniff() to rdpcap() I get the same error but its now "rdpcap is not defined".
I've read through a bunch of previous stack overflow answers and nothing is helping. My python script name is "pcap_grapher.py", so the issue is not the script name being scapy.py.
When I run pip3 install scapy in the terminal I get the message Requirement already satisfied: scapy in /home/vic/.local/lib/python3.6/site-packages (2.4.4)
I've also tried running pip3 install --pre scapy[basic] as the docs recommend here: https://scapy.readthedocs.io/en/latest/installation.html but it didn't help.
Any advice would be greatly appreciated. I am super stuck.
First off, you will never be able to import sniff() from scapy, since it is within the submodule scapy.all. There are two ways I can think of to fix this:
Since you're on Ubuntu, try running sudo apt-get install python3-scapy and see if it lets you run from scapy.all import sniff now.
Check the path that python is currently pulling from by typing the following in terminal:
$ which scapy
/location-of-scapy-on-your-system
Then run
$ python
>>> import os
>>> scapy_path = "/location-of-scapy-on-your-system"
>>> if not scapy_path in os.sys.path: os.sys.path.append(scapy_path)
That should hopefully fix things. If not, it's possible you installed the wrong version of scapy: you can check this by running
$ python
>>> import scapy
>>> print(scapy.__version__)

How to use Asterisk AGI with python3?

Using Asterisk 16.2.1 my AGI script (at the bottom) works with python2 #!/usr/bin/env python2, but not with python3 #!/usr/bin/env python3.
I do not even get as far as agi.verbose("python agi started") (with python3), so I assume it has something to do with the AGI import or initialization agi = AGI()
Having used agi set debug on does not really help, the only info I see is
Launched AGI Script /home/.../asteriskAgi.py
-- <SIP/..-00000002>AGI Script /home/.../asteriskAgi.py completed, returning 0
As it works with python2, but not 3 I have also installed pyst3 from https://pypi.org/project/pyst3/ , but it did not help (it does not work with or without pyst3 installed).
Q: Any idea how to configure asterisk for python3, or how to find the root cause?
Any chance to get more detailed log information of where the script actually fails_
#!/usr/bin/env python3
import sys
import rpyc
from asterisk.agi import AGI
agi = AGI()
agi.verbose("python agi started")
aCallerId = agi.env['agi_callerid']
aType = agi.env["agi_type"]
agi.verbose("XXXXXXXXXXXXXX call from %s" % aCallerId)
agi.verbose(sys.executable)
l = [aCallerId, aType]
agi.verbose("XXXXXXXXXXXXXX l")
c = rpyc.connect("localhost", 18861)
c.root.asteriskCall(l)
Even this minimalistic version does not work with "3"
#!/usr/bin/env python3
import rpyc
from asterisk.agi import AGI
agi = AGI()
agi.verbose("python agi started")
eventually solved by:
uninstalled pyst3 and
forced a re-install of pyst2 like pip3 install --upgrade --force-reinstall pyst2. No idea what went wrong in the first place.
Your minimalistic version works for me(with pyst2 installed via pip)
Check permission and installed packages. Also ensure that your asterisk running under environment which able find python3 and packages installed.

ImportError: No module named flask using MAC VSCODE

ScreenGrab of error when trying to import db I created
You either need to install Flask from pip, or something similar, or Python is not able to find the location of Flask if it is, in fact, installed.
At your Python prompt, try this to see where it is looking for libraries:
>> import sys
>> print (sys.path)

ImportError with from . import x on simple python files

I wanted to port some code from python 2 to python 3 and it failed on an import error. So I tried to get rid of the porting itself and focus on the import by creating 2 basic python files to test with. However I can't even get those to work.
So I have 2 files
test.py:
print('Test works')
and test2.py:
from . import test
The result however is this error in Pycharm:
ImportError: cannot import name 'test' from '__main__'
(C:/Users/Username/test2.py)
In Ubuntu Shell:
Traceback (most recent call last): File "test2.py", line 1, in
from . import test1 SystemError: Parent module '' not loaded, cannot perform relative import
How can I solve it?
This "folder structure matters" is a large problem in python3. Your folder structure should NOT matter when coding, but should be referenced properly.
I've just resorted to using if/else depending on if ran locally or as part of a module:
if __name__ == "__main__": # Local Run
import test
else: # Module Run, When going production - delete if/else
from . import test
In Python3
test2.py:
import test
test.py:
if __name__ == "__main__":
print('Test works')
If you want to print "Test works" in other file
test2.py:
import test
test.main()
test.py:
def main():
print('Test works')
if __name__ == "__main__":
main()
The folder structure matters. You didn't name your module; I will call it foo. Put things in the right place:
$ mkdir foo
$ touch foo/__init__.py
$ mv /some/place/{test1,test2}.py foo/
Notice that python -c 'import test' works already, even before you wrote your test.py file. To avoid confusion I recommend naming your file test1.py.
The way you call your code matters. Verify that . dot is in sys.path:
$ export PYTHONPATH=.
$ python -m foo.test1
Alternatively this should work, if you prefer:
$ python foo/test1.py
EDIT:
I answered question #1, and OP now asks question #2, about this diagnostic:
ImportError: cannot import name 'test' from 'main' (C:/Users/Username/test2.py)
Please organize your files in the proper structure. You need to put test2.py within a foo/ directory (or whatever you care to call it). You need to create an empty foo/__init__.py file, as that is important for the import machinery.
Also, the PYTHONPATH env var in the calling environment matters. In addition to the command line environment, you have now introduced a PyCharm environment. Take care to configure PyCharm correctly for your project. Click on Preferences -> Project Structure and ensure that foo shows up as a Source Folder. You can debug this in either environment by executing these lines:
import sys
import pprint
pprint.pprint(sys.path)
Since import test would succeed even without your project being in place, I recommend you rename to test1.py and use import test1 so you can be sure you're pulling in your code.
I had the same problem, and ended up removing the "from . " that was added from the 2to3 conversion script.

Resources