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 support? #852

Closed
lighth7015 opened this issue Oct 19, 2019 · 40 comments
Closed

Python virtualenv support? #852

lighth7015 opened this issue Oct 19, 2019 · 40 comments
Labels
enhancement New feature or request investigate this requires investigation Stale used for marking issues and prs as stale

Comments

@lighth7015
Copy link

Is there a way to enter a python virtualenv from nushell?

@sophiajt
Copy link
Member

I don't believe so. We're currently working on adding environment support, and this is a good test case for the kind of thing we should support.

@andrasio andrasio added enhancement New feature or request investigate this requires investigation labels Oct 23, 2019
@estysdesu
Copy link

So the wait is until nu supports temporary environments? Is this on the roadmap (is there a roadmap)?

@thegedge
Copy link
Contributor

Is this on the roadmap (is there a roadmap)?

Not yet. For now, just some pinned issues in the repo. I'll chat with the team to see if we can better present what is priority for reaching 1.0 🙂

@Halkcyon
Copy link

Halkcyon commented Nov 18, 2020

I imagine this will need to tie into #291 and require engagement from pypa to integrate a nushell script into virtualenv.

At the end of the day, venvs just mutate environment variables which is possible/straight-forward to do through nu, but not very ergonomic to do by hand.

@cjwyett
Copy link

cjwyett commented May 10, 2021

Hi, is there any update on this? The only thing stopping me from using nu full time...

@sophiajt
Copy link
Member

@cjwyett - we've improved support for environments quite a bit, so it should be possible to support Nushell now from virtualenv. If it's not, we should be close, and I'd be happy to continue adding additional support to help that work.

@github-actions
Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the Stale used for marking issues and prs as stale label Jun 24, 2021
@github-actions
Copy link

github-actions bot commented Jul 7, 2021

This issue has been marked stale for more than 10 days without activity. Closing this issue, but if you find that the issue is still valid, please reopen.

@github-actions github-actions bot closed this as completed Jul 7, 2021
@cjwyett
Copy link

cjwyett commented Jul 7, 2021

Is this fixed? I still can't activate a python venv with source myenv/bin/activate like in other shells

@igitur
Copy link

igitur commented Jul 7, 2021

It's not fixed. Overzealous github bots at it again. Please reopen, @jonathandturner .

@Tehada
Copy link

Tehada commented Sep 17, 2021

Also this seems like some kind support should be implemented from the virtualenv side, currently the first lines of activate file contain this message:

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

which kinda implies that it is not compatible with nushell.

@jokeyrhyme
Copy link
Contributor

Some comments from other issues:

I've just read through this issue, https://www.nushell.sh/blog/2020-06-09-nushell_0_15_0.html#per-directory-environment-variables-samhedin , #2083 , #2117 , and #2130, #3872

Figured it was worth linking these all together so others can keep up with related conversations

There's also the work for python virtual environments going on here: https://github.com/nushell/nu_scripts/tree/main/virtual_environments

@ngmisl
Copy link

ngmisl commented Apr 21, 2022

any news?

@flying-sheep
Copy link
Contributor

flying-sheep commented May 4, 2022

The standalone virtualenv module (superset of the builtin venv) will by default create a <venv>/bin/activate.nu

(For Arch users: the Arch package is outdated and won’t create a working nushell activation script, you need virtualenv>=20.14.0)

@phiresky
Copy link
Contributor

The script generated by virtualenv seems to be broken with nushell 0.67.0:

Error: nu::parser::parse_mismatch (link)

  × Parse mismatch during operation.
    ╭─[activate.nu:10:1]
 10 │ 
 11 │     let is-windows = ((sys).host.name | str downcase) == 'windows'
    ·         ─────┬────
    ·              ╰── expected valid variable name
 12 │     let virtual-env = '.../.venv'
    ╰────

@igitur
Copy link

igitur commented Aug 18, 2022

Which version of virtualenv are you using? v20.16.3 seems to work fine on nushell 0.67 for me.

@phiresky
Copy link
Contributor

phiresky commented Aug 18, 2022

virtualenv version 20.16.3, python version 3.10.6

Seems to be caused by this: https://www.nushell.sh/blog/2022-08-16-nushell-0_67.html#new-variable-naming-convention-jt

@igitur
Copy link

igitur commented Aug 18, 2022

What is your output when you execute these commands?

