pipenv
pipenv copied to clipboard
Running pipenv shell in oh-my-posh doesn't set VIRTUAL_ENV
Be sure to check the existing issues (both open and closed!), and make sure you are running the latest version of Pipenv.
Check the diagnose documentation for common issues before posting! We may close your issue if it is very similar to one of them. Please be considerate, or be on your way.
Make sure to mention your debugging experience if the documented solution failed.
Issue description
On a zsh shell on Arch, Ubuntu, & WSL when I execute pipenv shell the env variable VIRTUAL_ENV is not being set. If I create a traditional venv and activate it then the variable is being set.
Versions:
- zsh: 5.8.1
- python: 3.12.5
- pipenv: 2024.0.1
Expected result
After executing pipenv shell typing echo $VIRTUAL_ENV should show the name of the virtual environment.
Actual result
When possible, provide the verbose output (--verbose), especially for locking and dependencies resolving issues.
$ pipenv shell --verbose
Launching subshell in virtual environment...
. /home/tmpuser/.local/share/virtualenvs/tmp-g6-Tn6kM/bin/activate
--verbose
$ echo $VIRTUAL_ENV
$ source venv/bin/activate
$ echo $VIRTUAL_ENV
/home/tempuser/tmp/venv
Steps to replicate
- Create a blank user account
- Install
asdf-vmand pythonasdf plugin-add pythonasdf install python latestasdf global python latestpip install pipenv
- Install
oh-my-posh - Below is the contents of my .zshrc file
mkdir tmp; cd tmp- Create pipenv & venv
pipenv installpython -m venv venv
Inside this temp dir I can activate either of the virutal enviroments but only the venv one sets the VIRTUAL_ENV env variable.
$ pipenv --support
Pipenv version: '2024.0.1'
Pipenv location: '/home/tmpuser/.asdf/installs/python/3.12.5/lib/python3.12/site-packages/pipenv'
Python location: '/home/tmpuser/.asdf/installs/python/3.12.5/bin/python3.12'
OS Name: 'posix'
User pip version: '24.0'
user Python installations found:
PEP 508 Information:
{'implementation_name': 'cpython',
'implementation_version': '3.12.5',
'os_name': 'posix',
'platform_machine': 'x86_64',
'platform_python_implementation': 'CPython',
'platform_release': '6.8.12-1-pve',
'platform_system': 'Linux',
'platform_version': '#1 SMP PREEMPT_DYNAMIC PMX 6.8.12-1 (2024-08-05T16:17Z)',
'python_full_version': '3.12.5',
'python_version': '3.12',
'sys_platform': 'linux'}
System environment variables:
PATHSHELLZLE_RPROMPT_INDENTPOSH_PIDSSH_AUTH_SOCKPWDLOGNAMEXDG_SESSION_TYPEASDF_DEFAULT_TOOL_VERSIONS_FILENAMEMOTD_SHOWNHOMELANGPOSH_SHELL_VERSIONOSTYPECONDA_PROMPT_MODIFIERASDF_DATA_DIRSSH_CONNECTIONXDG_SESSION_CLASSTERMPOSH_PROMPT_COUNTASDF_DIRUSERSHLVLXDG_SESSION_IDPOSH_THEMEXDG_RUNTIME_DIRSSH_CLIENTDEBUGINFOD_URLSASDF_CONFIG_FILEDBUS_SESSION_BUS_ADDRESSMAILSSH_TTYPOWERLINE_COMMANDOLDPWDPIP_DISABLE_PIP_VERSION_CHECKPYTHONDONTWRITEBYTECODEPIP_PYTHON_PATHPIPENV_ACTIVE_PYTHONFINDER_IGNORE_UNSUPPORTED
Pipenv–specific environment variables:
PIPENV_ACTIVE:1
Debug–specific environment variables:
PATH:/home/tmpuser/.asdf/installs/python/3.12.5/bin:/home/tmpuser/.asdf/plugins/python/shims:/home/tmpuser/.asdf/installs/python/3.12.5/bin:/home/tmpuser/.asdf/shims:/home/tmpuser/.asdf/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/tmpuser/.local/bin:/home/tmpuser/.local/binSHELL:/usr/bin/zshLANG:C.UTF-8PWD:/home/tmpuser/temprepo
Contents of Pipfile ('/home/tmpuser/temprepo/Pipfile'):
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"
[packages]
[dev-packages]
[requires]
python_version = "3.12"
I'm not sure if this is oh-my-posh squashing the env var or pipenv not setting it, if nothing else hoping for some direction.
Facing exactly the same issue here.
There is a discussion in https://github.com/JanDeDobbeleer/oh-my-posh/discussions/5573 where @CVirus identified that if you create a shell with pipenv shell --fancy things work as expected. Not sure if this is a pipenv or oh-my-posh issue so not closing this as of yet.
Is this something that can be worked into: https://github.com/pypa/pipenv/pull/6230
I don't think that a fix for the issue should be incorporated into the PR you mentioned, as it is a local fix for a thing which already worked. I believe the fancy solution is OK. If needed, I am willing to open another PR to fix this issue, as it is easy for me to test.
Analysis of Pipenv Issue #6226
1. Problem Summary:
The issue reports that pipenv shell does not set the VIRTUAL_ENV environment variable when used in a Zsh shell with Oh My Posh. This only occurs when using the default shell invocation. Using the --fancy flag resolves the issue, indicating a potential incompatibility between Pipenv's default shell activation and Oh My Posh.
2. Comment Analysis:
- Another user confirms experiencing the same issue, suggesting a widespread problem.
- A contributor suggests investigating the possibility of incorporating a fix into a related PR.
- Another contributor correctly points out that the existing
--fancysolution is a suitable workaround and avoids modifying previously working functionality.
3. Proposed Resolution:
The problem likely stems from Oh My Posh overriding the VIRTUAL_ENV variable after Pipenv sets it during the default shell activation. The --fancy flag works because it employs a different activation mechanism that's less susceptible to interference from Oh My Posh.
Given that a working solution exists with --fancy, directly modifying Pipenv's core logic might introduce unintended consequences. Instead, we can address the issue with the following approaches:
- Documentation: Clearly document the incompatibility with Oh My Posh and recommend using the
--fancyflag as a workaround. This should be added to thepipenv shellcommand's help text and potentially the troubleshooting section of the documentation. - Improve Error Handling: Detect when Pipenv is run in a Zsh shell with Oh My Posh and emit a specific warning message recommending the
--fancyflag. This can be done by checking for specific environment variables set by Oh My Posh.
4. Code Snippet (pipenv/cli/command.py):
@pass_state
def shell(state, fancy=False, shell_args=None, anyway=False, quiet=False):
# ... (Existing Code) ...
# Detect Oh My Posh in Zsh
if os.environ.get("POSH_THEME") and shell.cmd.endswith("zsh"):
click.echo(
"Warning: Oh My Posh detected. 'pipenv shell' may not activate the "
"virtual environment correctly. Consider using `pipenv shell --fancy`.",
err=True,
)
# ... (Rest of the code) ...
5. Additional Steps:
- Upstream Collaboration: Engage with the Oh My Posh developers to understand the specific mechanism causing the
VIRTUAL_ENVoverride and explore potential solutions within Oh My Posh itself. - Explore Shell Detection: While the current
--fancysolution works, it might not be ideal for all users. Investigate ways to improve Pipenv's shell detection and activation logic to handle Oh My Posh more gracefully without requiring the--fancyflag.
This issue highlights the complexity of managing environment variables across different shells and customization tools. By documenting the known issue and exploring collaborative solutions, we can provide a more reliable experience for Pipenv users.