Python crashes on "import pdb" - python

I am debugging a python script. I have one section that is throwing IndexErrors, and I want to use pdb to find out why. Here is my script:
try:
# do stuff that throws an IndexError
except IndexError:
import pdb; pdb.set_trace()
When I run the script, it crashes on the line import pdb; pdb.set_trace(). The traceback I get:
Traceback (most recent call last):
File "myfile.py", line 113, in <module>
import pdb; pdb.set_trace()
File "/usr/lib/python2.7/pdb.py", line 10, in <module>
import bdb
File "/usr/lib/python2.7/bdb.py", line 3, in <module>
import fnmatch
ImportError: No module named fnmatch
import pdb, import bdb, and import fnamatch all work without error in the python interpreter. I don't even know where to start with this one.
UPDATE June 8 2016:
This question was recently brought to my attention again. I'm afraid I don't remember this at all, what the resolution was, or even what I was working on. I am not sure why I neglected to respond to the (good questions!) comments. At this point I assume it was resolved - I have successfully used pdb in the last 4 years :D
What I can say at this point: I was most likely using Ubuntu (probably 12.04).
I likely would have been using a virtualenv. Or at least thought I was using a virtualenv. I would expect stack traces to reference something like /home/foo/.virtualenvs/bar/..., not /usr/lib/python2.7.
It might be a reach, but I recall pip didn't always use --no-site-packages by default. Perhaps something installed on the system python conflicted with something in my virtualenv. Absolute paths somewhere, or messing with PYTHONPATH in some way?
This starts to be a reach, but I'm having trouble imagining what scenario I got myself into. This at least sounds plausible to me:
I had package X installed in the system python.
I created a virtualenv without --no-site-packages, thus pulling X into the virtualenv.
I installed Y in the virtualenv, which conflicted with X.
I was running my script in terminal A, in the virtualenv, and encountered the error.
I opened terminal B, forgot to activate the virtualenv, and thus ended up using a different interpreter (credit to #DSM for this one).
Without the virtualenv (and the hypothetical conflict), import pdb works fine.
I'm sorry this question appears to draw continued interest, and I only have hunches and guesses. If you encounter this today, my suggestion would be to check the stack trace locations are what you expect. If that's the case, check that any files referenced there actually exist, and appear to be intact.
Barring that, I would try a fresh virtualenv.
If you experience this problem and find a fix, please post it! I'll be happy to accept your answer.

Related

Can't import custom modules in python3.9 when running in wsl2

So I am trying to write some python code that will do two things, that seem to be mutually exclusive on my machine. My PC's host operating system is windows and I run Kali-Linux in WSL2 when I need to test my code on Linux. My code's main function creates two separate multiprocessing.Process objects, assigning a different thread, starting them both one after the other and then calling for them both to be joined. The plan is to allow each to run a simple server application simultaneously on different ports. This does not work when running python3 in PowerShell, as it seems to require access to os.fork() which doesn't work in said environment. When I found this out I pivoted to running in WSL2 which worked fantastically, for a time. After a while of experimenting with some ideas I decided to take some of my code and spin it off into its own file, which I placed in its own 'Libs' folder. WSL2 however, was unable to import this new file, instead giving me the exception ModuleNotFoundError: No module named 'NetStuff'. I originally had added:
sys.path.append('./Libs')
as has worked for me in the past, however when I found that WSL2 was unable to find my module, I printed out sys.path and it revealed that rather than appending my $current_working_directory/Libs like I intended, I was just appending the literal string, which wasn't useful. I then decided to try:
sys.path.append(str(pathlib.Path().resolve()) + '/Libs')
which at the bare minimum shows up as I would expect in sys.path. This, however still didn't work, python was unable to find my module and would unceremoniously crash every time. This led me to try something else, I ran my code in python3 under PowerShell again, which had no issue importing my module, it did still crash due to lacking os.fork() but the import gave no issues. Confused and annoyed I opened my code in IDLE 3.9 which, for some inexplicable reason, was able to import the file, and seemingly use os.fork(). The only major issue with running in IDLE is that it is seemingly incapable of understanding ascii colour escape characters. Given that the goal is to run my code in bash, and ideally also PowerShell, I am not satisfied with this as a solution. I returned to trying to fix the issue in WSL2 by adding my module to /home/Noah/bin, and appending this directory to sys.path, but this has still not so much as given me a new symptom.
I am utterly at a loss at this point. none of the fixes I know off hand are working, and neither are the new ones I've found online. I can't tell if I'm just missing something fundamental about python or if I'm running into a bug, if it's the latter, i can't seem to find other people with the same issue. As a result of my confusion and frustration I am appealing to you, kind users of stackoverflow.
The following is the snippet that is causing me problems in WSL2:
path0 = ('/home/Noah/bin')
path1 = (str(pathlib.Path().resolve()) + '/Libs')
sys.path.append(path0)
sys.path.append(path1)
print(sys.path)
import NetStuff
The following is output of print(sys.path) in WSL2:
['/mnt/c/Users/Noah/VSCodeRepos/Python/BlackPack', '/usr/lib/python39.zip', '/usr/lib/python3.9', '/usr/lib/python3.9/lib-dynload', '/home/noah/.local/lib/python3.9/site-packages', '/usr/local/lib/python3.9/dist-packages', '/usr/lib/python3/dist-packages', '/home/Noah/bin', '/mnt/c/Users/Noah/VSCodeRepos/Python/BlackPack/Libs']
The following is the error being thrown by WSL2:
Traceback (most recent call last):
File "/mnt/c/Users/Noah/VSCodeRepos/Python/BlackPack/BlackPackServer.py", line 21, in <module>
import NetStuff
ModuleNotFoundError: No module named 'NetStuff'
I am specifically hoping to fix the issue with WSL2 at the moment as I am fairly certain that getting the code to run on PowerShell is merely going to require rewriting my code so that it doesn't rely on os.fork(). Thank you for reading my problem, and if I left out any information that you would like to see just tell me and I'll add it in an edit!
Edit: I instantly realized that I should specify that my host machine is running windows 10.

ImportError: No module named stdio, any way to start fresh with python?

I'm getting the error
Traceback (most recent call last):
File "ghs.py", line 1, in <module>
import stdio
ImportError: No module named stdio
When I try to run my script. I can run my script on other machines just fine. I have installed python using homebrew. And I've tried everything I can think of to get it to recognize my modules! I've uninstalled and reinstalled using brew. I've tried changing the path (though I don't fully understand this). I get no issues using brew doctor.
I've also tried using a python virtual environment but to no avail.
Any ideas on how to fix this issue or else 'start fresh' from a fresh version of python?
When you import a module, Python looks for it at the directory your code is, and the directory in which the built-in libraries are (C:\Users\pc\AppData\Local\Programs\Python\Python35-32\Lib in my case, I'm using Windows 10 and Python 3.5). If it can't find it, it raises ImportError.
I couldn't find a module named stdio in my computer. I also know some C++ and as far as I know, stdio is the library for inputs and outputs(prints). In python, there is no need to import such a library.
You can use try,except statement to test if your code works without importing the module like this.
try:
import stdio
except:
#rest of your code goes here
You will need to indent your whole code however this can be done easily with a text editor in which you can edit more than one line at a time.

Why doesn't import work for me? - Python

Whenever I try to import a file into python, it comes up with this error(or similar):
Traceback (most recent call last):
File "C:/Python33/My Files/username save.py", line 1, in <module>
import keyring.py
ImportError: No module named 'keyring'
I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works. I must be doing something wrong, but whenever I look anything up for python, it never works out for me. It's almost as if loads have things have been changed over the years.
and idea's?
The keyring module is not part of the Python standard library. You need to install it first. Installation instructions are included.
Once installed, use import keyring, not import keyring.py; the latter means import the py module from the keyring package. Python imports should use just the name of the module, so not the filename with extension. Python can import code from more than just .py python files.
I was getting the same error "ModuleNotFoundError: No module named 'keyring'". And after installing this module pip install keyring, the same error occured with another module name. Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv, even after setting the Python Itereptor. Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.
To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image. Click ont the Python version, and then you will be able to select your virtual environment.
Now it will take the modules from your virtual environment.

Trouble importing Python modules on Ninja IDE

I have been trying to import modules into Ninja IDE for python. These are modules that I have working on the terminal (numpy, scipy, scitools, matplotlib, and mpl_toolkits), but will not run correctly in Ninja.
First I was only getting the message No module named ____. I checked sys.path and found that the path was within the application
/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7 was a typical path. I tried changing the path,but it doesn't seem to do anything to sys.path even after restarting the ide.
But I wanted the path to refer to where the modules are stored (which is /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages). I was able to get numpy and scipy to work as well as parts of mpl_toolkits by adding the contents of my path to the folders that sys.path gave. However, I still can't get fully functioning modules within the ninja ide interpreter. I'll give some examples below of what happens when I import certain modules.
import matplotlib.pyplot
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/matplotlib/__init__.py", line 106, in <module>
ImportError: No module named sysconfig
import mpl_toolkits
from mpl_toolkits.mplot3d import axes3d
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/mpl_toolkits/mplot3d/__init__.py", line 1, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 14, in <module>
File "/Applications/Ninja IDE.app/Contents/Resources/lib/python2.7/matplotlib/__init__.py", line 106, in <module>
ImportError: No module named sysconfig
Thanks for the help. I apologize, I am very new to programming, but I did put in about a day and a half of research before posting here.
That's strange as the sysconfig module is a part of Python 2.7 standard library.
Are you sure that Ninja is using the right Python version? Try running:
import sys
print sys.version_info
from Ninja, to see which Python version it is actually using.
I know this question is a few months old, but I wanted to post my solution in case others find it useful. I had a very similar problem, and had a lot of trouble finding a quick workable solution anywhere.
My somewhat roundabout solution was to simply create a virtualenv folder with the version of numpy I wanted, and then pointed the "virtualenv" property for NinjaIDE project to that folder. I restarted NinjaIDE and boom, instantly worked.
To set the virtualenv property for your project via the GUI, go to the Project menu:
Project > Open Project Properties > Project Execution,
and you should see a variable called "Virtualenv Folder". Point that to the folder for your virtualenv, and it should work. (May need to restart NinjaIDE.) This worked for me, NinjaIDE version 2.2 under Ubuntu 12.04.
One quick note: I actually didn't use virtualenv exactly -- I had to use a "conda env," since I am using the Anaconda distribution, and apparently it is not well-tested with virtualenv yet. (I actually got a warning when I went to easy_install virtualenv. Hadn't seen that before.)
Either way, this stackoverflow question has some nice pointers to virtualenv tutorials: Comprehensive beginner's virtualenv tutorial?
Good luck!
I was having a similar problem trying to import a module from /home/paul/lib/python using the console of the Ninja-IDE. I found out that /home/paul/lib/python didn't appear in syspath when checking in the console of the Ninja-IDE. But it did in the terminal!
By starting the Ninja-IDE from the terminal, /home/paul/lib/python was in syspath when checking in the console of the Ninja-IDE. I was now able to import the module I needed.
I hope this might be of some help. If not to ebris1 than maybe to others.