C:\> cd C:\temp\testvenv
C:\temp\testvenv〉virtualenv .venv                                                                                                                       
created virtual environment CPython3.9.6.final.0-64 in 6122ms
  creator CPython3Windows(dest=C:\temp\testvenv\.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\asdf\AppData\Local\pypa\virtualenv)
    added seed packages: pip==22.1.2, setuptools==63.2.0, wheel==0.37.1
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
C:\temp\testvenv〉source .\.venv\Scripts\activate.nu                                                                                                     
(.venv) C:\temp\testvenv〉                                                                              

@phiresky
Copy link
Contributor

❯ virtualenv .env
created virtual environment CPython3.10.6.final.0-64 in 187ms
  creator CPython3Posix(dest=/tmp/16.12/.env, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/tehdog/.local/share/virtualenv)
    added seed packages: pip==22.2.2, setuptools==63.2.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
/tmp/16.12
❯ source .env/bin/activate.nu
Error: nu::parser::parse_mismatch (link)

  × Parse mismatch during operation.
    ╭─[activate.nu:10:1]
 10 │
 11 │     let is-windows = ((sys).host.name | str downcase) == 'windows'
    ·         ─────┬────
    ·              ╰── expected valid variable name
 12 │     let virtual-env = '/tmp/16.12/.env'
    ╰────

@igitur
Copy link

igitur commented Aug 18, 2022

Line 11 of my activate.nu is let is_windows = ((sys).host.name | str downcase) == 'windows' . Note the underscore. I'm still sure you're using an outdated virtualenv. Also see https://github.com/pypa/virtualenv/pull/2386/files

@phiresky
Copy link
Contributor

Ah, I guess I have 20.16.3 in my virtualenv but 20.15.1 in my system, and it seems to not overwrite the script when it already exists (but is broken)

@flying-sheep
Copy link
Contributor

flying-sheep commented Aug 19, 2022

If anyone uses Arch’s python-virtualenv package: Friendly ping for @felixonmars: Please update it so we can create virtual environments that work with our shell.

@FrancescoSaverioZuppichini

news?

@whyboris
Copy link

whyboris commented Nov 1, 2022

When I run my command source ~/.popo/venv/bin/activate I get this:

  × Missing required positional argument.
   ╭─[activate:4:1]
 4 │ 
 5 │ if [ "${BASH_SOURCE-}" = "$0" ]; then
   ·                                ▲
   ·                                ╰── missing cond
 6 │     echo "You must source this script: \$ source $0" >&2
   ╰────
  help: Usage: if <cond> <then_block> (else <else_expression>)

🤷

@jokeyrhyme
Copy link
Contributor

@whyboris nu does not support the same syntax that is understood by sh / bash / zsh / etc

So, the script that is generated by virtualenv for Python will not work in nushell

@whyboris
Copy link

whyboris commented Nov 2, 2022

Thank you @jokeyrhyme for the explanation. I don't mind -- I can use zsh for my work 👌

Just wanted to report in on this issue -- since it seemed like there was intent to make virtualenv work with nu initially.

Is the new verdict that nu will not likely support virtualenv until at least after 2023? 🤔

@flying-sheep
Copy link
Contributor

flying-sheep commented Nov 3, 2022

So, the script that is generated by virtualenv for Python will not work in nushell

Not quite. The script <venv>/bin/activate is for POSIXy shells, yes, but virtualenv also creates <venv>/bin/activate.nu

Is the new verdict that nu will not likely support virtualenv until at least after 2023? 🤔

It’s the other way around. virtualenv supports nu. And as implied above, it does so already, you just tried to source the wrong file.

@whyboris
Copy link

whyboris commented Nov 3, 2022

Thank you @flying-sheep -- I see the activate.nu file, but I can't activate it 😓
source ~/.popo/venv/bin/activate.nu doesn't seem to work:

/Users/byakubchik/.popo/venv/bin〉source ~/.popo/venv/bin/activate.nu                                                                                                                     11/03/2022 10:20:46 AM
Error: nu::parser::parse_mismatch (link)

  × Parse mismatch during operation.
   ╭─[activate.nu:1:1]
 1 │ # Setting all environment variables for the venv
 2 │ let path-name = (if ((sys).host.name == "Windows") { "Path" } { "PATH" })
   ·                                                    ─────┬────
   ·                                                         ╰── expected operator
 3 │ let virtual-env = "/Users/byakubchik/.popo/venv"
   ╰────

