Unable to import module from current path in python3 - python

I'm working on flask project which is in python3.4, my project structure is like below,
|-- venv
|-- web
|--admin
|--settings
|--custom_settings.py
|--script.py
Here venv folder is python3 virtual env with flask & etc modules installed.
in script.py file, i am trying to import module from settings package as below,
from admin.settings.custom_settings import db, Role, Users
but when i try to execute the script i am getting error
user#mint ~/PRJ/web $ ../../venv/bin/python3 script.py
Traceback (most recent call last):
File "script.py", line 23, in <module>
from admin.settings.custom_settings import db, Role, User
File "/home/user/PRJ/web/admin/__init__.py", line 19, in <module>
admin.settings.custom_settings import db, Role, User
ImportError: No module named 'settings'
output from sys.path
/home/user/PRJ/admin/web
/home/user/PRJ/admin
/home/user/PRJ/venv/lib/python3.4
/home/user/PRJ/venv/lib/python3.4/plat-x86_64-linux-gnu
/home/user/PRJ/venv/lib/python3.4/lib-dynload
/usr/lib/python3.4
/usr/lib/python3.4/plat-x86_64-linux-gnu
/home/user/PRJ/venv/lib/python3.4/site-package
Please let me know what what I'm doing wrong here.

Related

How do I get the Flask package tutorial work with Poetry?

I'm using Poetry to manage my Python project (dependencies, package, etc) and I'm trying to implement the four-line hello world from Flask documentation: https://flask.palletsprojects.com/en/1.1.x/patterns/packages/#simple-packages
My folder structure looks like this:
myproject/
.venv/
myproject/
__init__.py
views.py
poetry.lock
pyproject.toml
README.rst
The two files, init.py:
import myproject.views
__version__ = '0.1.0'
from flask import Flask
app = Flask(__name__)
And views.py:
from myproject import app
#app.route('/')
def index():
return 'hello world'
Yet when I run export FLASK_APP=myproject and FLASK_ENV=development and do flask run, and point my browser to the port, it says
flask.cli.NoAppException
flask.cli.NoAppException: While importing "myproject", an ImportError was raised:
Traceback (most recent call last):
File "/myproject/.venv/lib/python3.9/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/myproject/myproject/__init__.py", line 1, in <module>
import myproject.views
File "/myproject/myproject/views.py", line 1, in <module>
from myproject import app
ImportError: cannot import name 'app' from partially initialized module 'myproject' (most likely due to a circular import) (/myproject/myproject/__init__.py)
How can I fix this? I'd like to use the recommended pattern.
In your terminal, type poetry run flask run.
I believe your issue comes from importing the views at the top of __init__.py. Try moving the import statement to the end of the file, just like the tutorial does.

No module named 'instance' while trying to run a Flask app

Traceback (most recent call last):
File "manage.py", line 6, in <module>
from app import create_app, db
File "C:\Users\miran\Dropbox\PyReddit-master\app\__init__.py", line 6, in <module>
from instance.config import app_config
ModuleNotFoundError: No module named 'instance'
I am testing out this repository (https://github.com/tuvtran/PyReddit) and it keeps telling me there is no module instance, I have searched but found nothing that helped the error. The venv is in the same folder as the script.
Essentially this is occurring because the app\__init__.py is is importing a file in the instance directory. As per the Flask documentation, this directory normally contains instance configuration overrides default configurations.
Unfortunately, the project currently saves its default configurations in this directory, but it is not checked into Git (see .gitignore). To fix this:
Remove from instance.config import app_config
Add a configuration file following these instructions
Import the configuration file you just created
Replace app.config.from_object(app_config[config_name]) with app.config.from_object(your_configuration)

why do I get an error message 'No module named...'

Starting from a new directory, I clone the following repository:
https://github.com/deribit/deribit-api-python
I rename the cloned directory to deribit_api_python
In the base directory, I then have a one line Python file:
from deribit_api_python import RestClient
This results in:
Traceback (most recent call last):
File "my_code.py", line 1, in <module>
from deribit_api_python import RestClient
ImportError: cannot import name 'RestClient'
Why does my code generate an error?
in __init__.py file write
from .deribit_api import RestClient
or you need to change intial calling by
from deribit_api_python.derbit_api import RestClient
The best way to implement the package would be to install via pip as mentioned in the repository. pip install deribit-api and then import it via from deribit_api import RestClient

Heroku Moduke Not Found Error

[![][1]][1]
the module 'pyicloud' is listed in requirements.txt
I have checked using the 'ls' command- I checked the directory in which the package should be - and the package does exist on the heroku server.
The app gives no such issues on my local machine, even though there is no difference in the code
I also tried the suggestions given here : https://help.heroku.com/BWJ7QYTF/why-am-i-seeing-importerror-no-module-named-site-when-deploying-a-python-app
but it didnt work.
(venv)dhcp-10-4-0-197:mes-amis mananhora$ heroku run python manage.py db upgrade -a mesamis
▸ heroku-cli: update available from 6.16.18-62346b1 to 6.99.0-ec9edad
Running python manage.py db upgrade on ⬢ mesamis... up, run.1121 (Free)
Traceback (most recent call last):
File "manage.py", line 5, in <module>
from project import app, db
File "/app/project/__init__.py", line 40, in <module>
from project.import_data.functions import import_data_blueprint
File "/app/project/import_data/functions.py", line 6, in <module>
from pyicloud import PyiCloudService
ModuleNotFoundError: No module named 'pyicloud'

cannot build up a django project

every time I tried to build a django project. I have no initialized files like manage.py in my project.
Then I tried to create the project on command line, I got this:
C:\Python34\Lib\site-packages\Django-1.9.3-py3.4.egg\django\bin> python .\django-admin.py startproject "MySite"
Traceback (most recent call last):
File ".\django-admin.py", line 2, in <module>
from django.core import management
File "C:\Python34\lib\site-packages\django-1.9.3-py3.4.egg\django\core\management\__init__.py", line 10, in <module>
from django.apps import apps
File "C:\Python34\lib\site-packages\django-1.9.3-py3.4.egg\django\apps\__init__.py", line 1, in <module>
from .config import AppConfig
File "C:\Python34\lib\site-packages\django-1.9.3-py3.4.egg\django\apps\config.py", line 6, in <module>
from django.utils.module_loading import module_has_submodule
File "C:\Python34\lib\site-packages\django-1.9.3-py3.4.egg\django\utils\module_loading.py", line 67, in <module>
from importlib.util import find_spec as importlib_find
ImportError: cannot import name 'find_spec'
Somebody could help me? I really appreciate your help.
ps: python3.4, django_1.9
U need to put your code here because it seems you are having too many import errors
You are trying to create a Django project from within your Python installation (C:\Python34\Lib\site-packages\Django-1.9.3-py3.4.egg\django\bin>).
Try going to your home directory and trying there. django-admin should be in C:\Python34\Scripts\ and that should be in your PATH so you should be able to call it from anywhere.
PS Use virtualenv

Categories

Resources