python related (numpy and scipy) - python-2.7

when i run command import numpy as np or import scipy as sp, it gives me error like:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import numpy as np
ImportError: No module named numpy
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
import scipy as sp
ImportError: No module named scipy

(Disclaimer: there are already tons of well established way / tutorials on the internet. I'm merely posting this to hopefully help out quickly)
 What you want to achieve
To install a library (e.g. numpy, scipy) locally on a machine (e.g. laptop, server, etc.) and import that library from a Python code.
 One of the solutions: Anaconda
One of the popular / quick ways in the Python scientific community is to do this via Anaconda (disclaimer 2: I personally prefer Anaconda due to its ease of enabling me to switch / play with different Python environments). Here is the step by step instructions:
Download and install the Anaconda distribution onto the machine locally.
Create a file environment.yml and store it anywhere you like (e.g. a subdirectory within your home directory). The file looks like this gist file - tweak it to your taste (e.g. choose Python version 2.x vs 3.x, add/remove/edit dependencies, etc.)
Within the same directory where you've created the environment.yml, create a conda environment by: conda env create -f environment.yml. For this particular gist file, it will create a conda environment (called "helloworld") with the specific Python version (2.7) and the anaconda package (which includes the popular numpy and scipy libraries).
Activate the environment (i.e. "go inside that environment") by: source activate helloworld (replace "helloworld" with whatever name you specified in environment.yml).
Now you are in the "helloworld" conda environment, start up a python console: jupyter console.
Now try importing stuff within this console:
Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import numpy as np
In [2]: import scipy as sp
In [3]: np.version.version
Out[3]: '1.12.1'
In [4]: sp.version.version
Out[4]: '0.19.0'
(to quit console just do a Ctrl + D to go back to command line)
For step 5 above, also try out:
jupyter notebook
jupyter qtconsole
And play with the python commands.
When you are done with the conda environment, just "deactivate" (i.e. get out of) it by doing this in the command line: source deactivate.
Top tip: don't forget step 4 - this defines which conda environment you are in (i.e. which python version and libraries available etc.). I occasionally ommited step 4 by accident and get that error "no module named numpy", etc.)
See this Anaconda get started guide for more info.
The non Anaconda way
If you would like to avoid Anaconda all together, just simply do this in the command line:
Install the numpy and scipy libraries:
pip install numpy
pip install scipy
Start a Python interpreter:
python
Do the library import stuff within the Python interpreter:
>>> import numpy as np
>>> import scipy as sp
>>> np.version.version
'1.11.3'
>>> sp.version.version
'1.11.3'
 Additional alternatives
You could try out the Python VirtualEnv - though I've never really used it since I've started using Anaconda.

Related

PIL.ImageTk import and/or installation error

When I get to my beloved Spyder console and it welcomes me with
Python 2.7.12 (default, Dec 4 2017, 14:50:18)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
I go on and type this:
In [1]: from PIL import Image
And it goes and shows another line like everything was right.
Then, on the next line, I type this:
In [2]: from PIL import ImageTk
And it returns this:
Traceback (most recent call last):
File "<ipython-input-2-47edf18ebb7f>", line 1, in <module>
from PIL import ImageTk
ImportError: cannot import name ImageTk
Okay, this means I should have a problem with my libraries. No sweat.
A fellow programmer had a similar error here, and:
I am sure there are no typos in my import
Ubuntu reports the python-imaging package as up-to-date:
Ubuntu reports the following on the python-imaging-tk package:
These Bash lines:
python-imaging is already the newest version (3.1.2-0ubuntu1.1).
<more bash lines />
The following packages have unmet dependencies:
python-imaging-tk : Depends: python-imaging (= 1.1.7-4) but 3.1.2-0ubuntu1.1 is to be installed
This is the juiciest part. When I go back to Spyder and type to get the version of PIL.Image, it returns this:
In [3]: Image.VERSION
Out[3]: '1.1.7'
I am at a loss here. Please send help.
Doing a
sudo apt-get install python-imaging=1.1.7-4
and then
sudo apt-get install python-imaging-tk
solved the problem.
It still bothers me why but at least my dependencies are working now.
sudo apt-get install python-imaging-tk
this just solved my problem. This is for python 2.7

Gurobipy DLL load failed

When trying to import gurobipy i get the following ImportError:
D:\Development\Anaconda3\envs\hiwi\python.exe D:/Development/Hiwi/Project_Code_Source/experiment/demo/run.py
Traceback (most recent call last):
File "D:/Development/Hiwi/Project_Code_Source/experiment/demo/run.py", line 2, in <module>
import experiment.demo.scenarios as scenarios
File "D:\Development\Hiwi\Project_Code_Source\experiment\demo\scenarios.py", line 15, in <module>
import framework.simu.ins_gen_v3 as insgen
File "D:\Development\Hiwi\Project_Code_Source\framework\simu\ins_gen_v3.py", line 10, in <module>
from gurobipy import *
File "D:\Development\Anaconda3\envs\hiwi\lib\site-packages\gurobipy\__init__.py", line 1, in <module>
from .gurobipy import *
ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden. (English: Module not found.)
Process finished with exit code 1
Since searching google resulted in errors related to environment variables and 64 vs 32 bit Versions i checkt those.
I am running the 64 bit Versions of Annaconda (3.6 using a 2.7 env) on Windows 10 and Gurobi
Gurobi is installed according to this instruction (including the license): http://www.gurobi.com/downloads/get-anaconda
The GUROBI_HOME variable is correctly set to C:\gurobi702\win64 and the Path variable to C:\gurobi702\win64\bin
Python Path variables are set.
Following packages are installed: gurobi 7.5.1, mkl 2017.0.3, numpy 1.13.1, pip 9.0.1, python 2.7.13, setuptools 27.2.0, vs2008_runtime 9.00.30729.5054, wheel 0.29.0, wsgiref 0.1.2
Starting the gurobi console via cmd gurobi works fine.
I reinstalled my complete python environment multiple times and tried restarting after installation.
I got it running with a new conda environment and installing via python setup.py install, as mentioned by Greg Glockner and sascha.
This problem [from .gurobipy import] is caused from the IDE Pycharm on Ubuntu. I am not sure if you may face it with the other IDEs .
First You can make sure from the shell the python has gurobipy installed.
after you installed from the file Gurobi ~\gurobi752\win64
python setup.py install
then just run python from the shell as normal python command
python yourFileName.py and gurobi will works perfectly
In windows 10 i added the path of gurobi to the IDE pycharm
after python setup.py install and pip tool for gurobipy https://sites.google.com/site/pydatalog/python/pip-for-windows, then i added the path
Settings->Project Interpreter-> show all -> interpreter paths -> add your gurobi path
I hope it works cheers ;)

