Python2.7 searches the unwanted directory when importing modules - python

I tried to use Python 2.7 to import numpy.
Configuration that may cause the problem
Two Python interpreter installed on my computer, Python 2.4 and 2.7
two copies of numpy installed, one in my home directory, which is the desired one, the other one in the Python 2.4 place, /usr/lib/python2.4/site-packages/, which is undesired.
Also, I set the PYTHONPATH environment variable to contain the necessary path to import the desired numpy copy.
But Python 2.7 searches where Python 2.4's default module installation path is, in my case,the /usr/lib/python2.4/site-packages.
How can this happen?

If you cannot find where the inclusion comes from you can put the following at the top of your program before other imports:
import sys
sys.path.remove('/usr/lib/python2.4/site-packages')

Related

So i installed numpy . But when i call it in a program an error occurs. Any method to solve it permanently in windows 10

Here is the error
import numpy
Exception has occurred: ModuleNotFoundError
No module named 'numpy'
File "C:\path\to\file\32.py", line 1, in <module>
import numpy
Let me know how did you install the NumPy package; using pip or something else?
If you have multiple python versions, i.e. 2.x and 3.x at the same time, please make sure your interpreter for the 32.py file is the version that you installed NumPy on.
To possibly fix your problem, you should first try installing it and see if there are any errors. You should also check the version of Python you are running on Windows 10, because when you update Python it sometimes switches names between py and python
As you can see, the version of Python has changed between py and python so you should try changing that first.
If this does not work, you should try finding the directory for NumPy and adding it to the system PATH in your script. The installer usually shows you the location by doing the following:
import sys
sys.path.append("<insert numpy location here>")
import NumPy
This should manually force it into finding the package. If none of this works, please tell us and we should be able to find a different solution.
Happy Coding!
If you're using a code editor like PyCharm, you could install it by clicking on
file then settings then the project interpreter setting and install new module! You can search for the module and install.
Make sure that the python version that you want to use is a Windows Environmental Variable. You can test this by running this line in your command line.
> python --version
If you get some other python version that is not the one that you wish to use you can set the python version you want by finding where exactly your Python folder is located and go into settings and set the path as a new variable (I can leave a tutorial for you). If that is too much of a hassle, the Python installers can set the python that you will install as an environmental variable for you. (You could uninstall and reinstall and make sure that you allow it to make it an environmental variable.
After that you should be able to import whatever external packages you want using pip
for example:
pip install numpy

Module imports, but doesn't have any attributes

I am trying to import a Python module (fiasco). I cloned it from GitHub and everything appeared to be working fine. Importing it works, but when I try to type, for example iron = fiasco.Element('iron', [1e4, 1e6, 1e8]*u.K), I get the error module 'fiasco' has no attribute 'Element'. I am using Spyder's iPython console. This also fails if I start iPython from the terminal, but works if I start python3 from the terminal.
I had done this on two different computers - on one, it worked at first, but started giving me the same error after I restarted the kernel. On the other, it never worked at all.
If it helps: after importing, I tried typing fiasco. When I did this on the computer where it originally worked, the output was <module 'fiasco' from '/Users/shirman/fiasco/fiasco/__init__.py'>. Now, and on the computer it never worked on, it just says <module 'fiasco' (namespace)>. So maybe this has something to do with paths?
Addition: sys.path points to /Users/shirman, and several paths within /Users/shirman/anaconda3. The fiasco folder is in /Users/shirman.
You have inadvertently created a namespace package due to your sys.path setting. Namespace packages are directories without an __init__.py in the Python search path and allow loading of submodules or -packages from different paths (e.g. path1/foo/a.py and path2/foo/b.py can be imported as foo.a and foo.b).
The problem is that import fiasco finds /Users/shirman/fiasco first and imports it as a namespace package. If you set sys.path such that /Users/shirman/fiasco comes before /Users/shirman, the importer finds the actual package /Users/shirman/fiasco/fiasco first.
Namespace packages are a Python 3.3 feature, so either the other machine had a different sys.path setting, a really old Python 3 installation, or you were using Python 2.

how python import packages with multiple copies

I was believing that when importing a package, it would search from sys.path and use the first hit for import. However, it seems not true:
import mpl_toolkits
print(mpl_toolkits.__path__)
And it outputs:
['/Library/Python/2.7/site-packages/matplotlib-1.5.0-py2.7-macosx-10.11-intel.egg/mpl_toolkits', '/usr/local/lib/python2.7/site-packages/mpl_toolkits']
Can someone please explain to me how exactly python looks for packages if it is installed multiple times in the machine (in different location searchable by sys.path)? Or a pointer to relevant reference would be good.
when you import a module, python uses PYTHON PATH (system variable that contains a list of folders) and loops to search for importable module.
Python will test if it is a package (folder containing init.py) or a module (*.py). it will stop on first module found if no module is found python raises an import error

added path in PYTHONPATH changes sys.path entires

I have numerous versions of packages installed. My base, system python is ~/Library/Enthought/Canopy_64bit/User/lib/python2.7/
but I also have an application ('yt') which installed its own python to
~/Applications/yt/yt-x86_64/lib/python2.7/
I've added the yt path so that I can import a module which it includes, when I run my system python. The problem is, when I add the yt-path to PYTHONPATH it adds a ton of other directories, to higher entries in my sys.path so that when I try to import numpy (for example), I end up getting the yt-version, instead of my system version.
Is there a way to keep my sys.path from being modified?
PYTHONPATH values are always inserted in front of the standard python library paths in sys.path
One potential approach around this problem is to add the yt path to sys.path yourself.
So try
# append to the *end* of the system path.
sys.path.append('~/Applications/yt/yt-x86_64/lib/python2.7/path/to/libs')
this will put the yt specific modules at the end of the list, and your system's numpy will be found/imported first.

Python: import _io

I'm trying to determine which files in the Python library are strictly necessary for my script to run. Right now I'm trying to determine where _io.py is located. In io.py (no underscore), the _io.py module (with underscore) is imported on line 60.
Some modules are compiled directly into the interpreter -- there are no files corresponding to them. You can retrieve a list of these modules from sys.builtin_module_names. In my Pyton 3.1 installation, _io is included in this list.
You might want to have a look at snakefood to determine the dependencies of your script.
Not all Python modules are written in Python. Try looking for _io.so or _io.pyd.
Try the DLLs folder under your base python install directory if you are on windows. It contains .pyd modules Ignacio mentions. I had a similar problem with a portable install. Including the DLLs folder contents to my install fixed it. I am using python 2.5.
From python-list email archive: is "_io.py" missing from 2.7.4 ?, the situation for Python 2 and 3 is different:
In Python 2.7:
To find where the _io module lives, at the interactive interpreter run
this:
import _io
_io.__file__
Under Linux, you should get something like this:
'/usr/local/lib/python2.7/lib-dynload/_io.so'
and the equivalent under Windows.
In Python 3:
Note that in Python 3.3, the _io module is now built-in into the
compiler, so _io.__file__ no longer exists.

Categories

Resources