Locally installed package-ImportError: module '__main__' has no attribute 'main' - python-3.x

I need a little help with python packaging. I known similar questions has been already asked, but I could not find the solution for my problem.
Here is the output of tree:
.
├── env
├── prala
│   ├── __init__.py
│   └── __main__.py
└── setup.py
setup.py:
from setuptools import setup, find_packages
setup(
name='prala',
version='0.5',
description='Practice Language',
url='http://github.com/*/*',
author='*',
author_email='*#*.com',
license='MIT',
classifiers =[
"Programming Language :: Python",
"Programming Language :: Python :: 3",
],
packages=find_packages(),
entry_points = {
'console_scripts': ['ppp=__main__:main'],
},
zip_safe=False)
__main__.py:
def main():
print("hello world")
if __name__ == "__main__":
main()
I did the following:
I activated the virtualenv in the root: $ source env/bin/activate
I built the dist and installed it: (env) $ python setup.py install
I run the entry point: (env) $ ppp
Unfortunately I got error instead of the 'hello world' message:
Traceback (most recent call last):
File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2332, in resolve
return functools.reduce(getattr, self.attrs, module)
AttributeError: module '__main__' has no attribute 'main'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/akoel/Projects/python/delete/env/bin/ppp", line 11, in <module>
load_entry_point('prala==0.5', 'console_scripts', 'ppp')()
File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 480, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2693, in load_entry_point
return ep.load()
File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2324, in load
return self.resolve()
File "/home/akoel/Projects/python/delete/env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2334, in resolve
raise ImportError(str(exc))
ImportError: module '__main__' has no attribute 'main'
Can anyone help me showing what did I miss?

I found the problem:
In the entry_points in the setup.py I forgot to put the project name for the console_settings:
entry_points = {
'console_scripts': ['ppp=prala.__main__:main'],
},

Related

import inside a python package - ModuleNotFoundError

I'm trying to build a package where specific modules call other modules. Example of the structure of the package:
provapackage/
├── main.py
└── pippo
├── derivative_functions.py
├── functions_pippo.py
└── __init__.py
content of the functions_pippo module:
def add(x,y):
return x+y
content of the derivative_functions module:
from functions_pippo import add
def add_lst(l):
n=0
for i in l:
n = add(n,i)
return n
content of the main.py file:
from pippo.derivative_functions import add_lst
lst = [1,2,3,4]
print(add_lst(lst))
when I run main.py I get this error:
Traceback (most recent call last):
File "/home/g/Documents/provapackage/main.py", line 1, in <module>
from pippo.derivative_functions import add_list
File "/home/g/Documents/provapackage/pippo/derivative_functions.py", line 1, in <module>
from functions_pippo import add
ModuleNotFoundError: No module named 'functions_pippo'
It seems like python cannot find the functions_pippo module even if it's in the same folder. But when I run derivative_functions.py I don't gat any error message. Is it an import problem?
See this answer here
from .functions_pippo import add
not
from functions_pippo import add

No longer can create a Python app using "setup.py"

