I know many similar questions have been asked, but I feel like I've tried everything and I keep getting the same error. I followed this article https://gehrcke.de/2014/02/distributing-a-python-command-line-application/ and I think I've done the same. Anyway, here's my project structure:
nc-textmanager-tool/
├── nc_textmanager
│ ├── gui/
│ ├── textmanager/
│ ├── __init__.py
│ ├── __main__.py
│ └── nc_main.py
├── LICENSE
├── bundles.yaml
├── env.yaml
├── requirements.txt
├── README.rst
└── setup.py
And here's the content of setup.py
import setuptools
with open("README.md", "r", encoding="utf-8") as f:
long_desc = f.read()
with open("requirements.txt", "r", encoding="utf-8") as f:
requirements = f.readlines()
setuptools.setup(
name="nc-textmanager",
version="1.1.11",
author="XXX",
author_email="XXX",
description="A simple tool for viewing and searching through keys and values used by TextManger in NC projects.",
long_description=long_desc,
long_description_content_type="text/markdown",
packages=setuptools.find_packages(),
entry_points={
"console_scripts": ['nc_textmanager = nc_textmanager.nc_main:run']
},
url="???",
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.8',
install_requires=requirements
)
Installing it by running python .\setup.py install works fine, but when I try to run my nc_textmanager script from the console, I get this error:
> nc_textmanager
Traceback (most recent call last):
File "C:\Users\SEHE\AppData\Local\Programs\Python\Python39\Scripts\nc_textmanager-script.py", line 33, in <module>
sys.exit(load_entry_point('nc-textmanager==1.1.11', 'console_scripts', 'nc_textmanager')())
File "C:\Users\SEHE\AppData\Local\Programs\Python\Python39\Scripts\nc_textmanager-script.py", line 25, in importlib_load_entry_point
return next(matches).load()
File "C:\Users\SEHE\AppData\Local\Programs\Python\Python39\lib\importlib\metadata.py", line 77, in load
module = import_module(match.group('module'))
File "C:\Users\SEHE\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 984, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'nc_textmanager.nc_main'
I did try to put the nc_textmanager-script.py file in my project root and execute it there, and everything works fine. I'm thus thinking there's an issue with the working directory...
Using python 3.8 on Windows 10.
Related
I am creating a Python package with setuptools that has an underscore in its name. The underscore is, according to some naming rules, replaced by dashes.
I don't mind so much about the naming, but I cannot automatically import the package, because
pkg_resources.working_set is only giving the name with dashes and not underscores.
foo_bar
├─ foo_bar
| ├─ __init__.py
| ├─ foo_bar.py
└─ setup.py
python3 setup.py develop --user
Listing the pip packages shows that foo_bar is replaced by foo-bar:
pip3 list | grep foo
foo-bar 0.1 /home/XYZ/foo_bar
My problems start if I want to search through all imported modules and import foo_bar again:
import pkg_resources
import importlib
for d in pkg_resources.working_set:
if('foo' in d.project_name):
print(d.location, d.project_name, d.version, d.key)
libstr = d.key
print('Importing lib',libstr)
testlib = importlib.import_module(libstr)
/home/XYZ/foo_bar foo-bar 0.1 foo-bar
Importing lib foo-bar
Traceback (most recent call last):
File "/home/XYZ/foo_bar/test/import_foo_bar.py", line 10, in <module>
testlib = importlib.import_module(libstr)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'foo-bar'
Any ideas how to find the "proper" name? A manual import works:
In [1]: import foo_bar
In [2]: foo_bar.foo_bar
Out[2]: <module 'foo_bar.foo_bar' from '/home/XYZ/foo_bar/foo_bar/foo_bar.py'>
I'm using environment variables with FastAPI, but I'm getting a "ModuleNotFoundError" error while running the server with uvicorn main:app --reload:
Traceback (most recent call last):
File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/home/user/.pyenv/versions/3.9.2/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/home/user/.pyenv/versions/3.9.2/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
return future.result()
File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/server.py", line 67, in serve
config.load()
File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/config.py", line 458, in load
self.loaded_app = import_from_string(self.app)
File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 24, in import_from_string
raise exc from None
File "/home/user/.cache/pypoetry/virtualenvs/fastapi-alembic-Q2iwjFE4-py3.9/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "/home/user/.pyenv/versions/3.9.2/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "./main.py", line 3, in <module>
from app.core.config import settings
ModuleNotFoundError: No module named 'app'
This is my project tree:
|__fastapi-alembic
|__alembic
|__app
├── core
│ └── config.py
└── __init__.py
└── main.py
|__alembic.ini
|__poetry.lock
|__pyproject.toml
And these are the files:
# main.py
from fastapi import FastAPI
from app.core.config import settings
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
)
# config.py
import os
from pydantic import BaseSettings
class Settings(BaseSettings):
API_V1_STR: str = "/api/v1"
PROJECT_NAME: str = os.environ["PROJECT_NAME"]
settings = Settings()
I would like to know what the problem is & how could I solve it?
Assuming you're running uvicorn main:app --reload from the fastapi-alembic/app directory, your main.py needs to import your settings as from core.config import settings.
i.e. update your main.py to:
# main.py
from fastapi import FastAPI
from core.config import settings # This import here is what you want to update
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_V1_STR}/openapi.json",
)
As you appear to be running from within the app directory, you don't need to include app in your import statements.
I have been trying to organize my file structure in my flask app (the current structure is):
├───.git
├───.vscode
├───env
├───src
├─── __init__.py
├─── site.db
│ ├───main
│ │ └───__pycache__
│ │ └─── routes.py
│ │ └─── __init__.py
│ ├───posts
│ │ └───__pycache__
│ │ └─── routes.py
│ │ └─── __init__.py
│ │ └─── forms.py
│ │ └─── utils.py
│ ├───static
│ │ ├───css
│ │ ├───js
│ │ └───profile_pics
│ ├───templates
│ │ └─── ...
│ ├───users
│ │ └─── routes.py
│ │ └─── __init__.py
│ │ └─── forms.py
│ │ └─── utils.py
├───run.py
The site.db file for some reason is empty even though my __init__.py file has "db.create_all()"...
Although, I am getting this error when I try and request the index.html page
[2020-07-16 11:54:38,819] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1277, in _execute_context
self.dialect.do_execute(
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: post
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\_compat.py",
line 39, in reraise
raise value
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Owner\OneDrive\code\CloneBook\src\main\routes.py", line 13, in home
posts = Post.query.order_by(
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask_sqlalchemy\__init__.py", line 496, in paginate
items = self.limit(per_page).offset((page - 1) * per_page).all()
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\orm\query.py", line 3341, in all
return list(self)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\orm\query.py", line 3503, in __iter__
return self._execute_and_instances(context)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\orm\query.py", line 3528, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1014, in execute
return meth(self, multiparams, params)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\sql\elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1127, in _execute_clauseelement
ret = self._execute_context(
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1317, in _execute_context
self._handle_dbapi_exception(
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1511, in _handle_dbapi_exception
util.raise_(
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\util\compat.py", line 178, in raise_
raise exception
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\base.py", line 1277, in _execute_context
self.dialect.do_execute(
File "C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: post
[SQL: SELECT post.id AS post_id, post.title AS post_title, post.date_posted AS post_date_posted, post.content AS post_content, post.user_id AS post_user_id
FROM post ORDER BY post.date_posted DESC
LIMIT ? OFFSET ?]
Your code is most likely doing the db.create_all() before models have been imported (from whatever file they're in, since you don't have any models.py files). Importing models has the side-effect of registering them.
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'],
},
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