Unable to import functions from modules

70 views Asked by At

I tried to create a module using setup.py to practise my understanding of packages and __init__.py files. But I get the following error -

Traceback (most recent call last):
  File "/home/thoma/PycharmProjects/test_gymnasium/test.py", line 1, in <module>
    from test_final_1 import my_add
ImportError: cannot import name 'my_add' from 'test_final_1' (/home/thoma/anaconda3/envs/test_gymnasium/lib/python3.8/site-packages/test_final_1/__init__.py)

Here's the structure of my Python folder test-final-1 -

├── build
│   ├── bdist.linux-x86_64
│   └── lib
│       └── test_final_1
│           ├── calc.py
│           └── __init__.py
├── setup.py
├── test_final_1
│   ├── calc.py
│   └── __init__.py
└── test_final_1.egg-info
    ├── dependency_links.txt
    ├── PKG-INFO
    ├── requires.txt
    ├── SOURCES.txt
    └── top_level.txt
    

The __init__ file contains the following -

from test_final_1.calc import *

The calc file contains the following -

def my_add(a,b):
    return a+b

def my_sub(a,b):
    return a-b

Here is my test file which throws an error -

from test_final_1 import my_add
a = 2
b = 4
my_add(2,4)

Here is the setup file -

from setuptools import setup

setup(
    name="test_final_1",
    version="0.0.1",
    install_requires=[],
)

This is how I run the setup file from the parent directory - pip install e .

Edit 1

Based on of the comments I received, I went to site-packages. Here I observed that, the __init__ file is the same. But the calc file is strangely empty. Why would that have happened?

0

There are 0 answers