8

Hi everyone this is my first question on StackOverflow and I hope it finds everyone well. I recently started using TMUX and I'm having a problem using it for a machine learning problem set I have.

I'm creating a program using python and I'm using the sklearn module. Basically when I run the code in the terminal out my TMUX session, everything works fine. However, when I start a TMUX session and run the code, I get the following error.

Traceback (most recent call last):
  File "hw1.py", line 5, in <module>
    from sklearn import svm
ImportError: No module named sklearn

For some reason, it can't find sklearn even though it is installed and it works fine outside the TMUX session. Here are my import statements.

import numpy 
import scipy.io
from sklearn import svm
from random import sample 

Why can't it find the module while in TMUX and how do I fix this?

Barmar
  • 741,623
  • 53
  • 500
  • 612
James Musk
  • 171
  • 1
  • 2
  • 6
  • 1
    It sounds like a difference in a python-related environment variable. – Barmar Sep 12 '15 at 21:31
  • run `pip freeze > environ.txt` at both the places(in and out of tmux session) and check if there is some difference(or may be post it if you cannot find it). – Barun Sharma Sep 12 '15 at 21:36
  • Odds are that tmux is not using the correct PATH variable. Type which python in and out of tmux and see if it is using the same python install. Or it is the anaconda environment variable. – Charlie Haley Sep 13 '15 at 00:39
  • Hi guys, thanks for the helpful advice. I'm still fairly new to working with the terminal and using PATH variables so I'll learn a little bit about it and let you know if I manage to solve this. As of now, I haven't. – James Musk Sep 14 '15 at 06:46

3 Answers3

14

I ran into the same problem on OS X. It seems that the PATH variable gets messed up when you call tmux while in a non-default anaconda environment. If I run tmux in a new terminal before calling source activate and then activate the environment I want while in tmux then things work as expected. Unfortunately with this workaround I have to remember to call source activate in every pane I open in tmux so it's a less than ideal solution.

tomsgd
  • 1,080
  • 1
  • 11
  • 24
  • 1
    also sometimes you need to `conda deactivate` and `source activate` to get it to work – qwr Jul 08 '19 at 19:27
  • I found the same thing on Ubuntu. If I run tmux from within an environment I can't import modules, if I run tmux from outside of an environment and then activate the environment within tmux, everything seems to work. – Eric Antoine Scuccimarra Aug 27 '19 at 05:33
  • Yeah, have to do conda deactivate before calling tmux – ethanjyx Dec 08 '21 at 20:15
0

I usually use Anaconda to run a python script on MacOS. Back to local from conda environment (maybe called "(base)") using

conda deactivate or source deactivate

and using

tmux

You can import python module successfully.

JHH11
  • 1
0

Solution of @tomsgd works for me.

However, if you need to automate this, simply do the following

cd ~/  # go to the root folder
nano .bashrc  # edit the .bashrc file

and add any cmd(s) you need to run at new boot or at new tmux at the end of the file. For example, I have added the following two lines at the end,

source activate  # as mentioned above by @tomsgs
source activate pytorch  # to activate desired env

Then save the file and exit.

Next time you tried to use Tmux (or boot), you will not have to type it again and again.

Wenuka
  • 887
  • 2
  • 9
  • 25