ModuleNotFoundError: No module named X although the module does exist - python-3.x

I have a very strange error with Python.
python main1.py
Traceback (most recent call last):
File "main1.py", line 1, in <module>
from sfr import hello
File "C:\sfr\hello.py", line 1, in <module>
from constants1 import OBJECT_NAME
ModuleNotFoundError: No module named 'constants1'
My main1.py looks like this
from sfr import hello
print('Hello World')
type sfr\hello.py
from constants1 import OBJECT_NAME
type sfr\constants1.py
OBJECT_NAME = 'salesforce_object_name'
I am unable to fathom this why am i getting no module named constants1 ?
The constants1.py is located in sfr folder

Since you are running the main1.py file from the parent directory (outside sfr), the import path should be from the base. That is,
from sfr.constants1 import OBJECT_NAME

You probably need to give imports from the root path of your project. If sfr is a package at root level, your statement should be:
from sfr.constants1 import OBJECT_NAME

Related

Calling a Python File from another file and viceversa

I have few common functions written in file a.py which is called from file b.py. And, also I have few common functions written in file b.py which is called from file a.py. but when I try to run these files I get error as unable to import name. Below is the code for reference.
I have just provided an example below, these files have common piece of code in real scenario which is specific to a functionality and that is the reason its being called from one another
a.py code below
from b import get_partner_id
def get_customer_id(tenant_id):
customer_id = tenant_id + "tenant_name"
return customer_id
def get_partner_details():
partnerid = get_partner_id()
data = {"partnerId": partnerid}
print(data)
b.py code below
from a import get_customer_id
def get_partner_id():
customer_id = get_customer_id("7687")
env = 'sandbox-all' + customer_id
return env
get_partner_id()
Below is the error for reference,
Traceback (most recent call last):
File "C:/testCases/b.py", line 1, in <module>
from a import get_customer_id
File "C:\testCases\a.py", line 2, in <module>
from b import get_partner_id
File "C:\testCases\b.py", line 1, in <module>
from a import get_customer_id
ImportError: cannot import name 'get_customer_id'

"ModuleNotFoundError" when importing my own classes

I have 2 files, both are in the same folder.
The file that I am trying to import is "HelloWorldClass.py"
(Code to HelloWorldClass.py)
class HelloWorld():
def __init__(self):
print("Hello World")
and the file that I am calling "HelloWorldClass" from is "ClassTest.py"
(Code to ClassTest.py)
from Classes import HelloWorldClass
HelloWorld()
and for some reason, I am getting this error...
Traceback (most recent call last):
File "D:/Coding file/Owl Hoot/Classes/ClassTest.py", line 3, in <module>
from Classes import HelloWorldClass
ModuleNotFoundError: No module named 'Classes'
>>>
Both files are in the Classes file. I don't see what I am doing wrong, can anyone help?
If both are in the same directory you can import like that:
from HelloWorldClass import HelloWorld

Cyclic import in Python3

I have the following directory structure:
my-game/
__init__.py
logic/
__init__.py
game.py
player.py
game.py and player.py have import dependency on each other (cyclic imports).
game.py has the following definition.
from logic.player import RandomPlayer, InteractivePlayer
T = 8
class Game:
def __init__(self, p1, p2)
...
# some other things
if __name__ == '__main__':
p1 = RandomPlayer()
p2 = InteractivePlayer()
g = Game(p1, p2)
...
player.py is as follows:
from logic.game import T
class Player:
def __init__(self):
...
class RandomPlayer(Player):
def __init__(self):
...
class InteractivePlayer(Player):
def __init__(self):
...
I am trying to run the game from logic/ directory but I get the following error.
$ python3 game.py
Traceback (most recent call last):
File "game.py", line 2, in <module>
from logic.player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named 'logic'
I then tried running game.py from the directory higher up (my-game/).
$ python3 logic/game.py
Traceback (most recent call last):
File "logic/game.py", line 2, in <module>
from logic.player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named 'logic'
What am I doing wrong? How can I make these cyclic imports work?
I have also tried using this import in player.py
from .game import T
and using
from .player import RandomPlayer, InteractivePlayer
in game.py.
In this case, I get a different error. For example, when running from my-game/,
$ python3 logic/game.py
Traceback (most recent call last):
File "logic/game.py", line 2, in <module>
from .player import RandomPlayer, InteractivePlayer
ModuleNotFoundError: No module named '__main__.player'; '__main__' is not a package
I get a similar error when running from logic/ directory.
I looked at this post but didn't understand where I was going wrong.
You are made a circulair import in your code try to remove it from your import
Vist this link you can find some other info about circular import :Remove python circular import

why when I use " from module import * " and I want want to see functions of the module by help (module) it is not working?

why should I use just import module to see the functions in it?
By help (module )
but it is not woking with from module import *
is there any way to see the functions of it by from module import *
When you do from module import * - only the exported symbols from module are added to your module. The name module itself is not imported. Since help merely looks at documentation of imported symbols, that's why your help(module) is not working.
>>> from os import *
>>> help(os)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined
>>>
What you can do is this:
>>> import os
>>> help(os)
>>>
>>> from os import path
>>>

ModuleNotFoundError: No module named 'getpwnam'

I want to use the following code to change the owner of files:
import os
from getpwnam import pwd
from getgrnam import grp
uid = getpwnam('edamame')[2]
gid = grp.getgrnam('staff')[2]
os.chown('/Users/edamame/workspace/git/chinese_nlp/venv/lib/python3.7/site-packages/psutil/_psosx.py', uid, gid)
But the following errors:
Traceback (most recent call last):
File "/Users/edamame/workspace/git/chinese_nlp/chinese_segmenter1.py", line 6, in <module>
from getpwnam import pwd
ModuleNotFoundError: No module named 'getpwnam'
Process finished with exit code 1
I am using Python 3.7 virtual env in PyCharm. I couldn't find the module called getpwnam to install. Which package should I install? Thanks!
You got the imports a bit backwards, I know, it happens :) Try this (pwd and grp are just standard Python library modules):
>>> from pwd import getpwnam
>>> getpwnam('root')[2]
0
>>> from grp import getgrnam
>>> getgrnam('root')[2]
0

Resources