Cannot import tensorflow in python after source build - python-2.7

I am trying to install tensorflow with cuda and cudnn on a linux machine. I do not have sudo access, so I am building from source. I followed instructions here: https://www.tensorflow.org/versions/master/get_started/os_setup.html#source
I got till the part where we get lots of output:
This tutorial iteratively calculates the major eigenvalue of a 2x2 matrix, on GPU.
The last few lines look like this.
000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]
But after that, when I open python and try to import tensorflow, it says there is no such module.
Thanks in advance.

You need to take a few more steps before you can import TensorFlow in a Python shell: just building //tensorflow/cc:tutorials_example_trainer does not build any of the Python front-end.
The easiest way to do this from a source installation is to follow the instructions for building a PIP package from source, and then installing it (either globally on your machine, or in a virtualenv).

Related

rpy2 on pycharm generates segmentation fault error

I am using (osx) pycharm as ide and anaconda as python (2.7.10) distribution.
Recently I have installed rpy2 which works quite well on notebook e.g.
In [5]:import rpy2.robjects as robjects
In [7]:robjects.r.pi[0]
Out[7]:3.141592653589793
But on pycharm I get a segmentation fault error.
import rpy2.robjects as robjects
/Users/xxx/anaconda/envs/analytics3/bin/python.app: line 3: 695 Segmentation fault: 11 /Users/xxx/anaconda/envs/analytics3/python.app/Contents/MacOS/python "$#"
PYcharm support claims that this is a bug in the code.
any ideas what that might be?
many thanks
Reinstalling rpy2 from
conda install -c conda.anaconda.org/rpy2
solved the issue.
If you install rpy2 via conda, and also have a system installation of R on the same machine (e.g with RStudio), the system's R installation will be used. Since this R version doesn't match the one that rpy2 needs, segmentation faults occur.
1) remove any existing system installations of R (see here). Verify that you don't have any installations of R:
$>which R
R not found
2) define R_HOME env variable, either in your .rc file:
export R_HOME=/Users/<your user>/anaconda3/envs/<env name>/lib/R
or dynamically in the python project:
import os
os.environ['R_HOME'] = '/Users/<your user>/anaconda3/envs/<env name>/lib/R'

Can you run both python 2 and python 3? [duplicate]

I'm running Windows and the shell/OS automatically runs Python based on the registry settings when you run a program on the command line. Will this break if I install a 2.x and 3.x version of Python on the same machine?
I want to play with Python 3 while still being able to run 2.x scripts on the same machine.
The official solution for coexistence seems to be the Python Launcher for Windows, PEP 397 which was included in Python 3.3.0. Installing the release dumps py.exe and pyw.exe launchers into %SYSTEMROOT% (C:\Windows) which is then associated with py and pyw scripts, respectively.
In order to use the new launcher (without manually setting up your own associations to it), leave the "Register Extensions" option enabled. I'm not quite sure why, but on my machine it left Py 2.7 as the "default" (of the launcher).
Running scripts by calling them directly from the command line will route them through the launcher and parse the shebang (if it exists). You can also explicitly call the launcher and use switches: py -3 mypy2script.py.
All manner of shebangs seem to work
#!C:\Python33\python.exe
#!python3
#!/usr/bin/env python3
as well as wanton abuses
#! notepad.exe
Here's my setup:
Install both Python 2.7 and 3.4 with the windows installers.
Go to C:\Python34 (the default install path) and change python.exe to python3.exe
Edit your environment variables to include C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;
Now in command line you can use python for 2.7 and python3 for 3.4.
From version 3.3 Python introduced Launcher for Windows utility https://docs.python.org/3/using/windows.html#python-launcher-for-windows.
So to be able to use multiple versions of Python:
install Python 2.x (x is any version you need)
install Python 3.x (x is any version you need also you have to have one version 3.x >= 3.3)
open Command Prompt
type py -2.x to launch Python 2.x
type py -3.x to launch Python 3.x
You can have both installed.
You should write this in front of your script:
#!/bin/env python2.7
or, eventually...
#!/bin/env python3.6
Update
My solution works perfectly with Unix, after a quick search on Google, here is the Windows solution:
#!c:/Python/python3_6.exe -u
Same thing: in front of your script.
Here is a neat and clean way to install Python2 & Python3 on windows.
https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a
My case: I had to install Apache cassandra. I already had Python3 installed in my D: drive. With loads of development work under process i didn't wanted to mess my Python3 installation. And, i needed Python2 only for Apache cassandra.
So i took following steps:
Downloaded & Installed Python2.
Added Python2 entries to classpath (C:\Python27;C:\Python27\Scripts)
Modified python.exe to python2.exe (as shown in image below)
Now i am able to run both. For Python 2(python2 --version) & Python 3 (python --version).
So, my Python3 installation remained intact.
I'm using 2.5, 2.6, and 3.0 from the shell with one line batch scripts of the form:
:: The # symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
#c:\programs\pythonX.Y\python.exe %*
Name them pythonX.Y.bat and put them somewhere in your PATH. Copy the file for the preferred minor version (i.e. the latest) to pythonX.bat. (E.g. copy python2.6.bat python2.bat.) Then you can use python2 file.py from anywhere.
However, this doesn't help or even affect the Windows file association situation. For that you'll need a launcher program that reads the #! line, and then associate that with .py and .pyw files.
When you add both to environment variables there will a be a conflict because the two executable have the same name: python.exe.
Just rename one of them. In my case I renamed it to python3.exe.
So when I run python it will execute python.exe which is 2.7
and when I run python3 it will execute python3.exe which is 3.6
Here you go...
winpylaunch.py
#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
# #!/usr/bin/python
# #!C:\Python26\python.exe
# # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
# HKEY_CLASSES_ROOT\Python.File\shell\open\command
# (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#
import subprocess
import sys
USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""
if __name__ == "__main__":
if len(sys.argv) > 1:
script = sys.argv[1]
args = sys.argv[2:]
if script.endswith(".py"):
interpreter = "python.exe" # Default to wherever it is found on the path.
lines = open(script).readlines()
for line in lines:
if line.startswith("#!") and line.find(".exe") != -1:
interpreter = line[2:].strip()
break
process = subprocess.Popen([interpreter] + [script] + args)
process.wait()
sys.exit()
print(USAGE)
I've just knocked this up on reading this thread (because it's what I was needing too). I have Pythons 2.6.1 and 3.0.1 on both Ubuntu and Windows. If it doesn't work for you post fixes here.
Try using Anaconda.
Using the concept of Anaconda environments, let’s say you need Python 3 to learn programming, but you don’t want to wipe out your Python 2.7 environment by updating Python. You can create and activate a new environment named "snakes" (or whatever you want), and install the latest version of Python 3 as follows:
conda create --name snakes python=3
Its simpler than it sounds, take a look at the intro page here: Getting Started with Anaconda
And then to handle your specific problem of having version 2.x and 3.x running side by side, see:
Managing Python Versions with Anaconda
Switching between Python 2 and Python 3 environments
As far as I know Python runs off of the commandline using the PATH variable as opposed to a registry setting.
So if you point to the correct version on your PATH you will use that. Remember to restart your command prompt to use the new PATH settings.
The Python installation normally associates .py, .pyw and .pyc files with the Python interpreter. So you can run a Python script either by double-clicking it in Explorer or by typing its name in a command-line window (so no need to type python scriptname.py, just scriptname.py will do).
If you want to manually change this association, you can edit these keys in the Windows registry:
HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command
Python Launcher
People have been working on a Python launcher for Windows: a lightweight program associated with .py and .pyw files which would look for a "shebang" line (similar to Linux et al) on the first line, and launch Python 2.x or 3.x as required. See "A Python Launcher for Windows" blog post for details.
Here is how to run Python 2 and 3 on the same machine
install Python 2.x
install Python 3.x
Start Powershell
Type Python -2 to launch Python 2.x
Type Python -3 to launch Python 3.x
The Python Launcher for Windows was embedded into Python since Version 3.3, as promised in 2011 when the Stand alone first made its debut:
Python Launcher for Windows
Easy-peasy ,after installing both the python versions add the paths to the environment variables ;see. Then go to python 2 and python 3 folders and rename them to python2 and python3 respectively as shown and . Now in cmd type python2 or python3 to use your required version see .
I think there is an option to setup the windows file association for .py files in the installer. Uncheck it and you should be fine.
If not, you can easily re-associate .py files with the previous version. The simplest way is to right click on a .py file, select "open with" / "choose program". On the dialog that appears, select or browse to the version of python you want to use by default, and check the "always use this program to open this kind of file" checkbox.
You should make sure that the PATH environment variable doesn't contain both python.exe files ( add the one you're currently using to run scripts on a day to day basis ) , or do as Kniht suggested with the batch files .
Aside from that , I don't see why not .
P.S : I have 2.6 installed as my "primary" python and 3.0 as my "play" python . The 2.6 is included in the PATH . Everything works fine .
Before I courageously installed both simultaneously, I had so many questions. If I give python will it go to py3 when i want py2? pip/virtualenv will happen under py2/3?
It seems to be very simple now.
Just blindly install both of them. Make sure you get the right type(x64/x32).
While/after installing make sure you add to the path to your environment variables.
[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")
Replace the x in the command above to set the path.
Then go to both the folders.
Navigate to
python3.6/Scripts/
and rename pip to pip3.
If pip3 already exists delete the pip. This will make sure that just pip will run under python2. You can verify by:
pip --version
In case you want to use pip with python3 then just use
pip3 install
You can similarly do the same to python file and others.
Cheers!
I had the same problem where I wanted to use python3 for most work but IDA pro required python2. SO, here's what I did.
I first created 3 variables in the user environment variable as follows:
PYTHON_ACTIVE : This is initially empty
HOME_PYTHON27 : Has a path to a folder where Python 2 is installed. Eg. ";/scripts;"
HOME_PYTHON38 : Similar to python 2, this variable contains a path to python 3 folders.
Now I added
%PYTHON_ACTIVE%
to PATH variable. So, basically saying that whatever this "PYTHON_ACTIVE" contains is the active python. We programmatically change the contains of "PYTHON_ACTIVE" to switch python version.
Here is the example script:
:: This batch file is used to switch between python 2 and 3.
#ECHO OFF
set /p choice= "Please enter '27' for python 2.7 , '38' for python 3.8 : "
IF %choice%==27 (
setx PYTHON_ACTIVE %HOME_PYTHON27%
)
IF %choice%==38 (
setx PYTHON_ACTIVE %HOME_PYTHON38%
)
PAUSE
This script takes python version as input and accordingly copies HOME_PYTHON27 or HOME_PYTHON38 to PYTHON_ACTIVE. Thus changing the global Python version.
I would assume so, I have Python 2.4, 2.5 and 2.6 installed side-by-side on the same computer.
I am just starting out with python now. I'm reading Zed Shaw's book "Learn Python the Hard Way" which requires python version 2.x but am also taking a class that requires python 3.x
So here is what I did.
Download python 2.7
run power shell (should already be installed on windows)
run python IN POWERSHELL (if it doesn't recognize then go to step 4)
Only if powershell doesn't recognize python 2.7 type in the following:
"[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHON27", "USER")"
(no outside quotes)
Now type python and you should see it say python 2.7 blah blah blah
NOW for python 3.x
Simple, python 3.x download comes with python for windows app. SO simply pin the Python for Windows app to your task bar, or create shortcut to the desktop and you are done!
Open Python for Windows for 3.x
Open Powershell for python 2.x
I hope this helps!
Hmm..I did this right now by just downloading Python 3.6.5 for Windows at https://www.python.org/downloads/release/python-365/ and made sure that the launcher would be installed. Then, I followed the instructions for using python 2 and python 3. Restart the command prompt and then use py -2.7 to use Python 2 and py or py -3.6 to use Python 3. You can also use pip2 for Python 2's pip and pip for Python 3's pip.

pandas 0.13 system error: cannot set thread affinity mask

this is my first stack overflow question so please pardon any ignorance about the forum on my part.
I am using python 2.7.6 64 bit and pandas 0.13.1-1 from Enthought Canopy 1.3.0.1715 on a win 7 machine. I have numpy 1.8.0-1 and numexpr 2.2.2-2.
I have inconsistent error behviour running the following on a pandas series of 10,000 numpy.float64 loaded from hdf:
import python
s = pandas.read_hdf(r'C:\test\test.h5', 'test')
s/2.
This gives me inconsistent behaviour, it sometimes works and sometimes throws:
OMP: Error #134: Cannot set thread affinity mask.
OMP: System error #87: The parameter is incorrect.
I have replicated this error on other machines, and the test case is derived from a unit test failure (with the above error) which was replicted on several machines and from a server. This has come up in an upgrade from pandas 0.12 to pandas 0.13.
The following consistantly runs with no error:
import python
s = pandas.read_hdf(r'C:\test\test.h5', 'test')
s.apply(lambda x: x/2.)
and,
import python
s = pandas.read_hdf(r'C:\test\test.h5', 'test')
pandas.computation.expressions.set_use_numexpr(False)
s/2.
Thanks for the help.
This is a very similar problem to as described in this issue, and the linked issue
It seems that only canopy is experiencing these issues. I think it has to do with the canopy numpy MKL build, but that is a guess as I have not been able to reproduce this. So here are some work-arounds:
try to upgrade numexpr to 2.4 (current version), or downgrade to 2.1
try to use numpy 1.8.1 via canopy
try to install numpy/numexpr from the posted binaries here; I don't know exactly how canopy works so not sure if this is possible
uninstall numexpr
you can also disable numexpr support via pandas.computation.expressions.set_use_numexpr(False). Note that numexpr is REQUIRED in order to read/use HDF5 files via PyTables. However the expressions disabling of numexpr should just disable it for 'computations' (and not the HDF access).

Using Point Cloud Library in Python

Hello I am trying to use Point Cloud Library in Python and as I am new to this C++ library and Python I am following the tutorial on http://strawlab.github.io/python-pcl/#pcl.PointCloud
However whenever I try to import pcl and define a module such as pcl.PointCloud() I get the following error;
AttributeError: 'module' object has no attribute 'PointCloud'
I have used sys.path.append to point to the correct directory where the PCL files have been installed as they are not in site packages. It says on the above link that the C++ code has already got Python bindings but I am unsure exactly what files I am trying to call in Python. Does anybody know how to overcome this error and load these modules?
Thanks,
Andrew
Try this,
sudo add-apt-repository ppa:sweptlaser/python3-pcl
sudo apt update
sudo apt install python3-pcl
After you've done that then you should be able to run:
python3 -c 'import pcl'
and it will return with no error (as opposed to ModuleNotFoundError: No module named 'pcl').
Tested on:
Ubuntu 18.04 LTS
Python 3.6.9
Reference: https://askubuntu.com/a/1170661/922137
Try skipping the parentheses:
from pcl import PointCloud
And then create an instance of PointCloud, for example, p:
p = PointCloud()
Hope it helps!
Looks like you need PCL 1.5.1 and cython 0.16 according to this page: http://strawlab.github.io/python-pcl/
"""https://blog.pollithy.com/python/numpy/pointcloud/tutorial-pypcd"""
python3.6 -m pip install --user git+https://github.com/DanielPollithy/pypcd.git

How to run neato from pygraphviz on Windows

I am trying to use pygraphviz and networkx in python (v 2.7) to create a network map. I found a script that looks very useful on stackoverflow:
import networkx as nx
import numpy as np
import string
import pygraphviz
dt = [('len', float)]
A = np.array([(0, 0.3, 0.4, 0.7),
(0.3, 0, 0.9, 0.2),
(0.4, 0.9, 0, 0.1),
(0.7, 0.2, 0.1, 0)
])*10
A = A.view(dt)
G = nx.from_numpy_matrix(A)
G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())),string.ascii_uppercase)))
G = nx.to_agraph(G)
G.node_attr.update(color="red", style="filled")
G.edge_attr.update(color="blue", width="2.0")
G.draw('/tmp/out.png', format='png', prog='neato')
I get an error on the last line, basically it cannot find neato:
"ValueError: Program neato not found in path."
The error refers to the agraph.py file for pygraphviz, but I cannot see anything that could be causing the problem when I look through agraph.py
Any ideas how to resolve this? I am using windows and IDLE for my coding.
Thanks!
I had the same problem. Here's what I did in case anyone else is struggling to get pygraphvis working on Windows.
First off, I installed graphviz. I tried to install pygraphvis thrugh pip, but it refused to work. Eventually, I found the unofficial Windows binaries, so I installed that. Importing the module now works, but calling G.layout() led to the above error.
Calling neato -V worked, so it was on my PATH. I figured out that the problem was that python was running in a command prompt that was created prior to installing pygraphvis, so PATH wasn't updated. Restarting the command prompt fixed this, but led to a new error, something about C:\Program not being a valid command.
I figured that pygraphvis was probably failing to quote the path correctly, meaning it cuts off at the space in Program Files. I solved the problem by symlinking it to a path without spaces.
mklink /d C:\ProgramFilesx86 "C:\Program Files (x86)"
Note that this must be run in admin mode. You can do it by going to the start menu, typing in cmd, and then hitting Ctrl+shift+enter.
After this, I edited my PATH to refer to the symlink, restarted cmd, and everything worked.
The problem is that pygraphviz call an external program, a part of the graphviz suite called neato, to draw the graph. What is happening is that you doesn't have graphviz installed and when python try to call it it complains about not finding it. Actually pygraphviz is just a wrapper that gives you the possibility to call graphviz from inside python, but per se doesn't do anything and doesn't install graphviz by default.
The easiest solution is to try a different solution for the plot instead of neato. the accepted option are:
neato
dot
twopi
circo
fdp
nop
try one of those and see if one of them works. Otherwise you can install graphviz, that will give you the required program. It's and open-source program available on every platform, so it shouldn't be a problem to install it.
see at http://www.graphviz.org/
If you simply need to have a sketch of the graph you can use the networkx.draw function on a networkx graph, that uses matplotlib to create an interactive plot.
import networkx as nx
G = G=nx.from_numpy_matrix(A)
nx.draw(G)
Your problem is that neato is missing.
neato is part of the graphviz suite which you can install on your PC e.g. from here. (I used the .msi)
Now neato is "installed", but your system does not know where. So add the directory where neato.exe is contained in to your PATH environment variable. On Win10, this can be done with Start -> Edit environment variables for your account -> select path in the upper window -> edit -> New -> C:\Program Files (x86)\Graphviz2.38\bin\
or whatever your install directory is.
There is probably more than one cause for this error, but if it is caused by a missing path to the graphviz modules [neato,dot,twopi,circo,fdp,nop], then there is one hack that worked for me. I'm currently asking what the correct solution is, but you can use this
if not 'C:\\Program Files (x86)\\Graphviz2.38\\bin' in os.environ["PATH"]:
os.environ["PATH"] += os.pathsep + 'C:\\Program Files (x86)\\Graphviz2.38\\bin'
at the beginning of your script. To generalize, if your graphviz files are saved somewhere else:
graph_path='your_bin_folder_path'
if not graph_path in os.environ["PATH"]:
os.environ["PATH"] += os.pathsep + graph_path
In particular, this worked on windows 10, using anaconda navigator and python version 3.7.
Try something like this to see where pygraphviz thinks your external programs are:
# Get paths of graphviz programs
import pygraphviz as pgv
A = pgv.AGraph()
progs_list = ['neato', 'dot', 'twopi', 'circo', 'fdp', 'nop', 'wc', 'acyclic', 'gvpr',
'gvcolor', 'ccomps', 'sccmap', 'tred', 'sfdp', 'unflatten']
for prog in progs_list:
try:
runprog = A._get_prog(prog)
print(f'{runprog}')
except ValueError as e:
print(f'{prog} gets this error: {str(e).strip()}')
After looking at the results, it's a lot of work outside your IDE installing Graphviz and setting up your Path environmental variable in the System control panel, etc.