James Musk
James Musk

Reputation: 171

TMUX Session Won't Import Python Module

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?

Upvotes: 8

Views: 7872

Answers (3)

Wenuka
Wenuka

Reputation: 1090

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.

Upvotes: 0

JHH11
JHH11

Reputation: 1

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.

Upvotes: 0

tomsgd
tomsgd

Reputation: 1135

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.

Upvotes: 14

Related Questions