pandas working in ipython notebook with python 2.7 but NOT with python 3.5

After asking this question, I installed environments for both python 2.7 and python 3.5: Easiest way to have Python 2 and 3 coexist on Mac OSX 10.8 with Anaconda installed
i also manually installed kernels for both python versions. If I start in my terminal the py35 environment (source activate py35) and then ipython notebook, i can choose a kernel that lets me use either python 2.7 or python 3.5.
Up to this point, everything is good.
However, when I run a check of all the modules I'm going to need, including ipython, numpy, scipy, pandas, scikitlearn, etc...using commands such as:
import pandas
print("Pandas version: %6.6s " % pandas.__version__)
the notebook crashes when it gets to pandas, even though I checked manually with conda list and pandas is indeed installed and updated to the last version.
What's interesting is that if i run the same checks using the python 2.7 kernel, everything runs smoothly, including the pandas command.
What's going on?
EDIT: Here's the kind of feedback I get upon crashing:
ValueError Traceback (most recent call last)
<ipython-input-2-3b71b0be8baa> in <module>()
12
13 # Pandas makes working with data tables easier
---> 14 import pandas
15 print("Pandas version: %6.6s (need at least 0.16.2)" % pandas.__version__)
16
then more of that...and at the very end:
ValueError: unknown locale: UTF-8

Error importing theano "cannot import name gof"

