Skip to main content

Set Up Your Python Environment for LangChain

Every tutorial in this track runs in the same kind of project: a Python 3.14 project made with uv, with LangChain and its Claude package installed, and your Claude API key in a .env file that Git never commits. This page builds that project one step at a time, explains what each piece is for, and ends with a first call to Claude in eight lines of Python.

What You Need, and Why​

A LangChain project is a Python project with a few packages and one secret. Each piece has a job:

PieceWhat it isIts job here
uvA Python package and project manager, from AstralCreates the project, gets Python 3.14, installs the packages and runs your scripts with them
Python 3.14The version of Python the project usesThe newest version that LangChain's packages list as supported
langchainLangChain itselfGives you init_chat_model, create_agent and the rest of LangChain
langchain-anthropicLangChain's Claude packageLets LangChain talk to Claude
python-dotenvA small package that reads .env filesCopies your key from .env into the program's environment variables
A Claude API keyA secret from the Claude ConsoleProves to Claude's API that the call is yours, and bills it to your account
.env and .gitignoreA file that holds the key, and Git's list of files to leave outKeep the key out of your code and out of Git

The key never appears in your code. It travels from the .env file to Claude like this:

load_dotenv() copies each line of .env into the program's environment variables, the settings every running program can read. LangChain's Claude model reads ANTHROPIC_API_KEY from there and sends it with every call. .gitignore tells Git never to pick up .env, so the key stays on your computer.

What It Costs​

uv, Python, LangChain, langchain-anthropic and python-dotenv are all free. Calls to Claude are not. Anthropic's pricing page says: "The Claude API has no free tier or self-serve trial, and new accounts don’t include free credits." You buy credits in the Claude Console first; the minimum purchase is $5.

This track uses Claude Opus 5.5, which costs $4 per million input tokens and $20 per million output tokens. A token is a piece of a word, and output tokens include the thinking Claude sometimes does before it answers. The first call on this page used 25 input tokens and between 26 and 95 output tokens across my runs, so each run of it costs between about $0.0006 and $0.002, well under a cent.

Prerequisites​

  • uv. Installing uv has the installer for every system. Step 1 checks it.
  • Git, which Step 5 uses to check that the key stays out of commits. Git's downloads page has it for every system.

Step 1: Check uv​

Open a terminal and ask uv for its version:

Terminal
uv --version
Output
uv 0.13.10 (e3e7c1eac 2026-11-24 aarch64-apple-darwin)

The terminal after uv --version: it prints uv 0.13.10 (e3e7c1eac 2026-11-24 aarch64-apple-darwin).

A version number means uv is installed and the terminal can find it. This track uses uv 0.13.10. How to Check Which Version of uv You Have explains the rest of the line, and why uv version, without the dashes, prints something else.

Step 2: Create a Python 3.14 Project​

In the folder where you keep your projects, create one called hello-langchain, then move into it:

Terminal
uv init --python 3.14 --no-package hello-langchain
cd hello-langchain
Output
Initialized project `hello-langchain` at `/Users/you/source/hello-langchain`

The terminal after uv init --python 3.14 --no-package hello-langchain: Initialized project hello-langchain at /Users/you/source/hello-langchain.

  • uv init hello-langchain creates the project in a new folder: pyproject.toml for its settings and packages, a sample main.py, a README.md, a .python-version file and a .gitignore. It also starts a Git repository there; uv help init says "By default, uv will initialize a Git repository".
  • --python 3.14 pins the project to Python 3.14. uv writes 3.14 into .python-version, and requires-python = ">=3.14" into pyproject.toml.
  • --no-package leaves out the build system, so the project is a plain folder with its code in main.py. That's all a project you run, rather than publish, needs.
  • cd hello-langchain moves the terminal into the new folder. Every command from here on runs there.

Run the sample main.py:

Terminal
uv run main.py
Output
Using CPython 3.14.10
Creating virtual environment at: .venv
Hello from hello-langchain!

The terminal after the first uv run main.py: Using CPython 3.14.10, Creating virtual environment at: .venv, and Hello from hello-langchain!

uv run runs a script with the project's own Python and packages. The first time, it found Python 3.14.10 and created the project's virtual environment, the .venv folder: the project's own copy of Python and its packages, kept apart from every other project. If Python 3.14 isn't on your computer, uv gets it for you: its documentation says it "will automatically download Python versions if they cannot be found on the system". How to Create a Python Project with uv explains every file uv init makes.

Step 3: Install LangChain and Its Claude Package​