@flying-sheep
Copy link
Contributor

flying-sheep commented Nov 3, 2022

nu changes fast, so old syntax isn’t necessarily supported anymore. You probably need to recreate the scripts with an updated virtualenv version. virtualenv will leave the existing packages intact, you just need to be careful to use the same python version the virtualenv was originally created with, e.g.:

python3.9 -m virtualenv ~/.popo

or

python -m virtualenv -p python3.9 ~/.popo

@harrysarson
Copy link

I had to update to virtualenv v20.21.0 to support nushell 0.77.0

@alex-way
Copy link

For those wondering the command you need is overlay use <path_to_venv>\Scripts\activate.nu

@jokeyrhyme
Copy link
Contributor

I dearly wish virtualenv had been opinionated about destination directory names

It makes it very hard to detect when a virtualenv exists if you have to search every directory just in case

Really wish it was just .virtualenv and that's it :S

@theoparis
Copy link

I'm getting this:

➜ overlay use ./venv/bin/activate.nu
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
    ╭─[./venv/bin/activate.nu:21:1]
 21 │         # Try to parse 'true', '0', '1', and fail if not convertible
 22 │         let parsed = do -i { $env | get $name | into bool }
    ·                         ─┬
    ·                          ╰── expected operator
 23 │         if ($parsed | describe) == 'bool' {
    ╰────

I'm using nu v0.78.0 if that helps.

@flying-sheep
Copy link
Contributor

the virtualenv version with the newest nushell syntax changes hasn’t been released yet, you can use it like this:

# install dev version of virtualenv
pip install --user git+https://github.com/pypa/virtualenv.git
# update virtualenv with new script
python -m virtualenv /path/to/venv/

@glennDittmann
Copy link

For those wondering the command you need is overlay use <path_to_venv>\Scripts\activate.nu

For me it was overlay use <path_to_venv>\bin\activate.nu, on pop!_os.

@Heidar-An
Copy link

not sure if I'm doing something wrong, but when I get the dev version of virtualenv (and also when I don't), installing in my "virtualenv" still installs the package globally.

@detrin
Copy link

detrin commented Sep 3, 2023

Seems like venv doesn't have the nu support yet. However this worked for me

python3.11 -m pip install virtualenv
python3.11 -m virtualenv env_nu 
overlay use env_nu/bin/activate.nu
pip install -r requirements.txt 

@amtoine
Copy link
Member

amtoine commented Sep 4, 2023

Seems like venv doesn't have the nu support yet. However this worked for me

python3.11 -m pip install virtualenv
python3.11 -m virtualenv env_nu 
overlay use env_nu/bin/activate.nu
pip install -r requirements.txt 

yeah i think we have integration with virtualenv but not python -m venv 👍

@detrin
Copy link

detrin commented Sep 4, 2023

Seems like venv doesn't have the nu support yet. However this worked for me

python3.11 -m pip install virtualenv
python3.11 -m virtualenv env_nu 
overlay use env_nu/bin/activate.nu
pip install -r requirements.txt 

yeah i think we have integration with virtualenv but not python -m venv 👍

Yes, I know python/cpython#108852 (comment)

bobhy pushed a commit to bobhy/nushell that referenced this issue Oct 22, 2023
…#852)

The Direnv recipe no longer worked on the latest version of Nushell, as the hook syntax has changed from code in a string to using blocks.

This updates the recipe accordingly.
@MatrixManAtYrService
Copy link

MatrixManAtYrService commented Feb 26, 2024

As a workaround I've been using sh -i -c 'source venv/bin/activate ; nu' which lets me stick to the python standard library.

You just have to remember that you're now in a subsubshell (sh is your parent, and your original nu is its parent), so instead of invoking deactivate you need to to type exit or press Ctrl+D. When you exit, the -c command finishes and sh exits on its own, so you end up all the way back out to your original nu shell (no need to exit twice).

❯ python -m venv venv
❯ sh -i -c 'source venv/bin/activate ; nu'
venv ❯ exit
❯

I wonder why venv's weren't implemented as subshells in the first place, it seems like a suitable abstraction to me. venv activate would detect the parent shell and invoke it again as a subshell, but with the necessary env args set. Then there would be no need to ask the shell to evaluate code under the assumption that it was compatible, and you'd get cleanup (on exit) for free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request investigate this requires investigation Stale used for marking issues and prs as stale
Projects
None yet
Development

No branches or pull requests