ipython import modules fine but python import fails - python

there.
I'm not quite sure what I have done recently that cause the problem as mentioned in the title. I usually run the simple code in ipython, and run longer scripts use python command. However, just now it seems that python somescript.py doesn't work well if some modules are imported in the somescript.py file. For example, say it contains import matplotlib.pyplot as plt, when I run python somescript.py in the terminal, I will get an error message:
ImportError: no module named matplotlib. This is weird since when I run import matplotlib.pyplot as plt under ipython, it works fine, no error.
And I checked that the python version and path is different from ipython. ipython has several more paths that python, and the version is 2.7.11, while python is 2.7.10. So, what should I do to permanently resolve this issue? Add the other paths to python? How can I make sure that they import the same modules each time? Thanks a lot.

Related

import rpy2 but can not import rpy2.robjects when changing start folders

The Current problem I am looking into is described below:
My computer is Win10, I installed only one anaconda 3.5.3 on it. Using where python there is only one python in my computer.
I downloaded a rpy2python wheel file from the uefi website, and install that using pip install.
When I import rpy2 in C disk, it is already fine, import rpy2,import rpy2.robjects are all OK.
But when I import rpy2 in my own project, I can only first import rpy2, when I import rpy2.robjects,the program says can not find rpy2 module.
Finally I found the problem is that in my project, I occasionaly established an rpy2.py file, when I first import rpy2, it where automatically create an rpy2.pycache folder, secondly when I import rpy2.robjects, Of Course the computer can not find an rpy2.robjects.
Just Keep a track of my problem.
You'll want to check the Python documentation about import rules for modules. By default, having a file called rpy2.py in the working directory for your Python code will cause import rpy2 to find this one rather that the rpy2 package.
The easiest fix is probably to rename your module rpy2.py into something else.

Module installed in Anaconda, same interpreter with Spyder yet Spyder gets "ModuleNotFoundError"

I have installed the netcdf4 library through
conda install -c anaconda netcdf4
And it shows up fine on my conda environment:
However, when I try to import it in Spyder, I am getting an
"ModuleNotFoundError: No module named 'netcdf4'"
I have tried the solutions here. As you can see below, both the interpreters are the same (in tools/preferences it's set as python.exe, not pythonw.exe):
Yet when I try to import the netcdf4 module, it says there is no module named this.
Any ideas what the cause could be? It's been two days of trying to get Anaconda to work and it has not been a smooth experience at all, really close on giving up on Anaconda as a whole.
Added per request, a screenshot of my entire IDE. Notice in the console I ran pip install netcdf4, where it says I already installed it. Directly below is importing the module and the error:
And how the console looks on each startup:
The docs/tutorial used stated
import netcdf4
While now it is case sensitive
import netCDF4
Such a simple fix..

How do I get Pyinstaller to work with pyplot?

I am trying to turn a program, that uses the matplotlib.pyplot module, into an executable.
The script itself works as intended when run in the terminal, but when I use the executable made by pyinstaller, it shows - "Script failed to execute".
I have narrowed down the issue to the following code:
import matplotlib.pyplot as plt
The .exe will run fine with this code commented out, but that way I am not able to plot graphs using pyplot.
EDIT: I did some research on this and found out it's a known issue with Matplotlib 3.3, so I was able to get around it by uninstalling the current version and using:
pip install matplotlib==3.1.3

VSC does not import installed python libraries

I recently got a new computer and installed VSC as well as the official python on it. I installed some extra libraries for python on it with pip but whenever I want to run a script it gives me the error: ModuleNotFoundError: No module named 'numpy', or any other library installed afterwards. The libraries that come with python do work.
When I run the same script in IDLE for example, all the libraries are found and it works fine, but I would like to be able to code in VSC.
For example:
import numpy as np
import pygame as pg
import time
Rest of the code...
The error occurs right when it tries to import the libraries.
Any ideas on what might be causing the problem?

Trouble in installing Basemap in matplotlib

I am trying to install Basemap, and beforehand I already have the prerequisite versions of Matplotlib, Python and Numpy working on my Mac. To install Basemap I am following the instructions in this website http://matplotlib.org/basemap/users/installing.html
I seemed to progress quite well with the steps, until the very end which is "Check your installation by running from mpl_toolkits.basemap import Basemap at the python prompt." I got "ImportError: cannot import name pyproj" from a line that reads "---> 30 from mpl_toolkits.basemap import pyproj".
Does anyone have any idea? Many thanks!
The compiled module could be accessible by python. To achieve that you should put the module into python path or you need to add the location of the module to PYTHONPATH environment variable.
To see the python installations paths you can write these lines to python shell:
import sys
for path in sys.path:
print(path)
The code will show the paths python is looking for modules.
After you compile the c library, you need to go to upper folder and run python setup.py install as it's said in the installation page. This will put python modules into one of your python paths.
I had the same problem installing basemap-1.0.7.
I found that the file
/usr/lib64/python2.6/site-packages/mpl_toolkits/basemap/init.py
had a reference to axes-grid1, but python lists only module axes_grid.
So I changed grid1 to grid and now basemap imports without error.
Had the same issue on OSX, found after much faffing that Basemap worked fine with a non-native version of python (for me 2.7.12 with everything under /opt/local/Library/Frameworks...) but not with the native version (for me 2.7.10 with most stuff under /System/Library/Frameworks... and Basemap under /Library/...). I did note also that under the native version there was already some mpl_tooklit content and without the permission to add Basemap there you end up with mpl_toolkit contents in multiple places. I wasn't sure if this was the problem specifically but as I said having it all under opt using non-native python was what solved this for me.

Categories