langchain
langchain copied to clipboard
import error when importing `from langchain import OpenAI` on 0.0.151
got the following error when running today:
from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
File "venv/lib/python3.11/site-packages/langchain/agents/__init__.py", line 2, in <module>
from langchain.agents.agent import (
File "venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 17, in <module>
from langchain.chains.base import Chain
File "venv/lib/python3.11/site-packages/langchain/chains/__init__.py", line 2, in <module>
from langchain.chains.api.base import APIChain
File "venv/lib/python3.11/site-packages/langchain/chains/api/base.py", line 8, in <module>
from langchain.chains.api.prompt import API_RESPONSE_PROMPT, API_URL_PROMPT
File "venv/lib/python3.11/site-packages/langchain/chains/api/prompt.py", line 2, in <module>
from langchain.prompts.prompt import PromptTemplate
File "venv/lib/python3.11/site-packages/langchain/prompts/__init__.py", line 14, in <module>
from langchain.prompts.loading import load_prompt
File "venv/lib/python3.11/site-packages/langchain/prompts/loading.py", line 14, in <module>
from langchain.utilities.loading import try_load_from_hub
File "venv/lib/python3.11/site-packages/langchain/utilities/__init__.py", line 5, in <module>
from langchain.utilities.bash import BashProcess
File "venv/lib/python3.11/site-packages/langchain/utilities/bash.py", line 7, in <module>
import pexpect
ModuleNotFoundError: No module named 'pexpect'
does this need to be added to project dependencies?
I am also dealing with the same issue
Seems like pexpect
was recently added in https://github.com/hwchase17/langchain/pull/3580/files#diff-5f99ae6d8500359517f487845b0a2b2f6cad1e3d7751c501bdc9b99bd18aadc3R7 but I don't seem to see the new package added anywhere, unless I'm missing something
I am also facing issues with pexpect
ERROR: AttributeError: module 'pexpect' has no attribute 'spawn'
this spawn won't work in WINDOWS OS. Ref: https://github.com/pexpect/pexpect/issues/321
I'm facing the same issue as well
Same here
@vowelparrot for vis
Will fix. Thank you. We will improve our CI to prevent this type of error from occurring moving forward
The reason this slipped through is that it is a dependency of poetry
itself. we will address this. Thanks for everyone's patience
Was there any solution to this or will I have to downgrade langchain version?
Downgrading is probably the fastest way currently, while this issue is getting fixed
As a WA you can simply install
pipenv update langchain
pipenv install pyexpect
this will patch the issue until it is fixed
As a WA you can simply install
pipenv isntall pyexpect
this will patch the issue until it is fixed
This does not help,. it will fail with:
def _initialize_persistent_process(prompt: str) -> pexpect.spawn:
AttributeError: module 'pexpect' has no attribute 'spawn'
Ran into the same issue this morning. Manually installing pyexpect
did not resolve it. Still getting "ModuleNotFoundError: No module named 'pexpect'". I had to downgrade langchain to version 0.0.150 by adding langchain = "==0.0.150"
to the pipfile.
Ran into the same issue this morning. Manually installing
pyexpect
did not resolve it. Still getting "ModuleNotFoundError: No module named 'pexpect'". I had to downgrade langchain to version 0.0.150 by addinglangchain = "==0.0.150"
to the pipfile.
Yea same, jumped a version back and that one works :)
what file do you open to change langchain = "==0.0.150"? sorry im a little new on this ^^
what file do you open to change langchain = "==0.0.150"? sorry im a little new on this ^^
Ah, not a file, when you installed langchain where was command pip install langchain, you can specify which version to install with pip install -U langchain==0.0.150
Thanks! Doing progress now!
@Nacho48 If you are using pipenv
, the line should be in your Pipfile. Otherwise, install directly like Vlado suggested
For windows you could just add your own class for PowerShell like this (until windows is supported):
from typing import List, Union
from uuid import uuid4
import re
import pexpect
from pexpect.popen_spawn import PopenSpawn
class PowerShellProcess:
"""Executes PowerShell commands and returns the output."""
def __init__(
self,
strip_newlines: bool = False,
return_err_output: bool = False,
persistent: bool = False,
):
self.strip_newlines = strip_newlines
self.return_err_output = return_err_output
self.prompt = ""
self.process = None
if persistent:
self.prompt = str(uuid4())
self.process = self._initialize_persistent_process(self.prompt)
@staticmethod
def _initialize_persistent_process(prompt: str) -> PopenSpawn:
process = PopenSpawn(
"pwsh -NoProfile", encoding="utf-8"
)
# Set the custom prompt
process.sendline(f"function prompt {{'{prompt}'}}")
process.expect_exact(prompt, timeout=10)
return process
def run(self, commands: Union[str, List[str]]) -> str:
"""Run commands and return final output."""
if isinstance(commands, str):
commands = [commands]
commands = ";".join(commands)
if self.process is not None:
return self._run_persistent(
commands,
)
else:
return self._run(commands)
def _run(self, command: str) -> str:
"""Run commands and return final output."""
try:
output = subprocess.run(
f"pwsh -Command {command}",
shell=True,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
).stdout.decode()
except subprocess.CalledProcessError as error:
if self.return_err_output:
return error.stdout.decode()
return str(error)
if self.strip_newlines:
output = output.strip()
return output
def process_output(self, output: str, command: str) -> str:
# Remove the command from the output using a regular expression
pattern = re.escape(command) + r"\s*\n"
output = re.sub(pattern, "", output, count=1)
return output.strip()
def _run_persistent(self, command: str) -> str:
"""Run commands and return final output."""
if self.process is None:
raise ValueError("Process not initialized")
self.process.sendline(command)
# Clear the output with an empty string
self.process.expect(self.prompt, timeout=10)
self.process.sendline("")
try:
self.process.expect([self.prompt, pexpect.EOF], timeout=10)
except pexpect.TIMEOUT:
return f"Timeout error while executing command {command}"
if self.process.after == pexpect.EOF:
return f"Exited with error status: {self.process.exitstatus}"
output = self.process.before
output = self.process_output(output, command)
if self.strip_newlines:
return output.strip()
return output
Should work for most use cases
Fixed in 00.152 - The persistent Bash shell still doesn't support windows, but it shouldn't be a blocker for other usage
"persistence"-feature should work with that custom pwsh class in windows in the meantime