Python importing module goes wrong - python

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?

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

Unable to import Tkinter

I am using Git Bash to run some code involving Tkinter, but have been unable to run the code and have gotten the error ModuleNotFoundError: No module named 'Tkinter'. I have #!/usr/bin/env/python3 as the first line of my code, but this does not seem to help. When i type import Tkinter and import tkinter into my bash line, it returns with bash: import: command not found. When I attempt to use sudo, it responds with bash: sudo: command not found. I am not sure what to do at this point, as I have already reinstalled both git bash and python and neither seem to help.
Python's import statement is case sensitive, so it just might work if you write import tkinter instead of import Tkinter.
The reason some some tutorials use the latter is because that's how it used to be in python 2. It wasn't considered very pythonic however, since all package and module names should be lowercase, so it was changed in python 3.
Regarding the first error message joelhed's anwser should solve your problem.
You receive the second error message because you
can't directly run python commands in the bash line.
To run python commands in the command line run python first.

"from __future__ import annotations" results in "annotations is not defined" in VSCode

from __future__ import annotations
SyntaxError: future feature annotations is not defined
I get this error when I try to run my code in the VSCode terminal. I never have this error when I ran my code with PyCharm. I just don't understand what is making it not work in VSCode.
I'm using Python 3.7 as my project interpreter.
Unless you created a virtual environment and selected that as your interpreter, typing something like python3 myfile.py will not guarantee you are using the Python interpreter you selected in VS Code (that's under the control of your shell, not VS Code). Make sure to use Run Python File in Terminal as that will make sure your file is run using the selected Python interpreter.

Python run file from command line

Yesterday I installed Python (with Numpy & Scipy) on CentOS, everthing works fine when I'm using the CLI and do some math. But now I try to execute a file, and then a weird error appears, searched all over the place but can't find the solution to fix this.
I'm running two Python versions, the version I'm using is 3.4, and I installed it at: /usr/local/bin
Then I made a file called test.py in the same directory, with this code:
import numpy
When I try to run it with:
./python3.4 -m test.py
I get this error:
/usr/local/bin/python3.4: Error while finding spec for 'test.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__')
I hope somebody can lead me into the right direction, thanks in advance!
Running things like
python -m <something>
invokes Python, which then tries to import a Python Module.
From your short example it doesn't appear to be a valid Python module
Try running with python <your python code file> instead.

How to get python script failure message

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.

Categories