Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<directory> is not a valid Python package name #8400

Closed
jcrben opened this issue Feb 13, 2020 · 13 comments
Closed

<directory> is not a valid Python package name #8400

jcrben opened this issue Feb 13, 2020 · 13 comments

Comments

@jcrben
Copy link

jcrben commented Feb 13, 2020

Note that this message appears for a couple other reported issues, but it's not clear that they are the same: #8229 and #6003

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request?

Bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

Code is this in test1.py:

def greeting(name: str) -> str:
    return "Hello " + name

Directory layout is more critical:
test -> nested -> nested-2 -> test1.py

Next to test1.py is __init__.py - other directories are empty.

  • What is the actual behavior/output?

When I run ~/.local/pyenv/versions/3.7.2/bin/python -m mypy /Users/bencreasy/test/nested/nested-2/test1.py I get:
nested-2 is not a valid Python package name

If I remove __init__.py, the message goes away.

  • What is the behavior/output you expect?

Don't see why it can't run the file, but if it cannot, a better error message.

  • What are the versions of mypy and Python you are using?

Python: Python 3.7.2
mypy: mypy 0.761

  • Do you see the same issue after installing mypy from Git master?

Yes, after running pip install git+git://github.com/python/mypy.git@a07dbd00638c35729e30dfc4ae441a8985183563 - same message.

  • What are the mypy flags you are using? (For example --strict-optional)

None.

@ilevkivskyi
Copy link
Member

__init__.py files exist in Python for a reason: this means the directory represents a package (so that one can import it). Since import nested-2 is obviously a syntax error, package names must be valid Python identifiers.

TBH, I don't see what is not clear in this error message, but if you will make a PR with alternative wording we might consider it.

@abcdenis
Copy link

abcdenis commented Nov 3, 2020

Hi,
what I did:

  • checkout some-project-with-dashes-in-name
  • cd to it
  • run [mypy .]
  • got error ". is not a valid Python package name"

Now I have to investigate:

  • check if there is _ _ init _ _.py file inside
  • wonder that mypy stops on such condition
  • remove _ _ init _ _.py and rerun

It will be nice to print invalid package full name/directory.
Or print option name to skip such checking.

Thank you.

@markfickett
Copy link

I'm getting a similar error where my directory structure is /path/with/some-directory-in-it/pkgdir/file.py . When I cd to pkgdir and run mypy file.py I get an error saying some-directory-in-it is not a valid Python package. There is a some-directory-in-it/__init__.py, but I can use other Python tools, like pip install -e . (there's a setup.py that specifies a valid Python package name) and python file.py just fine, so it's confusing that mypy does not work in this context.

@wangkuiyi
Copy link

wangkuiyi commented Feb 15, 2023

@markfickett I got the same problem. After renaming some-directory-in-it into some_directory_in_it, mypy worked.

@BTripp1986
Copy link

I am getting this error for a parent directory which is not coupled with the actual project.

/home/me/my-code/python/my_project_with_mypy/main.py

The my_project_with_mypy directory is a git repository, and therefore independent of the parent directory.

I could move the project into another directory hierarchy, but then what if someone clones my repo into a directory with dashes?

This definitely seems like an error, but I am new to MyPy so I could just be missing some kind of configuration which will work around this.

@stevleibelt
Copy link

@BTripp1986 do you get any response if this is indeed an issue or not?

I ran into the same problem. Checking out a git repository with the name foo-bar.
It feels so wrong that this is an issue since I execute mypy inside the repository.

@elmago
Copy link

elmago commented May 31, 2024

I had the same problem, but I managed to make progress by directly pointing to the Python package I needed using the --package option like: mypy -p src

@caldempsey
Copy link

Running into this. I think this is a silly restriction. I'd like to use mypy in heterogenous codebases. Not everything has to adhere perfectly to Python standards. Python can also be a scripting language, and so PEP8 does not have to be a first class citizen on every occasion.

@AntiSol
Copy link

AntiSol commented Jul 25, 2024

I just ran into this in a situation where I am using a submodule

core-project:
  |- .git
  |- pyproject.toml
  |- __init__.py
  |- module.py
  
  
client-project:
  |- .git
  |- README
  |- pyproject.toml
  |- src
  |  |- core_project -> git@host:core-project.git
  |  |- main.py
  

main.py:


from core_project import module

module.do_stuff()

  • when running mypy over the core-project repo (using mypy . in core-project), I get this error
  • core-project needs its own linting / validation, independant of the client project, because core-project has no knowledge of the client project
  • core-project's root directory is a package. This is still not ideal because now there's e.g a pyproject.toml in client-project/src/core_project/, but git doesn't really offer an option to do a "partial submodule" that only includes a single directory from another repo. core-project's root does not have a subdirectory like src, because then you'd have to import core_project.src.module all over the place, which is ugly and unnecessary. This could be alleviated somewhat by manually importing src.module in an init.py at the root level, but that would still cause the same issue with mypy, and also means adding every module that you want to import there.
  • our company has a policy of naming repos with hyphens, naming the repo core_project would violate policy. Note that it is named core_project when added as a submodule, because as mypy points out core-project is not a valid package name.

@koljapluemer
Copy link

Kindly, why is this issue closed? This makes mypy effectively unusable within projects that have a naming standard that happens to not be PEP8, for very little reason. A development library forcing naming standards of wrapper directories is an absurd overreach for extremely little reason.

@sabpamdev1
Copy link

How to disable this rule, if anyone can help please?

@AntiSol
Copy link

AntiSol commented Oct 17, 2024

Frankly your best option is to just disable mypy completely, that's we ended up doing at my company. It's incredibly slow and it's output is wrong most of the time anyway.

Plus, you'll note the lack of response on tickets like this one.

My advice is to save yourself a heap of time and needless hassle and just chuck mypy in the bin :)

@klalumiere
Copy link

init.py files exist in Python for a reason: this means the directory represents a package (so that one can import it). Since import nested-2 is obviously a syntax error, package names must be valid Python identifiers.

TBH, I don't see what is not clear in this error message, but if you will make a PR with alternative wording we might consider it.

Well, your explanation is actually perfect: thanks for this! 🙂 So for the error help message, I suggest something based on your explanation:

<invalid-name> is not a valid Python package name. Hint: a parent or child directory might contain a ' __init__.py' file when it shouldn't.

Low cost to change, and it's going to help hundreds of people. 😎 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests