DavidVujic / python-polylith-example

Example Polylith setup for Python
MIT License
51 stars 8 forks source link

virtualenv per project #14

Closed yhkee0404 closed 6 months ago

yhkee0404 commented 6 months ago

According to your talk, it seems good to have a single virtualenv for the entire workspace, or monorepo. However, when I go inside the project directory to build an artifact it seems to create another virtualenv at the path of {cache-dir}/virtualenvs without reusing the .venv one at the project root directory. Is that OK?

Besides, I also wonder whether you would run the codes in the development directory inside it or at the project root directory. How have you set your IDE or IPython environment to organize path problems and achieve REPL-driven development? I hope there is another video about it like your current decent ones. Thanks!

Below is a zsh history that shows the former issue about Creating virtualenv.

➜  projects git:(main) ✗ poetry env list                                         <aws:developer>
.venv (Activated)
➜  projects git:(main) ✗ poetry config virtualenvs.path                          <aws:developer>
.venv
➜  projects git:(main) ✗ which python                                            <aws:developer>
/Users/yhkee0404/python-polylith-example/.venv/bin/python
➜  projects git:(main) ✗ cd my_aws_lambda_project                                <aws:developer>
➜  my_aws_lambda_project git:(main) ✗ poetry env list                            <aws:developer>
➜  my_aws_lambda_project git:(main) ✗ poetry config virtualenvs.path             <aws:developer>
/Users/yhkee0404/Library/Caches/pypoetry/virtualenvs
➜  my_aws_lambda_project git:(main) ✗ which python                               <aws:developer>
/Users/yhkee0404/python-polylith-example/.venv/bin/python
➜  my_aws_lambda_project git:(main) ✗ serverless deploy                          <aws:developer>
Running "serverless" from node_modules

Deploying aws-python-project to stage dev (eu-north-1)

✖ Stack aws-python-project-dev failed to deploy (0s)
Environment: darwin, node 18.14.0, framework 3.30.1 (local) 3.34.0v (global), plugin 6.2.3, SDK 4.5.1
Credentials: Local, "developer" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Cannot access package artifact at "dist/my_aws_lambda_project-1.1.1-py3-none-any.whl": ENOENT: no such file or directory, access '/Users/yhkee0404/python-polylith-example/projects/my_aws_lambda_project/dist/my_aws_lambda_project-1.1.1-py3-none-any.whl'
➜  my_aws_lambda_project git:(main) ✗ poetry build-project                       <aws:developer>
Creating virtualenv my-aws-lambda-project-roiV758L-py3.11 in /Users/yhkee0404/Library/Caches/pypoetry/virtualenvs
Using /Users/yhkee0404/python-polylith-example/projects/my_aws_lambda_project/pyproject.toml
Copied project & packages into temporary folder /Users/yhkee0404/python-polylith-example/projects/.prepare_my_aws_lambda_project
Generated /Users/yhkee0404/python-polylith-example/projects/.prepare_my_aws_lambda_project/pyproject.toml
Building my_aws_lambda_project (1.1.1)
  - Building sdist
  - Built my_aws_lambda_project-1.1.1.tar.gz
  - Building wheel
  - Built my_aws_lambda_project-1.1.1-py3-none-any.whl
Copied dist folder.
Removed temporary folder.
Done!
DavidVujic commented 6 months ago

Hi @yhkee0404!

I'm happy to hear that you liked my videos! 😄

I usually don't do any development work in the projects folder, and in general only build artifacts from it (i.e. wheels and/or sdists). The recommended way for Polylith is to create a virtual environment at the root (the development project) when developing and testing code and do all of the work there, having all bases and all components available.

The development folder is part of the development project, and I usually run code from the REPL with the development venv activated. You can do that from a separate shell, or in a more REPL Driven Development way from the code editor. I use Emacs, and have an "auto discover .venv" plugin/package. I have written about the setup here that covers Emacs, VS Code (and PyCharm, details in the comments section), I hope it will be useful for you!

https://davidvujic.blogspot.com/2022/08/joyful-python-with-repl.html

yhkee0404 commented 6 months ago

Hi Vujic!

I learnt a lot especially from your dad jokes. 😆

Then I think I may avoid creating another virtual environment per project and reuse the project root one by creating a poetry.toml per project and specifying virtualenvs.path.

For my question on development folder, I realized there was no problem at all because I can always import the top namespaced package in the same way like from example, no matter where the current directory is the project root or any of its descendants. Awesome!

The article was exactly what I was looking for. Thank you!

Today I introduced your great work to my colleagues. I wish they please break the ice soon and feel free to exploit the fun of building the company's artifacts with blue and green bricks at office!

Have a wonderful weekend! 💖

DavidVujic commented 6 months ago

Glad to hear that, and that you have introduced it to your colleagues! 👏 If you and/or your team have questions or feedback, don't hesitate to reach out. I will happily help out if you have anything Polylith related 😄