I was building apps in Python on a Mac. it was upgrading. I upgraded and now the program crashes in setup.py.
The code that I use is
"""
This is a setup.py script generated by py2applet
Usage:
python3 setup.py py2app -A
for this must do rm -rf dist build
To install a missing package "pip3 install MODULENAME"
or
python3 setup.py py2app for final App
or
/usr/local/bin/python3.10 setup.py py2app
"""
from setuptools import setup
APP = ['Analyze pH.py']
DATA_FILES = []
OPTIONS = {'iconfile': 'SVA.icns',
'argv_emulation': True}
setup(
app=APP,
version='3.5',
author='Howard Matis',
author_email='Howard#svadvantage.com',
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app']
)
When I run "usr/local/bin/python3.10 setup.py py2app" I get:
running py2app
creating /Users/matis/Library/Mobile Documents/com~apple~CloudDocs/Silicon Valley Advant/SensTek/pH Analysis/Analyze pH Files - V3.5/build/bdist.macosx-10.9-universal2/python3.10-standalone/app
creating /Users/matis/Library/Mobile Documents/com~apple~CloudDocs/Silicon Valley Advant/SensTek/pH Analysis/Analyze pH Files - V3.5/build/bdist.macosx-10.9-universal2/python3.10-standalone/app/collect
creating /Users/matis/Library/Mobile Documents/com~apple~CloudDocs/Silicon Valley Advant/SensTek/pH Analysis/Analyze pH Files - V3.5/build/bdist.macosx-10.9-universal2/python3.10-standalone/app/temp
creating build/bdist.macosx-10.9-universal2/python3.10-standalone/app/lib-dynload
creating build/bdist.macosx-10.9-universal2/python3.10-standalone/app/Frameworks
Traceback (most recent call last):
File "/Users/matis/Library/Mobile Documents/com~apple~CloudDocs/Silicon Valley Advant/SensTek/pH Analysis/Analyze pH Files - V3.5/setup.py", line 22, in <module>
setup(
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/setuptools/__init__.py", line 145, in setup
return distutils.core.setup(**attrs)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/distutils/core.py", line 148, in setup
dist.run_commands()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/distutils/dist.py", line 966, in run_commands
,,,
q = self._import_module(head, qname, parent)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/modulegraph/modulegraph.py", line 1421, in _import_module
m = self._load_module(fqname, fp, pathname, stuff)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/modulegraph/modulegraph.py", line 1512, in _load_module
self._scan_code(co, m)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/modulegraph/modulegraph.py", line 1719, in _scan_code
self._scan_bytecode_stores(compile(co, "-", "exec", 0, True), m)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/modulegraph/modulegraph.py", line 1752, in _scan_bytecode_stores
for inst in dis.get_instructions(co):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/dis.py", line 288, in get_instructions
line_offset)
UnboundLocalError: local variable 'line_offset' referenced before assignment
Help. I cannot figure out what is the problem. I am doing this on a PowerMac V 12.1.

Python call module on other directory but not find module

I learning python module , I want to put some file to one mudole dir, and then call it at the project root. but it not work. I try to solve it , but it still not success!
what is the problem ?
├── calculator
│   ├── __init__.py
│   ├── addTwo.py
│   ├── mulTwo.py
│   └── sum.py
└── main.py
# calculator/__init__.py
#nothing
# calculator/addTwo.py
def addFunc(lhs, rhs):
return lhs + rhs
# calculator/mulTwo.py
def mulFunc(lhs, rhs):
return lhs * rhs
# calculator/sum.py
import addTwo
def sumFunc(arg1, arg2, arg3):
ans = addTwo.addFunc(arg1,arg2)
ans = addTwo.addFunc(ans,arg3)
return ans
#main.py
from calculator import addTwo
from calculator import mulTwo
from calculator import sum
def test():
print(addTwo.addFunc(1,2))
print(mulTwo.mulFunc(1,2))
print(sum.sumFunc(1,2,3))
if __name__ == '__main__':
test()
run it
cong.lin#C02YN3P4LVCK  ~/Program/python/module_test  python3 main.py
Traceback (most recent call last):
File "main.py", line 4, in <module>
from calculator import sum
File "/Users/cong.lin/Program/python/module_test/calculator/sum.py", line 1, in <module>
import addTwo
ModuleNotFoundError: No module named 'addTwo'
✘ cong.lin#C02YN3P4LVCK  ~/Program/python/module_test 
You have to provide PYTHONPATH to root of your project
Try
export PYTHONPATH=$PWD
before running
python3 main.py

entrypoint module not found after install

In my python project, I have this hierarchy
+ slack_app
+ src
+ apps
+ intro
+ \__main__.py
+ slack_app.py
+ setup.py
This is my setup.py:
setup(
name= "my-slack-app",
packages = find_packages(),
entry_points = {
'console_scripts': [
'slack-app = src.apps.intro.__main__:main',
],
})
__main__.py:
def main():
print("WORKED")
if __name__ == "__main__":
main()
I run python3 setup.py install, then run my app which is installed in /usr/local/python3.6/bin but I always get this error when I run it:
Traceback (most recent call last):
File "/usr/local/python3.6/bin/slack-app", line 11, in <module>
load_entry_point('my-slack-app==0.1.dev2+g4fd21b0.d20200309',
'console_scripts', 'slack-app')()
File "/usr/local/python3.6/lib/python3.6/site-
packages/pkg_resources/__init__.py", line 490, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/usr/local/python3.6/lib/python3.6/site-
packages/pkg_resources/__init__.py", line 2854, in
load_entry_point
return ep.load()
File "/usr/local/python3.6/lib/python3.6/site-
packages/pkg_resources/__init__.py", line 2445, in load
return self.resolve()
File "/usr/local/python3.6/lib/python3.6/site-
packages/pkg_resources/__init__.py", line 2451, in resolve
module = __import__(self.module_name, fromlist=['__name__'],
level=0)
ModuleNotFoundError: No module named 'src.apps'
Any help please?
If you are using setuptools.find_packages, then you need to have __init__.py files. Also you are using the so-called src-layout so you need to adjust the package_dir and set the where parameter of find_packages.
Reference:
https://setuptools.readthedocs.io/en/latest/setuptools.html#using-find-packages

ModuleNotFoundError after packaging with pip

Why would I receive a ModuleNotFoundError: No module named 'helpers' when I execute the command prompt script after running pip3 install . from my package's root directory? The location of helpers is c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\mypackage\helpers.py so how to I get the program to look there?
PS C:\> mypackage
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts\mypackage-script.py", line 11, in <module>
load_entry_point('mypackage==0.1.2', 'console_scripts', 'mypackage')()
File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 572, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2755, in load_entry_point
return ep.load()
File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2408, in load
return self.resolve()
File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\pkg_resources\__init__.py", line 2414, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "c:\users\username\appdata\local\programs\python\python36-32\lib\site-packages\mypackage\__init__.py", line 16, in <module>
import helpers as h
ModuleNotFoundError: No module named 'helpers'
Here is the source tree:
PS C:\Users\username\source\repos\mypackage> tree /F
Folder PATH listing for volume mydisk
Volume serial number is 01234-A321
C:.
│ .gitignore
│ COPYING
│ MANIFEST.in
│ README.rst
│ setup.py
│
├───mypackage
│ │ helpers.py
│ │ __init__.py
│ │
│ └───__pycache__
│ helpers.cpython-36.pyc
│ __init__.cpython-36.pyc
│
└───tests
itsatrap.py
__init__.py
Here is the setup.py:
PS C:\Users\username\source\repos\mypackage> cat setup.py
from setuptools import setup, find_packages
from codecs import open
from os import path
path_to_here = path.abspath(path.dirname(__file__))
with open(path.join(path_to_here, 'README.rst'), encoding='utf-8') as readme_file:
readme = readme_file.read()
with open(path.join(path_to_here, 'COPYING'), encoding='utf-8') as license_file:
license = license_file.read()
setup(
name = 'mypackage',
version = '0.1.2',
license=license,
description = 'Who knows',
long_description = readme,
url = 'https://github.com/user/mypackage',
keywords = ['help!'],
packages = find_packages(exclude=['contrib', 'docs', 'tests']),
install_requires = ['matplotlib','numpy'],
extras_require = {
'dev': [''],
'test': [''],
},
entry_points = {
'console_scripts': [
'mypackage=mypackage:main'
],
},
)
Any help is appreciated. Packaging in Python is new territory for me! This program did run correctly when doing python mypackage/__init__.py from the package's root directory.
In Python 3 all imports are absolute. Use full path to import a module from a package:
from mypackage import helpers as h
To perform relative import do it explicitly:
from . import helpers as h

Resources