How to get python script failure message - python

This may sound a newbie question, however I'm new to python,
I'm launching python shell then typing following into it
from tastypie.resources import *
The return message is
>>> from tastypie.resources import *
Aborted
The necessary modules are installed I've checked, please don't suggest running the command in separate .py file as my shell is launched with some setup by other scripts.
It there any python interpreter log (where it is located in ubuntu ?)
Is there any python configuration which debugs script execution.
P.S. python is 2.7.2+
Thanks in advance

python -vv to trace import statements, try python --help.

Related

How to fix error message "from: can't read /var/mail/pwn" [duplicate]

I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell

Python importing module goes wrong

If I import a module in a python (2.7.13) script e.g
from winappdbg import breakpoint
and execute it with the IDLE Python Shell,
I get an error:
ImportError: cannot import name breakpoint
If I run from the commandline:
>> python
>> from winappdbg import breakpoint
everything is fine.
Same problem if I run the script via Commandline:
>> python C:\path\to\script.py
results in the same error
The Python Version displayed by the IDLE Shell is exactly the same like the one displayed by the Commandline Command.
I checked the PYTHONPATH and also tried to add the script manually to the PYTHONPATH with no effect.
I also tried different Python Versions - still the same problem.
I do not know how to continue - so maybe someone have an idea or got the same problem?

Can't get Python IDLE to recognize OGR/GDAL Module

Folks,
Just getting started using OGR and Python for a variety of geospatial tasks. I'm working outside of OSGEO4w, and have installed GDAL w/ Python Bindings as well as Python v. 2.7.8 on my machine.
That said, I can run python and import gdal from a command-line interface but am unable to import the module when I use the IDLE environment. It returns an error telling me that the module doesn't exist. My install must be sound given that it works in the cmd prompt, so what's the deal?
This is all new to me, I'd appreciate any help or advice ya'll can impart; all previous questions I've read were concerned w/ merely installing (which I've done successfully) and getting Python to recognize the module from the command prompt, which it does.
Cheers,
Mike
Mike,
You can open IDLE with your python installation from the cmd line through:
>>>from idlelib import PyShell
>>>PyShell.main()
This should open IDLE from your current python, and you should be able then to import gdal and ogr from there.
Alternatively, you should have a windows batch file here
C:\YOURPYTHONPATH\Lib\idlelib\idle.bat
Running this should achieve the same.
Martin
Consider creating the following batch where PyInst references the Python installation folder from which IDLE is launched (i.e. idle.bat) and QgisInst references the folder containing "bin\o4w_env.bat":
#echo off
set PyInst=C:\Python27
set QgisInst="C:\Program Files\QGIS Brighton"
call %QgisInst%\bin\o4w_env.bat
set PYTHONPATH=%PyInst%\DLLs;%PyInst%\Lib;%PyInst%\Lib\lib-tk
set TCL_LIBRARY=%PyInst%\tcl\tcl8.5
python %PyInst%\Lib\idlelib\idle.pyw

Running Python script from cmd line but start with import in code

I am running a Python script from Linux command line, and the script itself, on the first line, import several modules. I got some error message and searched online. Here is a reply from the author of the Python script:
it appears that you are running dexseq_count.py as if it were a shell script, rather than from Python. As a consequence, the first line of the script is interpreted as the Linux command 'import' rather than as Python code, leading to the error you report.
I am curious if the first line of import in Python has been mis-interpretated in Linux, and if so, how can I solve this problem? I have to run in the cmd line instead of in Python.
Thanks so much!
Two solutions here:
You can run the script using python like this: python my_program.py or add this at the top of the file: #!/usr/bin/env python which will switch from bash to python to run this script

How do I create a custom python interpreter? i.e. with certain modules already included?

If you've used Ruby on Rails, I'm thinking of the feature where the user types
'rails console'
and instantly gets a Ruby console with rails and the current app already loaded.
I want to make something like this for a python program I'm working on, does anyone know how I would get to type say,
'python myPythonConsole.py'
and open up a regular python interpreter but with my program and all its dependencies loaded?
If I understand you correctly then you might want python -i myPythonConsole.py. It gives you a console when the script has finished so you have to run your application in a different thread.
To create a console in a script you would use the code module.
If you are using IPython (if you are not you should, it is an awesome python shell with TAB completion and many shortcuts) it is possible to set up profiles, which basically are named configurations.
Each configuration can import modules (and do other stuff) at startup.
Django does this with its "shell" command:
./manage.py shell
will open a Python shell with your Django settings loaded so you can import your project code interactively.
Source: http://code.djangoproject.com/browser/django/trunk/django/core/management/commands/shell.py
The real answer here is to use the PYTHONSTARTUP environment variable. See the tutorial section The interactive startup file.
Do your custom imports in a file interpreter.py, and configure
PYTHONSTARTUP=/path/to/interpreter.py
Next time your start Python, the custom code will be executed before you're dropped in the REPL shell.
Here's my customization:
import os
import sys
from pathlib import Path
from pprint import pprint
pp = pprint
P = Path
version = ".".join(str(number) for number in sys.version_info[0:3])
print(f"\nCustomized Python interpreter v{version} running from {sys.prefix}")

Categories