LPTHW ex50:Can't find module utils

I'm at exercise 50 of lpthw this is the link.
And after having downloaded the lpthw.web framework in a very messy way.
I continued the exercise by writing a .py file.
import web
urls = ('/', 'index')
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello World"
return greeting
if __name__ == "__main__":
app.run()
but i got this error:
Traceback (most recent call last):
File "bin\app.py", line 1, in <module>
import web
File "c:\Python31\lib\site-packages\web\__init__.py", line 14, in <module>
import utils, db, net, wsgi, http, webapi, httpserver, debugerror
ImportError: No module named utils
What is the problem caused by,
I suspect it is the very messy way with wich I installed lpthw.web (I got a lot of errors, but tried multiple ways multiple times.)
Thank you in advance!
If anybody needs more info comment so I can edit.
I think you forgot to read the setup instructions. This learning guide is not python3 compatible:
Exercise 0: The Setup
You should follow these instructions as exactly as possible. For
example, Mac OSX computers already have Python 2, so do not install
Python 3 (or any Python).
Make sure you install Python 2 not Python 3.
Warnings For Beginners
A programmer may try to get you to install Python 3 and learn that. You should tell them, "When all of the python code on your computer is Python 3, then I'll try to learn it." That should keep them busy for about 10 years.
Remove python3 from your system and install python2.7 to allow this tutorial to work when you install suggested packages.
Otherwise, find another tutorial that is using examples from libararies that are python3 compatable. Not everything is updated as of right now. Many people stick to python2.7 for this reason.
As the tutorial state, you can try installing using easy_install or pip from the command line it would be easy_install lpthw.web or pip install lpthw.web
Also note that you need to properly reference application
this app = application(urls, globals()) should be
app = web.application(urls, globals())
note the ^^^ part.
Also if you are interested in python micro-web-frameworks I recommend bottle.py, it doesn't need to be installed its just a single python file called bottle.py
Update
I managed to recreate the problem using python3, so jdi was right, the problem here is that python3 isn't backwards compatible with python2, some python2 complaint applications won't properly run under python3 and vice-versa.
In this case python3 doesn't seem to support relative imports within package, did find this http://www.python.org/dev/peps/pep-0328/ which was kind of interesting.
exert from the pep:
For the second problem, it is proposed that all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.
so lpthw.web tries to import relative packages but it simply can't.
which is why we get ImportError: No module named utils since utils is relative module within the package.
even if it could loaded it, it will stumble with other non-backwards compatibility issues, just trying to import utils within the package we get
>>> import utils
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "utils.py", line 75
except KeyError, k:
^
SyntaxError: invalid syntax
which is related to this pep http://www.python.org/dev/peps/pep-3110/ which states different syntax to the except statement.
As you can see it will be a major hurdle trying to migrate this package to python3 so much so thats its better to leave it to the developers, this goes for the many many packages out there, why is python3 so different from python2, well mainly because they wanted to patch the language as much as possible ...
For the time being stick to python2, eventually most packages will be migrated to python3.

Categories