The file is in the same directory but cannot be imported - python-3.x

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'.

Related

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 functions

Image
I don't know what am I doing wrong here. I am importing the module from the functions package in the tests package. I tried every method but couldn't solve this problem while I tried to run valid_test.py
You need to use one dot before the functions so the python will know it is in the folder "above" the current location (import statement).
This look like this issue.
When specifying what module to import you do not have to specify the
absolute name of the module. When a module or package is contained
within another package it is possible to make a relative import within
the same top package without having to mention the package name. By
using leading dots in the specified module or package after from you
can specify how high to traverse up the current package hierarchy
without specifying exact names. One leading dot means the current
package where the module making the import exists. Two dots means up
one package level. Three dots is up two levels, etc. So if you execute
from . import mod from a module in the pkg package then you will end
up importing pkg.mod. If you execute from ..subpkg2 import mod from
within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification
for relative imports is contained within PEP 328.
There is another way to solve this issue by adding the "father" folder to the sys.path
by:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__, '..')))
I have been getting the same problem again and again while using SPYDER with anaconda.
I make my own function in other file and import those functions. The code was still not running giving me the error "ModuleNotFoundError: No module named 'my_functions".
I am sure you are selecting lines and executing only selected line only\
Solution to problem is here below
To execute your code as whole/line by line Follow the steps.
Change the working directory of spyder to the folder of current file as here https://pasteboard.co/ee79y0dLqzu4.png .
select lines and execute or you can also execute the whole code.

How can I set up an alias for imports in Python?

I want my project to work in two different situations. It should work as a standalone library, but also as sub package of a larger project. The main use case is that of a standalone library, where its internal imports should be of the form
from my_library import sub_package
When using the code as sub package of a larger project, these imports don't work as there is no global name my_library. Instead, I would have to use relative or absolute imports, for example
from large_project.my_library import sub_package
Let's assume I wrote my library as shown in the first example. How can I overwrite importing behavior when running as part of a larger project to automatically adjust import paths?
Thanks to #MatrixTai's suggestion of adding the parent directory of the package to the the module path, I came up with this dynamic solution. At the top of my_library/__init__.py:
# Make package global even if used as a sub package to enable short imports.
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
We have to navigate up two directories here to do from the my_library/__init__.py to my_library and from there to its parent direction, where imports will find the library.
You don't have much choice.
If you want to reference the my_library.py anywhere, there is 2 method (as I known) can do similar work.
1: add system path. Like those module you installed by pip. pip module is installed in /Python/Scripts. You can add a new path or simply put my_library.py into one of the path. For adding, that's in Computer(right-click)-> Properties -> Environment Variable -> Choose Path and Click Edit
(Though you may not want to use this.)
2: Changing __init__.py, but still at least one line you must add in my_library.py.
For example,
/Directory
/large_project
-__init__.py #call this sub_init
-my_library.py
-__init__.py #call this main_init, this fake
-main.py
In main_init,
import sys
sys.path.append('\\Directory\\large_project')
As main_init is not executed when you execute main.py (this is fake), so in main.py
import __init__
from my_library import sub_package
But as well you can take this main_init as the starter of library, like declaring __all__, etc.

Relative import not working when importing module from within the same package

I have the following structure:
ds/
BST.py
BSTNode.py
When I do
from .BSTNode import BSTNode
inside BST.py, and I run from the IDLE BST.py from within ds I receive the following error:
SystemError: Parent module '' not loaded, cannot perform relative import
Why am I receiving it? Note that I am not asking how to fix the problem, because it would work if I simply do
from BSTNode import BSTNode
i.e. without the leading .. I just want to understand better how the Python's import system works.
Note that theoretically I should not need any __init__.py within ds because I am using Python 3.5. If I am not clear enough, just ask!
Thank you!!!!

How to structure my little python framework

I wrote a simple set of python3 files for emulating a small set of mongodb features on a 32 bit platform. I fired up PyCharm and put together a directory that looked like:
minu/
client.py
database.py
collection.py
test_client.py
test_database.py
test_client.py
My imports are simple. For example, client.py has the following at the top:
from collection import Collection
Basically, client has a Client class, collection has a Collection class, and database has a Database class. Not too tough.
As long as I cd into the minu directory, I can fire up a python3 interpreter and do things like:
>>> from client import Client
>>> c = Client(pathstring='something')
And everything just works. I can run the test_files as well, which use the same sorts of imports.
I'd like to modularize this, so I can use it another project by just dropping the minu directory alongside my application's .py files and just have everything work. When I do this though, and am running python3 from another directory, the local imports don't work. I placed an empty init.py in the minu directory. That made it so I could import minu. But the others broke. I tried using things like from .collection import Collection (added the dot), but then I can't run things in the original directory anymore, like I could before. What is the simple/right way to do this?
I have looked around a bit with Dr. Google, but none of the examples really clarify it well, feel free to point out the one I missed
In this file ...minu/__init__.py import the submodules you wish to expose externally.
If the __init__.py file contains the following lines, and the client.py file has a variable foo.
import client
import collection
import database
Then from above the minu directory, the following will work:
from minu.client import foo

Resources