Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python virtualenv not activating in Windows 10 Cmder #1057

Closed
cwmat opened this issue Aug 3, 2016 · 12 comments · Fixed by #1070
Closed

Python virtualenv not activating in Windows 10 Cmder #1057

cwmat opened this issue Aug 3, 2016 · 12 comments · Fixed by #1070

Comments

@cwmat
Copy link

cwmat commented Aug 3, 2016

I can initialize a virtualenv environment using Cmder but when I run the following command:

env/Scripts/activate

Nothing happens (i.e. "(env)" doesn't show up in front of my path in the terminal as its supposed to).

When running the same in regualr cmd it works as intended.

Any thoughts? I was thinking maybe there is some Windows setting that might be preventing Cmder from running a batch file (activate.bat) or a file without an extension (activate). I ran Cmder as admin and it did not change anything though. I posted the full question and background here: http://superuser.com/questions/1108063/python-virtualenv-not-activating-in-windows-10-cmder

@jankatins
Copy link
Contributor

See here: #1056

I think we should do that automatically...

@cwmat
Copy link
Author

cwmat commented Aug 3, 2016

Thanks @JanSchulz . I did the following (below) but I am still running into the same problem. Just want to confirm that the following procedure was correct:

I made a file called "conda.lua" in the config folder with the contents as suggested in #1056

I then added the following line to /vendor/clink.lua:

-- now add our own things...
local conda_lua_file = clink.get_env('CMDER_ROOT')..'\\config\\conda.lua'
dofile(conda_lua_file)

Was this the correct approach? Any ideas why I still might be having issues getting conda to display?

Thank you.

@jankatins
Copy link
Contributor

jankatins commented Aug 3, 2016

Oups, sorry... No, don't change anything in vendor, only place a file in config. The conda example needs to be adjusted (conda is an alternative to virtualenv) . In some place it looks for an environment variable CONDA_DEFAULT_ENV and this needs to be replaced with whatever virtualenv uses for this purpose (probably VIRTUAL_ENV according to my reading of https://github.com/pypa/virtualenv/blob/master/virtualenv_embedded/activate.bat#L2).

@cwmat
Copy link
Author

cwmat commented Aug 3, 2016

Gotcha! Sorry for the confusion. That worked. Thank you. Just changed the CONDA_DEFAULT_ENV to VIRTUAL_ENVand it worked.

It did throw this though:
C:\PROGRA~1\CMDER_~1\config\venv.lua:8: attempt to call global 'basename' (a nil value)

I don't know much about LUA. Any reason you can think of off the top of your head as to why this message would get shown? If not no worries! You have given me enough information for me to go do some research and I can close the issue.

Thank you for all of the help so far!

@lpinner
Copy link

lpinner commented Aug 3, 2016

@cwmat
Copy link
Author

cwmat commented Aug 4, 2016

@lpinner this worked thanks! No more error message.

@cwmat cwmat closed this as completed Aug 4, 2016
@lpinner lpinner mentioned this issue Aug 4, 2016
@Tset-Noitamotua
Copy link

Hi! Cool this helped me, too on a Windows 7 64 bit machine.

The name of active virtual python environment (virtual env) was missing in cmder (see screenshots below).

This is how it looks in standard cmd (color is added in scrennshot - original is not colored):

image

And thats how it looked in cmder before I aplied your fix:

image

Looks great after fix:

image

As I am not using conda or something specific I renamed/removed conda to python

local clink_path_lua_file = clink.get_env('CMDER_ROOT')..'\\vendor\\clink-completions\\modules\\path.lua'
dofile(clink_path_lua_file)

function get_virtual_env(env_var)
    env_path = clink.get_env(env_var)
    if env_path then
        basen = exports.basename(env_path)
        return basen
    end
    return false
end

---
 -- add python virutual env name 
---
function conda_prompt_filter()
    -- add in python virtual env name
    local python_env = get_virtual_env('DEFAULT_ENV')
    if python_env then
        clink.prompt.value = string.gsub(clink.prompt.value, "λ", "["..python_env.."] λ")
    end
end

---
 -- add virtual env name 
---
function venv_prompt_filter()
    -- add in virtual env name
    local venv = get_virtual_env('VIRTUAL_ENV')
    if venv then
        clink.prompt.value = string.gsub(clink.prompt.value, "λ", "["..venv.."] λ")
    end
end

Now everything works and looks nice! Thank you guys!!!

@jankatins
Copy link
Contributor

jankatins commented Aug 8, 2016

So, what should we do here? I think cmder will get a lot of complains about missing environment names. I can guess a few ways to prevent that:

  • Add the above functions and prompt filters -> these will need updating for more environment commands (i hear node also has one?)
  • parse the actual prompt (e.g. in a prompt filter at step 0). This means that such a filter explicitly relies on the default prompt value and that all such environment commands will do it in a similar way (e.g. get the environment name as the thing between the brackets (...).
  • Put up a big FAQ about this and that users have to add such filters themselves...

My preference would be the parsing (I think the problem with the garbled unicode path names makes parsing needed anyway).

@Stanzilla
Copy link
Member

+1 for parsing

@jankatins
Copy link
Contributor

Please try #1070 for a version which parses the original prompt.

@Mechatron-NZ
Copy link

I cant get this to work for venv i placed the file.lua in C:\Program Files\cmder\config
but when I run my venv\scripts\activate.bat it doesn't do anything am I missing some step

@Victorciobanu
Copy link

https://www.yodiaditya.com/cmder-bashrc-and-show-virtualenv-in-windows-10/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants