124

I'm trying to get virtualenv to work with the fish shell. I have virtualenv installed and it works fine with bash and zsh. However, running the following command returns fish: Unknown command 'source':

$ source ~/path/to/bin/activate

Does anyone know how to get virtualenv and the fish shell to work together?

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
drbunsen
  • 10,139
  • 21
  • 66
  • 94
  • 2
    I've started to use https://github.com/adambrenecki/virtualfish works very well for my projects. –  Jul 26 '12 at 11:12

9 Answers9

280

You don't need to activate to use virtualenv it is a convenience. You can just use the virtualenv directly:

virtualenv venv
./venv/bin/pip install foo

Have you tried from fish using:

. venv/bin/activate.fish

It probably isn't as widely used as bash so may have issues - looking at the commit history shows a recent fix:

https://github.com/pypa/virtualenv/blob/master/virtualenv_embedded/activate.fish

Aiden Bell
  • 28,212
  • 4
  • 75
  • 119
  • 1
    Thanks. I had tried `active.fish`, but I neeed to update virtualenv to the latest version to get it working. – drbunsen Jun 10 '12 at 11:23
  • If you come across specific bugs please file at http://github.com/pypa/virtualenv Issue tracker –  Jun 14 '12 at 14:46
  • 2
    @Lee I am getting this issue. https://paste.ubuntu.com/25955380/ What does "." here refer to? – Abhishek Bhatia Nov 13 '17 at 17:39
  • 2
    Oh yeah, I'm going to type the full path every time I use something. Sounds great. – Alper Nov 11 '20 at 12:57
  • @AbhishekBhatia The answer below shows that virtualenv now has a fish activation so you don't need to do that period. Just `source bin/activate.fish` – Hercislife Mar 22 '21 at 15:20
61

For virtualenv, fish has a separate activation file in the in the bin directory with .fish extension.

So you will have to do:

$ source ~/path/to/bin/activate.fish

Sangeet
  • 946
  • 9
  • 12
  • 5
    This is definitely the way to go for anyone looking at this. The accepted answer is great but it was probably before this was an option. – Hercislife Mar 22 '21 at 15:19
  • 1
    PS: the documentation about this command is available in https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment – adrianosymphony Oct 04 '22 at 01:02
24

You can also use this : https://github.com/adambrenecki/virtualfish

It allows you to activate a virtualenv by typing this :

vf activate <my_env>
devict
  • 589
  • 4
  • 8
6

You can use virtualfish.

A Fish Shell wrapper for Ian Bicking’s virtualenv, somewhat loosely based on Doug Hellman’s virtualenvwrapper for Bourne-compatible shells.

Source: https://github.com/adambrenecki/virtualfish

Docs: http://virtualfish.readthedocs.org/en/latest/

Arie
  • 11,231
  • 1
  • 15
  • 11
2

If you can't use activate.fish, you can just add the bin directory to your PATH:

set -gx PATH /path/to/virtualenv/bin $PATH

That's pretty much all activate.fish does (well, not quite, it also unsets PYTHONHOME, (which wasn't set beforehand when I tried it anyway, YMMV); and it tries to mess with your fish_prompt).

Alternatively: I'm a former Bash user who's started using Fish and misses Doug Hellman's virtualenvwrapper, so I've just today started working on a replacement called virtualfish - it has a few handy shortcuts you might find useful, although it's nowhere near as complete as VEW.

Daisy Leigh Brenecki
  • 7,571
  • 6
  • 28
  • 43
2

(This thread seems close to be closed, but I found a solution :)

To enter a new fish shell with venv envrionment:

begin; set -lx PATH (realpath ./venv)/bin $PATH; fish; end

when the venv directory is ./venv.

To deactivate, just ctrl-d or exit.


Another solution, which does not invoke a child shell.

Make and enter a venv envrionment:

python3 -m venv ./venv
set -lx PATH (realpath ./venv)/bin $PATH

Exit from the envrionment:

set -lx PATH $PATH[2..-1]
1

If it's an env file try this .env/bin/activate.fish make sure your env file is inside your project file, in my case it's a django project. Tt worked for me

0

You can use the command - set VIRTUAL_ENV 'path to the virtual env directory' Example - set VIRTUAL_ENV '/home/aman/Desktop/test/venv'

0

You may can do the same but changes it to:

source env/bin/activate.fish
starball
  • 20,030
  • 7
  • 43
  • 238