Trouble importing Python modules on Ninja IDE - python

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.

Related

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.

Python crashes on "import pdb"

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.

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.

Python: Installing and using Exscript module Windows x86

I have been banging my head against the wall trying to get Exscript installed. After multiple failed attempts at doing it manually, I installed ActivePython and had success running "pypm install Exscript" from the cmd prompt.
I am now going through the Exscript documentation (found here https://github.com/knipknap/exscript/wiki/Python-API-Tutorial) and if I run the first example script I get an error:
>>> from Exscript.util.interact import read_login
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
from Exscript.util.interact import read_login
ImportError: No module named interact
So, I understand that this is saying that there is no module interact. How can I check this? Is there a way I can manually add this module? I would love to know WHY this module didnt come with the package, but that may be impossible to answer :)
Any and all help is greatly appreciated. Thank you
EDIT -
import Exscript.util works but if I try import Exscript.util.Interact I get the error. When I look in util.py I see an entry that says "from FooLib import Interact". I first thought it may just be a capitalization error (Exscript.util.interact vs util.Interact) but neither of those work. I am not sure where to go from here... :(
EDIT -
I have posted this question on the developers forums, hopefully he will have an answer for us. https://github.com/knipknap/exscript/issues/15
EDIT -
The developer suggested that I was using an old version and told me to download the latest. I had struggled installing the module manually so I googled how to easily install py modules. I found a writeup on easy_install.exe. I ran "c:\Python26>easy_install C:\Users\support\Desktop\lou\knipknap-exscript-v2.1-70-gf5583f3.tar.gz" from the cmd prompt, the module was installed (no errors) and now when i run the script it works.
Next challenge will be how to get these scripts to run as stand-alone exe's on users computers without Python installed :)
THANK YOU to everyone to commented I truely appreciate your help.
Lou
One common way packages are installed is as directories. So check your site-packages directory for an Exscript directory, and inside that there should be a util directory, and inside that there should be an interact.py file. Look for similar spellings in case the tutorial misspelled something.

Eclipse + PyDev ImportError

I am having trouble getting PyDev on Eclipse to recognize installed modules (gensim), which work fine in IDLE. I am using Windows Vista, 32-bit. Python 2.7.
I have found this question asked: here, here, here, and here.
The recommended solution is to go to preferences > pydev > interpreter - python, and remove and re-add (w/ Auto Config) the python interpreter. I have done this, and have restarted Eclipse. In PYTHONPATH, C:\Python27\lib\site-packages\gensim-0.8.0-py2.7.egg, appears, but I still run into the import error. My code is:
from gensim import corpora, models, similarities
And this yields:
Traceback (most recent call last):
File "C:\Users\Jpan\Documents\workspace\FirstProject\src\gensim.py", line 1, in <module>
from gensim import corpora, models, similarities
File "C:\Users\Jpan\Documents\workspace\FirstProject\src\gensim.py", line 1, in <module>
from gensim import corpora, models, similarities
ImportError: cannot import name corpora
Another recommended solution is to manually add the folder by clicking "New Folder" in the bottom part of the interpreter - python screen and navigating to the location where gensim installed. I have also done this, and added C:\Python27\lib\site-packages\gensim-0.8.0-py2.7.egg\gensim, which has all the necessary \__init__.py files. But, I still get the ImportError.
Any suggestions for what else I could try?
This is independent of Eclipse/PyDev. You'll get the same error running the code in any other way. Your module imports gensim. The first entry on the PYTHONPATH is the current directory, and your module is called gensim.py, so your module attempts to import iteself. Because imports are cached, you don't get into infinite recursion but get a reference to a module containing... nothing, especially not the things you expected from the "real" gensim module.
The error message should mention this possibility, it's incredibly common. The solution is to rename your file.

Categories