I am current getting the error
ImportError: cannot import name gof
when importing theano.
>>> import theano
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import theano
File "C:\Python27\lib\site-packages\theano\__init__.py", line 63, in <module>
from theano.compile import (
File "C:\Python27\lib\site-packages\theano\compile\__init__.py", line 9, in <module>
from theano.compile.function_module import *
File "C:\Python27\lib\site-packages\theano\compile\function_module.py", line 16, in <module>
from theano import gof
ImportError: cannot import name gof
I am using python 2.7.10 (). Theano is installed using pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git.
Hope to get you suggestion to solve this problem
Most of the time, when I see this error, it is caused by those 2 errors:
1) A syntax error in Theano. Update Theano and make sure to have no local modifcation. I nerver saw this error in the master of Theano, but just in case.
2) When there is multiple version of Theano that are installed.
In both case, remove all version of Theano. Do it multiple time to be sure there is none left. Then install again.
From memory, this always solved the problem when it wasn't a syntax error during development (but not in the master version of Theano that you use)
This ImportError can be caused because Theano is unable to compile the gof module itself. If this is the case, you will see an error message that looks like "Exception: Compilation Failed (return status=1): C:\Long\Path\...\mod.cpp:1: sorry, unimplemented: 64-bit mode not compiled in".
Fixing With Conda
If you are installing theano into a conda environment, make sure that you have a C compiler available to that environment.
The command
conda install m2w64-toolchain
will provide a C compiler to your environment that's isolated from the rest of the machine.
After the m2w64-toolchain package is installed, import theano should work
Fixing Manually
If you are installing Theano yourself, two points from these threads may help:
Install the bleeding edge version of Theano
Install libpython from http://www.lfd.uci.edu/%7Egohlke/pythonlibs/
I assume you're using Windows 7 or later.
If you have installed Python Anaconda, then open Windows Powershell or Command Prompt and type conda install mingw libpython before typing pip install theano
Alternatively, if you don't have Anaconda, download those packages from
anaconda.org/anaconda/mingw/files
anaconda.org/anaconda/libpython/files
github.com/Theano/Theano
Then open Command Prompt, navigate to each folder and type python setup.py install
Now run Python and import theano
Possible errors:
If you get the RuntimeError: "To use MKL 2018 with Theano you MUST set "MKL_THREADING_LAYER=GNU" in your environement" then
Go to Control Panel > System > Advanced system settings and select "Environment Variables".
In the "System variables" section, make a new variable name MKL_THREADING_LAYER and set its value to GPU
If you get other kinds of errors, then try the following:
Make an empty file called .theanorc (a file extension without a file name) in your home folder C:\Users\<username>. If you get the error "You must type a file name" then see stackoverflow.com/q/5004633
Open .theanorc and write this:
[global]
cxx=C:\<path to Anaconda>\Anaconda3\MinGW\bin\g++.exe
Run Python again and import theano. If it works, then you can probably delete .theanorc
In my case, the fix was to install a python build that's callable as a shared library:
PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 2.7.15

python 2.7 module pandas not installing "cannot import name hashtable"

I tried looking for an answer to this around the forum/google, but I can't find anything. My issue is this (from python console):
>>> import pandas
cannot import name hashtable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pandas\__init__.py", line 6, in <module>
from . import hashtable, tslib, lib
ImportError: cannot import name hashtable //also can't import name NaT somtimes
I ran the windows 1-click installer prior to attempting the import. I'm running everything 32-bit. The pandas installer is for python 2.7.
Here's a list of modules that I have correctly imported into Python.
setuptools
pip
mox
dateutil
six
numpy
SQLAlchemy
I'm on windows 7.
I also have anaconda installed, but that was really just a "hail mary" after I tried everything else. My end goal is to install the ultra-finance module. However, it seems to require pandas, hence me being stuck.
I'm a python noob, so please don't assume I know anything. Thanks.
EDIT: please let me know if I can provide any extra information.
The recommended way to install pandas is via pip:
pip install pandas
This hashtables error arises from the cython files not being built. This error message will be more informative from 0.11.1.
Try running your code in Spyder (Anaconda -> Spyder). It worked for me.
Check that you have python scripts included in your system path variable. In my case I had to add "C:\Python27\Scripts"
I was having a similar problem when downloading Pandas to my Windows 8 system. The first error I had was an egg error, but after installing some packages I think I have the solution.
First look at the previous pip errors with Pandas, make sure you have the most updated pip.
The second part is downloading wheel using
pip install wheel
After installing wheel and having the dependencies for panda and using pip it worked correctly.