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

Running pipenv shell in oh-my-posh doesn't set VIRTUAL_ENV #6226

Open
MrPaulAR opened this issue Aug 29, 2024 · 5 comments
Open

Running pipenv shell in oh-my-posh doesn't set VIRTUAL_ENV #6226

MrPaulAR opened this issue Aug 29, 2024 · 5 comments
Labels
ai-triaged Contributor Candidate The issue has been identified/triaged and contributions are welcomed/encouraged.

Comments

@MrPaulAR
Copy link

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-vm and python
    • asdf plugin-add python
    • asdf install python latest
    • asdf global python latest
    • pip install pipenv
  • Install oh-my-posh
  • Below is the contents of my .zshrc file
  • mkdir tmp; cd tmp
  • Create pipenv & venv
    • pipenv install
    • python -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:

  • PATH
  • SHELL
  • ZLE_RPROMPT_INDENT
  • POSH_PID
  • SSH_AUTH_SOCK
  • PWD
  • LOGNAME
  • XDG_SESSION_TYPE
  • ASDF_DEFAULT_TOOL_VERSIONS_FILENAME
  • MOTD_SHOWN
  • HOME
  • LANG
  • POSH_SHELL_VERSION
  • OSTYPE
  • CONDA_PROMPT_MODIFIER
  • ASDF_DATA_DIR
  • SSH_CONNECTION
  • XDG_SESSION_CLASS
  • TERM
  • POSH_PROMPT_COUNT
  • ASDF_DIR
  • USER
  • SHLVL
  • XDG_SESSION_ID
  • POSH_THEME
  • XDG_RUNTIME_DIR
  • SSH_CLIENT
  • DEBUGINFOD_URLS
  • ASDF_CONFIG_FILE
  • DBUS_SESSION_BUS_ADDRESS
  • MAIL
  • SSH_TTY
  • POWERLINE_COMMAND
  • OLDPWD
  • PIP_DISABLE_PIP_VERSION_CHECK
  • PYTHONDONTWRITEBYTECODE
  • PIP_PYTHON_PATH
  • PIPENV_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/bin
  • SHELL: /usr/bin/zsh
  • LANG: C.UTF-8
  • PWD: /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.

@CVirus
Copy link

CVirus commented Sep 5, 2024

Facing exactly the same issue here.

@MrPaulAR
Copy link
Author

MrPaulAR commented Sep 6, 2024

There is a discussion in JanDeDobbeleer/oh-my-posh#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.

@matteius
Copy link
Member

matteius commented Sep 6, 2024

Is this something that can be worked into: #6230

@oz123
Copy link
Contributor

oz123 commented Sep 10, 2024

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.

@matteius matteius added the Contributor Candidate The issue has been identified/triaged and contributions are welcomed/encouraged. label Sep 14, 2024
@matteius
Copy link
Member

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 --fancy solution 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 --fancy flag as a workaround. This should be added to the pipenv shell command'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 --fancy flag. 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_ENV override and explore potential solutions within Oh My Posh itself.
  • Explore Shell Detection: While the current --fancy solution 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 --fancy flag.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai-triaged Contributor Candidate The issue has been identified/triaged and contributions are welcomed/encouraged.
Projects
None yet
Development

No branches or pull requests

4 participants