Skip to content

Instantly share code, notes, and snippets.

@RchGrav
Last active May 12, 2024 01:00
Show Gist options
  • Save RchGrav/96255019a505b83ac72a85ff4a6ed79c to your computer and use it in GitHub Desktop.
Save RchGrav/96255019a505b83ac72a85ff4a6ed79c to your computer and use it in GitHub Desktop.

Resolving Dependency and Environment Issues with Pyenv and Poetry in WSL

When working with Python development tools like pyenv and poetry within the Windows Subsystem for Linux (WSL), users may encounter conflicts and issues due to path and environment settings. This guide aims to address these issues by outlining the steps necessary to properly configure your environment to avoid conflicts between Windows and Linux tools.

Problem Description

Users might experience errors similar to the following when trying to use pyenv installed on Windows from within WSL:

/mnt/c/Users/USER/.pyenv/pyenv-win/bin/pyenv: 3: cygpath: not found
/mnt/c/Users/USER/.pyenv/pyenv-win/bin/pyenv: 3: exec: cmd: not found

These errors occur because WSL is incorrectly referring to the Windows version of pyenv instead of a version installed within the WSL environment (Ubuntu). This misconfiguration can also interfere with poetry and other virtual environment tools, leading to a non-functional Python environment.

Solution Overview

The solution involves ensuring that each tool is installed within its respective environment and that their paths are configured correctly to prevent overlap. Here are the steps to set up pyenv correctly within WSL:

1. Install pyenv in WSL

First, install pyenv directly in your WSL environment, not Windows. This ensures that the Linux version of the tool is used. You can install pyenv using the following command:

curl https://pyenv.run | bash

2. Configure Environment Variables

After installation, add the following lines to your ~/.bashrc file, replacing USER with your actual username:

export PATH="/home/USER/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Reload your bash configuration:

source ~/.bashrc

3. Disable Windows Path Appending in WSL

To prevent WSL from using Windows paths (which can cause conflicts), modify the WSL configuration:

sudo vi /etc/wsl.conf

Add the following configuration to disable Windows path appending:

[interop]
appendWindowsPath = false

After saving your changes and exiting the editor, you'll need to restart WSL to apply the new configurations. Now, while you could manually close each WSL session and patiently wait for about 8 seconds, let’s be real—ain’t nobody got time for that! Instead, do what the pros do. Once you’ve confirmed that everything is saved, simply run the following command to instantly shut down all WSL sessions:

wsl --shutdown

*This command ensures that all changes are applied promptly and you can start fresh with your newly configured settings. *

Then, reopen WSL and check that no Windows paths are included:

echo $PATH

Conclusion

By ensuring that your development tools are correctly installed and configured in their respective environments and by managing the path settings appropriately, you can avoid many common conflicts and issues when using tools like pyenv and poetry in WSL. This setup provides a robust environment for Python development across both Windows and Linux systems.

Learn More Here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment