Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Why for scripts should I have a directory in the project that has the same name as the project directory? #7564

Answered by edgarrmondragon
LagrangeH asked this question in Q&A
Discussion options

I wanted to make a command to run the main script, but for a long time I couldn't figure out why I get No file/folder found for package when I try to run it.

I had a project structure like this:

.
├── poetry.lock
├── pyproject.toml
└── src
    ├── __init__.py
    ├── main.py
    ...

In the pyproject.toml file I added this:

...
[tool.poetry.scripts]
main = "src.main:main"

When I ran poetry run main I got No file/folder found for package my-package. I solved it by renaming src/ to my-project/, i.e. same as project name.

So, why is this and why is this not written in the documentation?

You must be logged in to vote

Poetry can use the src layout but you still need my_package inside the src directory. It's documented as part of creating a new project:

If you want to use a src folder, you can use the --src option:

poetry new --src my-package

That will create a folder structure as follows:

my-package
├── pyproject.toml
├── README.md
├── src
│   └── my_package
│       └── __init__.py
└── tests
    └── __init__.py

Replies: 1 comment · 4 replies

Comment options

Poetry can use the src layout but you still need my_package inside the src directory. It's documented as part of creating a new project:

If you want to use a src folder, you can use the --src option:

poetry new --src my-package

That will create a folder structure as follows:

my-package
├── pyproject.toml
├── README.md
├── src
│   └── my_package
│       └── __init__.py
└── tests
    └── __init__.py
You must be logged in to vote
4 replies
@LagrangeH
Comment options

Thanks! But why are there such restrictions? What's the point of this?

@edgarrmondragon
Comment options

It's not a restriction. When importing packages, Python follows the directory name. So, the src layout only really moves the package (e.g. my_package) to a subdirectory. So, the syntax src.main:main is actually just equivalent to:

from src import main

main()

But the src package does not exist, because Poetry will look for my_package/ inside src. A good read is https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/

@jefer94
Comment options

Should be required take a scripts folder as part of the source?, not necessarily, it's possible scripts be used to build, get resources or pack src content and scripts folder not be used at all in run time, even Node.js usually have a scripts folder and src folder separately thanks to don't restrict what can do the developer, even I rather to stop using src folder than break the sense of structure of my project

So if you want to fix this tragedy you can add this to your pyproject.toml, where you can replace the scripts name by the folder that poetry is restricts you to easily import

[tool.poetry]
name = "project"
version = "0.1.0"
description = ""
authors = ["I <I@I.I>"]
+packages = [{include = "scripts"}]

Other thing NPM is doing properly and Poetry don't is avoid to suppose each one of their users are library developers, I'm reading the resource was supplied and the first element I have been seeing they talk about install the project as python package to then test or using it, so you remember the last developer who did it in a backend project? no one, thus this restriction is not necessary in all the developments, why should others have the same restriction?

@Standard-IO
Comment options

I not sure sure if the syntax change recently. But following what @jefer94 suggest I used this configuration and all work perfectly, I'm not a library coder.

my-package
├── pyproject.toml
├── README.md
├── src
│   └── other_dir
│       └── __init__.py
└── scripts
    ├── __init__.py
    └── my_script
[tool.poetry]
name = "project"
version = "0.1.0"
description = ""
authors = ["I <I@I.I>"]
packages = [{include = "src"}, {include = "scripts"}]

But I'm not sure if is necessary to include src dir...

Answer selected by LagrangeH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.