import a module from other python folder - python-3.x

I have a a project with following path:
shift/
Code/
__init__.py
mymain.py
run.py
Test/
__init__.py
tester.py
runtester.py
requirments.txt
in mymain.py there is a class which I need to import from the tester.py. So in tester.py I'm importing class as following codes but none of them work.
from Code.mymain import NewClass
from .mymain import NewClass
from mymain import NewClass
from shift.Code.mymain import NewClass
I also tried to insert the path in tester.py:
cwd = os.getcwd()
sys.path.insert(0, cwd)
However still confused why it does not know my file

Try like this
import sys
import os
sys.path.insert(1, os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '\\Code')
from mymain import NewClass
More details at : importing files from different folder

Related

Kotlin - menuInflater cannot find reference of the xml file

I am trying to add create option menu to my activity, however the android-studio sent me this error
Unresolved reference: chat_app_menu
Here is my code in Kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.chat_app_menu)
return true
}
The line with menuInflater.inflate(R.menu.chat_app_menu) show me the error Unresolved reference: chat_app_menu
However, I have been create the resource file chat_app_menu.xml under res\menu\
Here are my import files
package com.example.chat_app
import android.R
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import androidx.appcompat.app.AppCompatActivity
import com.google.firebase.auth.FirebaseAuth
My file directory:
What should I do to disable this error to make the app work?
Use your com.example.chat_app.R instead of android.R
import com.example.chat_app.R

How can I import a file present in different folder in python?

I have the following structures:
SnapQuick
main
snap_view.py
login.py
signup.py
profile
profile.py
profile.py has the following imports
from os import sys
sys.path.insert(1,'../')
from main import login
import sqlite3
class profile():
def show_profile(self,user_id):
pass(some_code....)
login.py has the following imports
import sqlite3
from os import sys,system
sys.path.insert(1,"../")
import signup
from profile import profile
import snap_main
class login:
def login(self):
On running profile.py I am getting an error
Error:
Traceback (most recent call last): File "profile.py", line 6, in
from main import login File "../main/login.py", line 6, in
import signup ModuleNotFoundError: No module named 'signup'
Try this:
import sys
sys.path.append(".../SnapQuick/profile") (you need whole path)
from profile import login

Not able to import name from root of the project

I am having a server.py file which is written in Falcon. Which looks like this.
try:
import falcon, logging
from os.path import dirname, realpath, join
from wsgiref import simple_server
from .config.config import server_config
from .middlewares.SQLAlchemySessionManager import SQLAlchemySessionManager
from .middlewares.GlobalInternalServerErrorManager import InternalServerErrorManager
from .lib.dbConnector import Session
from .routes import router
except ImportError as err:
falcon = None
raise err
serv_conf = server_config()
salescoachbot = falcon.API(middleware= [
SQLAlchemySessionManager(Session),
InternalServerErrorManager()
])
But when I am trying to import "salescoachbot" to other folder and files
like:
from ..server import salescoachbot
This gives me an error saying that the
from ..server import salescoachbot
ImportError: cannot import name 'salescoachbot'
The server.py is in the root of the project and has an init.py as well as the file which is trying to import the name.
What am I doing wrong here?

import error while using db = SQLAlchemy(app)

I get an Import error while importing the thermos.py module in my models.py module.
C:\Users\sys\Thermos\thermos\thermos.py
C:\Users\sys\Thermos\thermos\models.py
Here is the relevant part of the thermos.py module.
import os
from datetime import datetime
from flask import Flask, render_template, url_for, request, redirect, flash
from flask_sqlalchemy import SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'thermos.db')
db = SQLAlchemy(app)
And, here is the relevant part of the models.py module.
from datetime import datetime
from thermos import db
Here is the image of the error I receive in CMD:
Kindly let me know what needs to be done to fix this issue.
Python can't decide if you're trying to import from the folder, or the file named thermos
You can rename
C:\Users\sys\Thermos\thermos\thermos.py
To
C:\Users\sys\Thermos\thermos\__init__.py
This makes the thermos directory a package with the db variable that can be used with an import
from . import db

exe created by cx-freeze gives me a error

Log.py
import logging
import logging.handlers
class Log:
def __init__(self):
FILENAME='LOG'
logging.basicConfig(level=logging.INFO)
root_logger = logging.getLogger('')
logger = logging.handlers.TimedRotatingFileHandler(FILENAME,'midnight',1)
root_logger.addHandler(logger)
logging.getLogger('log')
Main.py
from Log import Log
import time
import logging
log_obj = Log()
log = logging.getLogger('log')
log.info("Service Started")
while 1:
t=1
setup.py
from cx_Freeze import setup, Executable
setup(
name = "Test",
version = "0.1",
description = "Test",
executables = [Executable("Main.py", base="Win32GUI")])
So this is the final code which I am using. EXE file got created but I am getting an error while running it.The error is "Nonetype object has no attribute type 'write'"
Waiting for you reply.
Without a stack trace, i can only advise you to try base="Console".

Resources