PyDev - Eclipse can import numpy but none of it's functions? - python

I'm having a problem importing the numpy module in Eclipse. The import numpy line runs with no problems. But then if I try use a function from the numpy module (e.g. numpy.zeros, numpy.uint8, etc.) I get the error Undefined variable from import.
The functions work fine in the python interpreter or actually running a python script. The problem is Eclipse recognizing the module functions. I've had no trouble adding other such modules. All of the modules I'm using come from the unofficial windows binaries. Once I've installed them, I've simply added the module's directory to the Eclipse interpreter libraries to get them to work. This has been unsuccessful for numpy. I've even tried adding each of the subfolders of the numpy package to the interpreter libraries. I've checked the import in the interpreter using a verbose run of Python to see what all happens during the import, but I don't see much that I haven't already tried added to the Eclipse interpreter libraries. I attempted using earlier versions of numpy and the version from the official numpy site as well, but with the same result. I tried adding the numpy directory to the project PYTHONPATH specifically as well, but with no success there either.
I'm using Windows 7, Python 2.7, and 64-bit versions of all the packages (and python).
Any suggestions? Thank you much!

My bet is that since numpy.__init__ conditionally loads itself on import with the if __NUMPY_SETUP__ block, the static analyzer has no idea which branch will run, and thus just fails to identify what would end up in the module's namespace whatsoever. You should try adding NumPy to the forced builtins in PyDev. I've had similar problems with other libraries (for instance, anything off flask.ext), but basically have just learned to ignore them.

Related

reduce namespace pollution in python scripts

I am working on some old python code and I was wondering if there is an easy way to find out which modules are imported but never used. I have a few hundred python scripts, each importing tens of modules (most scripts where copy-pasted by some template).
Pylint can do this. It reports "Unused import <module>" (warning named unused-import, code W0611).
This warning is enabled by default along with many others, but if you want to check for this warning specifically, you can do:
pylint --disable=all --enable=unused-import *.py
Using Pycharm does inform you if any modules or variables are used or not. That would be good for you in my opinion as an IDE.

Using external modules in a Pycharm project

I am new to Pycharm and need some help. I am working on a project that makes use of a large library of modules (specifically, Schrodinger; which allows for a lot of cool chemistry programs). Schrodinger requires the use of Python 2.7, if that makes any difference.
There are too many modules to install to the project directory. When I move the project directory to the location of the modules, my script becomes stuck on 'initializing'. I have attempted to import it as a package to no avail.
I have also tried to use the sys.path command, however a lot of the modules make use of other modules as well. So I that has become a pain very quickly.
How can I use these modules within Pycharm? And if there is no easy way, do you have a recommendation for an IDE that does have this feature?
Thanks
Pycharm doesn't identifies user defined modules which are not imported to Pycharm.
I usually mask the module as a Sources Root see the picture for more details. if the modules are in same project.
Alternative way: In your case import the external modules using File -> Open modules with open -> open in current window -> add to currently opened project this looks like two different projects. Now you can mark Sources Root for the complete module (i.e. learning) which you have imported.
import stackoverflow
Now pycharm can identifies the user defined modules.

How to import numpy to portable Python 3.6?

I have downloaded Python 3.6 Portable.
I need to use numpy.
I have downloaded it using git: git clone https://github.com/numpy/numpy.git numpy . Copied the directory "numpy" to the Python directory.
Now, when I use "import numpy", I get the following error:
ImportError: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
What should I do?
And no, it's not a duplicate of Explain why numpy should not be imported from source directory, since copying the numpy to the directory "site-packages" in the Python directory makes it so, that the interpreter simply does not see the numpy module.
The suggestion that this is the duplicate of a question where author asks to EXPLAIN something, is not accurate. Since I didn't ask for explanation, I asked, how can I make this WORK. And the "possible duplicate" question only tells about he way is DOES NOT WORK.
By the way, I have already found the way to get what I want without numpy. But numpy is the solution, what was advised in other stackovervlow answer, and this becomes time consuming for those, who uses portable Python, since there seems to be no obvious solution for them. And they are not told that when advised to use numpy.
The answers
NumPy is not just a set of Python files. It also contains a set of C extensions that need to be compiled to be used. Thus you can't just copy the files into your path and expect it to work.
For instructions on installing NumPy, see this page: https://docs.scipy.org/doc/numpy-1.13.0/user/install.html

Inconsistency between IDLE/PythonWin during module imports

I have been fumbling around between IDLE/PythonWin and even Aptana Studio 3 trying to get some consistency regarding importing modules. The modules I have been using lately are wx from wxPython and the arcpy module. IDLE will let me import arcpy, but not wx. PythonWin will let me import wx, but not arcpy.
When I do sys.executable in IDLE I get "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\Pythonwin.exe"
When I do sys.executable in PythonWin I get "C:\Python27\pythonw.exe"
Additionally, I am wondering if I am using different interpreters, how to set them all to the same one, and which one to use.
I have been learning and using Python for a little over half a year now and am at the point where I would really like to have these kinds of issues under control.
edit: lesson learned years later: use pycharm and let it keep your environments separate for you! lol
The problem is that ArcGIS comes with its own specific Python bundled with the software and you have installed a normal distribution into "C:\Python27\". I would try removing the latter and reinstalling wxPython. I'm guessing this IDLE of which you speak is actually embedded in ArcGIS rather than being the normal IDLE.
To be more clear, when you originally installed wx, it saw the Python in "C:\Python27\" and installed in that location. If you can't get wx to install to the GIS location, there may be a way to fake it by just copying the wx folder from C:\Python27\ and putting it in ArcGIS. They should both be in something like "C:\Python27\Lib\site-packages\wx-2.9.3-msw" (depending on version) and there's also a wx.pth file you'll need to copy from ""C:\Python27\Lib\site-packages" to the same basic location in the GIS directory.
EDIT: I also found the following links that might address your issue:
http://www.scoop.it/t/arcpy/p/1759717424/custom-wxpython-gui-s-an-approach-for-arcgis-10-1-arcgis-resource-center
http://forums.arcgis.com/threads/41809-wxPython-hooked-to-arcmap
wxPython and ARCGIS
It sounds like you have to add wxPython as an add-in.

how to import a module in environment that won't let it be a hazard to my computer? (python)

python 2.7
windows 7
I wan't to download a bunch of modules and run them. the programmer of these modules could make them contain a virus. to prevent this I would like to run these modules in an environment that won't let it import any modules unless I specify that it can import that certain module (a module I would let it import would be the math module, or other modules in the package. a module that I would restrict would be the os module.)
it this possible in any shape or form, even if it doesn't match all of the specifications I gave, or would I have to go through the code myself and make sure its fine.
It's very hard to do correctly with CPython. PyPy has much better sandboxing capabilities.
One option may be to do virtualization of different environments, either at a lower level, like with VirtualBox (https://www.virtualbox.org/), or at a higher level, like with virtualenv (http://pypi.python.org/pypi/virtualenv).

Categories