0

Before asking this question, I have seen the following questions that are somehow similar to my question, but they didn't resolve the existing error:

I have the following structure:

backbone-project
├── backbone
│   ├── backbone.py
│   ├── cmd_parser.py
│   ├── command_executor.py
│   ├── config_files
│   ├── db_connections
│   ├── extras
│   ├── __init__.py
│   ├── __pycache__
│   ├── query_files
│   ├── templates
│   ├── tests
│   └── utils
├── LICENSE
├── README.md
├── requirements.txt
├── setup.py

and this is my setup.py file:

import setuptools
from backbone.utils.enums import Files, Letters


with open(Files.README.value, Letters.R.value, encoding=Letters.UTF_8.value) as readme_file:
    long_description = readme_file.read()

with open(Files.REQUIREMENTS.value, Letters.R.value, encoding=Letters.UTF_8.value) as requirements_file:
    requirements = requirements_file.read().splitlines()

setuptools.setup(
    name="backbone",
    version="1",
    entry_points={
      "console_scripts": [
          'backbone=backbone.backbone:main'
      ]
    },
    author="Mostafa Ghadimi",
    author_email="my email",
    long_description=long_description,
    install_requirements=requirements,
    # install_requires=requirements,
    long_description_content_type="text/markdown",
    url="url",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    package_dir={"": "backbone"},
    packages=setuptools.find_packages("backbone"),
    python_requires=">=3.6",
    license='MIT',
)

backbone/backbone.py

#!/usr/bin/env python
from cmd_parser import parse_args
from utils.logger import Logger
from utils.enums import Messages


def main():
    logger = Logger(__name__)
    logger.log(Messages.BACKBONE_WELCOME_MSG.value)
    parse_args()


if __name__ == '__main__':
    main()

The problem is that whenever I want to install the package, it is installed successfully, but as the command is being executed, the error is raised:

requirements.txt

git+https://<username>:<password>@<git_repository_url>.git

and install it using the following command:

pip install -r requirements

(It has been installed without any error.)

As I want to use the package (in bash), I face to following error:

Traceback (most recent call last):
  File "/home/user/.local/bin/backbone", line 5, in <module>
    from backbone import main
ImportError: cannot import name 'main' from 'backbone' (unknown location)

How can I resolve this error?

P.S. 1 On local after exporting PYTHONPATH the problem is resolved, but the problem still exists when I want to install the package from git repository!

P.S. 2 I am somehow sure that the problem is with entry_points. I have also tried backbone:main but it doesn't work!

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
  • 1
    I spot lots of strange things... But first of all, I think you don't need the `package_dir` line in your `setup.py`. Then it should be `install_requires=...` (and not `install_requirements`). – sinoroc Aug 16 '21 at 20:41
  • @sinoroc would you mind helping me resolving the existing problem? – Mostafa Ghadimi Aug 16 '21 at 20:49
  • I don't know. Too many things seems wrong. Maybe try this: In your `requirements.txt` you should write something like `backbone @ git+https://...`. – sinoroc Aug 16 '21 at 22:02
  • @sinoroc I have tried this one and it doesn't work. I am somehow sure that the problem is with `entry_points`, but I don't know how to resolve it. I will add my backbone.py file in a minute. – Mostafa Ghadimi Aug 16 '21 at 22:11
  • In you entry points you mention `backbone.main:main`, but you do not seem to have any `backbone.main` module (or package), in other words there is no `backbone/main.py` (or `backbone/main/__init__.py`). – sinoroc Aug 17 '21 at 17:24
  • @sinoroc I have also tried backbone.backbone.main:main but it doesn’t work. Is it possible for you to give me upvote? – Mostafa Ghadimi Aug 17 '21 at 17:27
  • But what do you want your entry point function to actually be? What is the name of the function? In which module/package is it? In which file? You don't seem to have any `main` file or directory, so it has to be something else. – sinoroc Aug 17 '21 at 17:47
  • @sinoroc I will try backbone.backbone:main is that ok? – Mostafa Ghadimi Aug 17 '21 at 17:58
  • I don't know. Where is your function? You did not answer. What is the name of the function? In which file is the function? If you have a function `main` in the file `backbone/backone.py` then `backbone.backbone:main` should work. – sinoroc Aug 17 '21 at 18:24
  • @sinoroc I haved applied the change (`backbone.backbone:main`), now still have another error (I have updated the post). Would you please take a look at it? – Mostafa Ghadimi Aug 29 '21 at 13:52
  • The error message does not seem to match what your project structure is claimed to be. If the entry point is `backbone.backbone:main`, then I would expect the error message to be `cannot import name 'main' from 'backbone.backbone'`. -- It is hard to follow what the real project structure is, I am confused. -- Maybe you should make 100% sure to uninstall this `backbone` application properly, then reinstall. -- If you are familiar with Python virtual environments, you could experiment in one. – sinoroc Aug 29 '21 at 14:09
  • @sinoroc yes I'm not beginner with Python and its applications. I've done the thing you've said in the main function of backbone/backbone.py I call another function called parse_args and I have applied the changes you've said. It doesn't work. Would you please give me more details? – Mostafa Ghadimi Aug 29 '21 at 22:01

1 Answers1

2

The reason is that there already exist package named 'backbone'. So it installs it from pip. But what you need is local module, which is also called 'backbone'. To fix this, simple type '.backbone' (add dot before the module name) instead of 'backbone'.

ShlomiRex
  • 321
  • 1
  • 2
  • 10
  • Thanks for your answer, would you please mention were should I apply the changes? Would you mind editing the answer? – Mostafa Ghadimi Aug 16 '21 at 20:38
  • @MostafaGhadimi The changes should be in the top of setup.py file, where you import 'main' from 'backbone'. Moreover, I suggest removing the module that you installed so it doesn't conflict. – ShlomiRex Aug 16 '21 at 20:44
  • It's not fixed using `.backbone` for local command. is there any way to name it as backbone except alias? – Mostafa Ghadimi Aug 31 '21 at 20:33