TMUX Session Won't Import Python Module
Asked Answered
A

3

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?

Anglocatholic answered 12/9, 2015 at 21:12 Comment(4)
It sounds like a difference in a python-related environment variable.Felicio
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).Drew
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.Geilich
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.Anglocatholic
M
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.

Manteltree answered 5/3, 2018 at 13:11 Comment(3)
also sometimes you need to conda deactivate and source activate to get it to workChurchwell
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.Rawinsonde
Yeah, have to do conda deactivate before calling tmuxCapsicum
O
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.

Obligato answered 18/1, 2021 at 7:58 Comment(0)
I
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.

Irrelevant answered 30/11, 2021 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.