Terminal
uv add langchain langchain-anthropic python-dotenv
Output
Resolved 47 packages in 522ms
Prepared 1 package in 669ms
Installed 45 packages in 88ms
+ annotated-types==0.8.0
+ anthropic==2.17.0
+ anyio==4.16.0
+ certifi==2026.11.20
+ charset-normalizer==3.5.3
+ distro==1.9.0
+ docstring-parser==0.18.0
+ h11==0.16.0
+ httpcore==1.0.9
+ httpcore2==2.18.0
+ httpx==0.29.0
+ httpx2==2.18.0
+ idna==3.21
+ jiter==0.19.0
+ jsonpatch==1.48
+ jsonpointer==3.12
+ langchain==1.5.2
+ langchain-anthropic==1.9.0
+ langchain-core==1.9.1
+ langchain-protocol==0.0.22
+ langgraph==1.2.25
+ langgraph-checkpoint==4.3.2
+ langgraph-prebuilt==1.1.3
+ langgraph-sdk==0.4.14
+ langsmith==0.16.8
+ opentelemetry-api==1.46.1
+ orjson==3.13.0
+ ormsgpack==1.13.1
+ packaging==26.4
+ pydantic==2.14.4
+ pydantic-core==2.50.5
+ python-dotenv==1.2.4
+ pyyaml==6.0.4
+ requests==2.34.2
+ requests-toolbelt==1.0.0
+ sniffio==1.3.1
+ tenacity==9.2.1
+ truststore==0.10.5
+ typing-extensions==4.16.0
+ typing-inspection==0.4.5
+ urllib3==2.8.0
+ uuid-utils==0.20.0
+ websockets==17.4
+ xxhash==4.1.0
+ zstandard==0.26.0

The terminal after uv add langchain langchain-anthropic python-dotenv: Resolved 47 packages, Installed 45 packages, and the list of installed packages, including langchain 1.5.2, langchain-anthropic 1.9.0, langchain-core 1.9.1, langgraph 1.2.25 and python-dotenv 1.2.4.

uv add adds packages to the project and installs them in .venv. You asked for three, and uv also installed everything they need: langchain-core, the base the other LangChain packages build on; langgraph, the runtime under every LangChain agent; anthropic, Claude's own Python SDK, which langchain-anthropic uses to make the calls; and langsmith, LangSmith's client, which stays switched off unless you turn tracing on.

uv recorded the three packages in pyproject.toml, each with the installed version as the lowest it accepts:

pyproject.toml
[project]
name = "hello-langchain"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
"langchain>=1.5.2",
"langchain-anthropic>=1.9.0",
"python-dotenv>=1.2.4",
]

It also wrote uv.lock, which the uv documentation describes as a lockfile that "contains the exact resolved versions that are installed in the project environment". Keep both files: they let the same versions be installed on another computer. How to Install LangChain in Python with uv shows another way to install the Claude package, langchain[anthropic], and what goes wrong in a project that still allows Python 3.10.

Step 4: Get a Claude API Key​

Calls to Claude need an API key from the Claude Console, and credits to pay for them. How to Get a Claude API Key walks through creating one and adding credits, with a screenshot of every screen.

Keep the key at hand for the next step, and treat it like a password: calls made with it are billed to your account, so never paste it into your code or into a chat.

Step 5: Keep the Key in .env, Out of Git​

Create a file called .env in the hello-langchain folder, in any text editor, with your key after the =:

.env
ANTHROPIC_API_KEY=sk-ant-your-key-here

ANTHROPIC_API_KEY is the name LangChain's Claude model looks for. Now look at the project the way Git sees it:

Terminal
git status --short
Output
?? .env
?? .gitignore
?? .python-version
?? README.md
?? main.py
?? pyproject.toml
?? uv.lock

The terminal after git status --short in hello-langchain: seven untracked files marked with two question marks, the first of them .env.

?? marks a file that Git doesn't track yet, and .env is one of them. A git add . would put it, and your key, into the next commit, and anyone who can see the repository could read the key. Add .env to the project's .gitignore, the list of files Git leaves out:

Terminal
echo ".env" >> .gitignore

Both commands add a line, .env, at the end of the file. Check that Git now ignores the file, and look at the project again:

Terminal
git check-ignore -v .env
git status --short
Output
.gitignore:11:.env .env
?? .gitignore
?? .python-version
?? README.md
?? main.py
?? pyproject.toml
?? uv.lock

The terminal after echo ".env" >> .gitignore: git check-ignore -v .env prints .gitignore:11:.env and .env, and git status --short now lists six files, without .env.

git check-ignore -v names the rule that ignores a file: line 11 of .gitignore, the .env line you just added below the ten lines uv init wrote. git status no longer lists .env. How to Keep a .env File Out of Git with .gitignore explains the rule, and what to do if a .env file is already in a commit. How to Set the Anthropic API Key for LangChain in Python shows the two other ways to give LangChain the key, and which one wins when they disagree.

Step 6: Make Your First Call to Claude​

Open main.py and replace the sample code. It starts by loading the .env file:

main.py (the first lines)
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv() # Reads ANTHROPIC_API_KEY from the .env file.

The two import lines bring in load_dotenv from python-dotenv and init_chat_model from LangChain. load_dotenv() looks for .env in the script's folder and copies its lines into the environment variables, so the key is there before the model asks for it.

Then it creates the model:

main.py (the model)
model = init_chat_model("anthropic:claude-opus-5-5")

