Learning LangChain from a variety of sources
I have used uv to manage the Python environment for this project. Here are the steps you should follow to recreate the same Python environment at your end, after you pull this GitHub repo to your local folder.
Before re-creating the Python environment, ensure you have uv installed on your local machine. To install uv for your operating system, follow the instructions here
This step is applicable when you are creating a new Python environment that will be managed by uv. Skip this step if you have cloned this repo from GitHub. In that case, skip this step and jump to the next section. We'll assume that your project is located in a folder ~/code/my_project and its subfolders. Let's refer to this folder as the Project Folder.
- Change directory to the
Project Folder$> cd ~/code/my_project
- Run the following command inside the
Project Folder- this creates a local Python environment in theProject Folder/.venv, but does not install any Python packages!Here we have chosen to use Python 3.12 (with the$> uv venv --python 3.12--python 3.12option). You can choose any other version of Python that you want to use for your project (replace3.12with3.10to use Python 3.10, for example). - Initialize the new managed environment
This will create a
$> uv initpyproject.tomlfile in theProject Folder. This file will be used byuvto manage the Python environment and help your replicate the same environment across various machines. - Activate the local environment you just created
$> source .venv/Scripts/activate ## on a Mac/Linux $> .venv\Scripts\activate ## on Windows
- Add packages to the local environment
For example:
$> uv add <package_name>
You can add multiple packages at once as follows:$> uv add langchainAs a best practice, you should add the packages to a$> uv add langchain openai python-dotenv .... # each package should be separated by a space
requirements.txtfile and then add all packages from that file using the following command:$> uv add -r requirements.txtrequirements.txtis just a regular text file with one package listed per line (as was the case used withpip)
These are the steps to setup the Python environment (NOTE: in the code listings below $> refers to the command prompt and should not be entered by you!)
-
cdto folder where you pulled the Github repo - let's refer to this folder as$PROJECT_HOMEhenceforth$> cd folder/to/learning_langchain
-
Run the following command inside
$PROJECT_HOME- - this creates a local Python environment in the$PROJECT_HOME/.venv, but does not install any Python packages! It also creates apyproject.tomlfile, which uv will update.$> uv venv --python 3.12 .venv -
Activate the Python environment you just created
$> source .venv/bin/activate ## on a Mac/Linux # For Windows users (PowerShell): # .venv\Scripts\Activate.ps1 # For Windows users (Command Prompt): # .venv\Scripts\activate.bat
-
Recreate the environmant - will install the packages from
pyproject.tomlfile$> uv sync -
Install some global packages - these will be installed globally without polluting your local Python environment. The include packages such as
ipykernel(to run Notebook files) andblack(to format Python code)$> uv tool install black $> uv tool install ipykernel
The sample programs are a mix of Notebook files (*.ipynb) and Python modules (*.py) files.
To run the notebooks files (*.ipynb)
- Open the notebook in your code editor, such as IPython Notebook or VS Code or Cursor or PyCharm (professional only!)
- Select the correct Python kernel to run the cells in your notebook - it should be
.venv\Scripts\python.exe - Run the notebook as you normally do.
To run Python modules (*.py) files:
From within your IDE -
- Assign the correct Python interpreter to your project - it should be
.venv\Scripts\python.exe - Run the Python code as you'd normally do from your IDE.
From the command line -
cdto$PROJECT_HOME$> cd folder/to/learning_langchain
- Use
uvto run your Python code as follows:For example:$> uv run full/path/to/python/code/file$> uv run src/langchain_tutorial/01_chat_models_and_prompts.py