Skip to content

Commit 59e42d8

Browse files
committed
Load .env vars on project run instead of in project's main.py. Add python-dotenv to dependencies. Verify with tests.
1 parent 818daa4 commit 59e42d8

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

agentstack/cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
import importlib.resources
1515
from cookiecutter.main import cookiecutter
16+
from dotenv import load_dotenv
1617

1718
from .agentstack_data import (
1819
FrameworkData,
@@ -184,6 +185,7 @@ def run_project(framework: str, path: str = ''):
184185
print(e)
185186
sys.exit(1)
186187

188+
load_dotenv(_path / '.env') # explicitly load the project's .env file
187189
entrypoint = _path / frameworks.get_entrypoint_path(framework)
188190
os.system(f'python {entrypoint}')
189191

agentstack/templates/crewai/{{cookiecutter.project_metadata.project_slug}}/src/main.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import sys
33
from crew import {{cookiecutter.project_metadata.project_name|replace('-', '')|replace('_', '')|capitalize}}Crew
44
import agentops
5-
from dotenv import load_dotenv
6-
load_dotenv()
75

86
agentops.init(default_tags=['crewai', 'agentstack'])
97

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies = [
2828
"packaging==23.2",
2929
"requests>=2.32",
3030
"appdirs>=1.4.4",
31+
"python-dotenv>=1.0.1",
3132
]
3233

3334
[project.optional-dependencies]

tests/test_project_run.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
from pathlib import Path
3+
import shutil
4+
import unittest
5+
from parameterized import parameterized_class
6+
7+
from agentstack import frameworks
8+
from agentstack.cli import run_project
9+
from agentstack.generation.files import ConfigFile
10+
11+
BASE_PATH = Path(__file__).parent
12+
13+
14+
@parameterized_class([{"framework": framework} for framework in frameworks.SUPPORTED_FRAMEWORKS])
15+
class ProjectRunTest(unittest.TestCase):
16+
def setUp(self):
17+
self.project_dir = BASE_PATH / 'tmp' / self.framework
18+
19+
os.makedirs(self.project_dir)
20+
os.makedirs(self.project_dir / 'src')
21+
(self.project_dir / 'src' / '__init__.py').touch()
22+
23+
# set the framework in agentstack.json
24+
shutil.copy(BASE_PATH / 'fixtures' / 'agentstack.json', self.project_dir / 'agentstack.json')
25+
with ConfigFile(self.project_dir) as config:
26+
config.framework = self.framework
27+
28+
# populate the entrypoint
29+
entrypoint_path = frameworks.get_entrypoint_path(self.framework, self.project_dir)
30+
shutil.copy(BASE_PATH / f"fixtures/frameworks/{self.framework}/entrypoint_max.py", entrypoint_path)
31+
32+
# write a basic .env file
33+
shutil.copy(BASE_PATH / 'fixtures' / '.env', self.project_dir / '.env')
34+
35+
def tearDown(self):
36+
shutil.rmtree(self.project_dir)
37+
38+
def test_run_project(self):
39+
run_project(self.framework, self.project_dir)
40+
41+
def test_env_is_set(self):
42+
"""
43+
After running a project, the environment variables should be set from project_dir/.env.
44+
"""
45+
run_project(self.framework, self.project_dir)
46+
assert os.getenv('ENV_VAR1') == 'value1'

0 commit comments

Comments
 (0)