init_chat_model creates a chat model from one string: the provider, anthropic, before the colon, and the model, Claude Opus 5.5, after it. How to Create a Chat Model with init_chat_model in LangChain covers its other settings.

Last, it sends one message and prints the answer:

main.py (the call)
response = model.invoke("Welcome me to LangChain in one short sentence.")
print(response.text)

invoke sends the text to Claude and waits for the reply, which comes back as a message object, and text holds the words of that reply. The complete file:

main.py
from dotenv import load_dotenv
from langchain.chat_models import init_chat_model

load_dotenv() # Reads ANTHROPIC_API_KEY from the .env file.

model = init_chat_model("anthropic:claude-opus-5-5")
response = model.invoke("Welcome me to LangChain in one short sentence.")
print(response.text)

The main.py file in a macOS-style code window: it loads the .env file with load_dotenv(), creates a Claude Opus 5.5 model with init_chat_model("anthropic"), asks it to welcome me to LangChain in one short sentence, and prints the reply's text.

Run it:

Terminal
uv run main.py
Output
Welcome to LangChain, where you can start building powerful applications with language models!

The terminal after uv run main.py: Claude answers Welcome to LangChain, where you can start building powerful applications with language models!

That sentence came from Claude. Your sentence will be different: Claude words its answer afresh every time. If the run stops with TypeError: Anthropic authentication failed: no API key or authorization credentials were provided, no key reached the model; Fixed: TypeError: Anthropic authentication failed shows how I found the cause and fixed it.

What You Have Now​

hello-langchain
hello-langchain/
├── .env
├── .git/
├── .gitignore
├── .python-version
├── .venv/
├── README.md
├── main.py
├── pyproject.toml
└── uv.lock
  • A Python 3.14 project, with its own virtual environment in .venv.
  • LangChain 1.5.2 and langchain-anthropic 1.9.0, recorded in pyproject.toml and pinned in uv.lock.
  • Your Claude API key in .env, which Git ignores.
  • A script that calls Claude through LangChain.

Clean Up​

Nothing in the project runs, or costs anything, while you're not using it: you pay only for the calls you make. Each tutorial in this track creates its own small project in the same way, so you can delete the hello-langchain folder when you're done with this page. Keep your key for the next tutorials. If you stop using the Claude API, disable or delete the key in the Claude Console, as How to Get a Claude API Key shows.

Common Mistakes​

MistakeWhat happensDo this instead
Running python3 main.pyModuleNotFoundError: No module named 'dotenv': that Python doesn't have the project's packagesRun uv run main.py, which uses the project's .venv
Running a script kept outside the project folderTypeError: Anthropic authentication failed, although .env holds the keyKeep the script next to .env
Leaving an old key set in the terminal401 invalid x-api-key: a key set in the terminal wins over .envunset ANTHROPIC_API_KEY, or Remove-Item -Path Env:\ANTHROPIC_API_KEY in PowerShell
Committing before .env is in .gitignoreThe key is in Git's history, and .gitignore doesn't take it outAdd .env to .gitignore before the first commit

Troubleshooting​

SymptomLikely causeFix
TypeError: Anthropic authentication failed: no API key or authorization credentials were provided.No key reached the modelCheck that .env is next to the script and names ANTHROPIC_API_KEY; Fixed: TypeError: Anthropic authentication failed
AnthropicAuthenticationError: 401 invalid x-api-keyA key arrived, but Claude's API rejected itCheck the key, and that no old key is set in the terminal
ImportError: Initializing ChatAnthropic requires the langchain-anthropic package. Please install it with `pip install langchain-anthropic`langchain-anthropic isn't installeduv add langchain-anthropic
ModuleNotFoundError: No module named 'dotenv'The script ran outside the project's environmentuv run main.py

Frequently Asked Questions​

Why Python 3.14 and not 3.15?​

LangChain's packages list Python 3.11 to 3.14 as the versions they support, so this track uses the newest of those. What Is LangChain? explains the reason in full.

Can I use pip instead of uv?​

Yes. LangChain's installation page gives pip install -U langchain next to uv add langchain. This track uses uv everywhere, because uv also creates the project, gets the right Python and runs each script with the project's packages.

Can I set the key in the terminal instead of a .env file?​

Yes. LangChain's Claude model reads ANTHROPIC_API_KEY from the environment, wherever it came from. How to Set the Anthropic API Key for LangChain in Python shows how in bash and PowerShell. A key set in the terminal lasts only until you close that window, which is why this track keeps it in .env.

Can I use another Claude model?​

Yes. Change the name after anthropic: in init_chat_model, for example to another model from Anthropic's models overview. Each model has its own price per token.

Conclusion​

You checked uv, created a Python 3.14 project with uv init, installed LangChain, its Claude package and python-dotenv with uv add, kept your Claude API key in a .env file that Git ignores, and called Claude through LangChain for a fraction of a cent. Every tutorial in this track starts from a project like this one.

Next: Your First Agent (coming soon) gives Claude a tool, runs an agent with it, reads every message of the run and shows why a second question doesn't remember the first.

Additional Resources​