import python file/module from parent/upper directory - python-3.x

i'm new to python and literely trying to import the module toolbox in the createRecipe.py file. Please see picture as it describes my problem
importProblem
I've looed in over 30 resources on the Internet and it seems there is no working solution for it. Though, i would like to raise the problem again, perhaps there is something new in python which makes this possible

Related

The file is in the same directory but cannot be imported

The python version I am using is 3.8.2
I searched a lot and most of the solutions are to use sys.path.append()
But it didn't solve the problem for me, if I use from . import players
it will say
ImportError: attempted relative import with no known parent package
if i use import players it will say
ModuleNotFoundError: No module named 'players'
The code I used to fix this:
sys.path.append(".")
sys.path.append(os.getcwd() + "\\players.py")
sys.path.append(os.getcwd())
still can't fix this,It's worth mentioning that at some point sys.path.append(os.getcwd() + "\\players.py") can run
Make sure that the name of the file is actually players.py.
And not for example players.py, players .py (note the spaces).
Also check for all other "invisible" Unicode characters that would not necessarily show up.
And make sure that there is no directory players as well.
I deleted the file completely so I couldn't test it, but I ran into this problem again, it's an import order problem, I tried to import attributes.py first and then content.py, there was a problem Check, can't find attributes.py , the problem is solved when their import order is swapped, I can't understand why, but if you encounter such problems please try once (other files are not imported)
I've had similar issues and I've created a new, experimental import library for Python to solve this kind of import error: ultraimport
Instead of from . import players it allows you to write
import ultraimport
players = ultraimport('__dir__/players.py')
This will always work, no matter how you run your code, no matter what is your current working directory and no matter if there's another directory somewhere called 'players'.

How to call a module from a previous parent directory?

I am running a Flask application at PythonAnywhere. I'm using Python 3.8. The folder structure looks like this:
project/
app.py
alpha/
beta/
gamma/
other.py
All of the folders have a blank __init__.py. From inside other.py, I want to do an import like:
from project.app import function
What is the cleanest way to make this happen?
I've seen suggestions to use import importlib.util in other answers. Will that work in PythonAnywhere? Instead of doing that in other.py is there a way to do this once and it work for the whole project? (so that things like from project.alpha.beta import function would work as well)
I have a partial answer. Partial because I'm not sure it is the best answer. In the PythonAnywhere web configuration the working directory is listed as /home/username/mysite/project. As per the example above, project had a module I was trying to reference. So at the top of app.py (in the project folder), I added this:
import sys
sys.path.append("/home/username/mysite")
Basically I added the directory above the module directory to the import path. That got me through the error, but it doesn't seem like the best solution.
I do want to give a shoutout to this PythonAnywhere help page because it has some good module debugging tips.

how to fix "Module 'cv2' has no ---- member pylint(no-member?

I am trying to start a project to learn how to use opencv and the first problem I encountered is this : "Module 'cv2' has no ---- member pylint(no-member) as posted in the picture.
I have found some information here in Stack Overflow but I'm afraid, I might not be proficient enough to understand what is going on. Could someone point me in the right direction to solve my problem?
Thanks!
One possible reason is that you might have a file named cv2.py somewhere on your machine other than the original cv2 module and now python is importing that file rather than the cv2 module.
Or perhaps you could've downloaded a wrong cv2 module, other than that i don't see anything that could've gone wrong as you don't have a complex code. Try reinstalling the module and/or removing a file named cv2.py (if you've accidentaly created one).
You can check the module by importing the module in the terminal and check for dir, on python console type import cv2 and then dir(cv2), now you should see all the classes contained in the cv2 module.
If the program runs correctly then I guess you are facing linting issue which is answer in this [https://stackoverflow.com/questions/26657265/hide-some-maybe-no-member-pylint-errors] question.
Solution is to turn off no-member linting error using below command
pylint disable=maybe-no-member
OR
pylint --disable=E1101

ImportError: No module named 'TkTreectrl'

I am pretty new to python and am trying to use the MultiListbox. It says I need to import TkTreectrl. I have already done import TkTreectrl as treectrl but it still give me the error.
After some reading online, I was under the impression I am supposed to add some file to the folder my program is in?
Can someone help me understand how exactly I can do this?
Here is a little checklist of what to do to get the file running:
1) Download the package file from the internet or get it whereever you can get it from
2) Unzip it (if nessecary)
3) Move it into the directory of your code file
4) Run your code, if there are no errors it will run.
I assume that the MultiListbox modules are in subfolders, you will need to call them like this:
from MultiListbox.subfolder import *
or
import Multilistbox
I hope I could help you!

Python 3 relative path conversion issue

I am currently working with converting Pycrypto over to Python 3.X
Whilst I seem to have the cryptography side working the same cannot be said for the tests
provided with the module :(
I have used the tests under Python 2.64 and all works fine.
I then ran '2to3' over the tests to generate new files in 3.X format.
There are several references to the following:
from .common import make_block_tests
Whenever I run the tests I get:
ValueError: Attempted relative import in non-package
If someone would point me towards a way to fix this it would be much appreciated :)
Cheers
Grail
You are trying to run the test files directly, then you can't have relative imports. Change them to be absolute imports, and it will solve the problem.

Resources