ModuleNotFoundError: No module named 'getpwnam' - python-3.x

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

Related

Rdkit: MaeMolSupplier NameError

I would like to able to extract details about a molecule in ".mae" format. I imported the rdkit.Chem.rdmolfiles functions and it seems to work for MolFromSmiles, but not for MaeMolSupplier as suggested in the 2019 documentation. Instead I get a NameError. Any aid/help in calling this function would be greatly appreciated.
Works OK with MolFromSmiles
import rdkit
from rdkit.Chem.rdmolfiles import *
mol = MolFromSmiles('C1NCN1')
print(mol)
(my-rdkit-env) [Me]$ python3 testrdkit.py
<rdkit.Chem.rdchem.Mol object at 0x7f237f917030>
Now to show the error
import rdkit
from rdkit.Chem.rdmolfiles import *
suppl = MaeMolSupplier(file('five.mae'))
print(suppl)
my-rdkit-env) [Me]$ python3 testrdkit.py
Traceback (most recent call last):
File "testrdkit.py", line 8, in <module>
suppl = MaeMolSupplier(file('five.mae'))
NameError: name 'MaeMolSupplier' is not defined
import * doesn't work here either.
Just import rdmolfiles.
from rdkit.Chem import rdmolfiles
suppl = rdmolfiles.MaeMolSupplier('five.mae')
print(suppl)
<rdkit.Chem.rdmolfiles.MaeMolSupplier object at 0x000002792CEFC5B0>

ModuleNotFoundError: No module named X although the module does exist

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

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

Python package not installing dependency with python3

I've created a reusable component (package) in Python but I'm having trouble using it with python3. My package uses a 3rd party library called requests like so, in one of my files core.py:
from __future__ import print_function
import requests
import math
import time
import csv
import os
...
Here is my setup.py:
from setuptools import setup
setup(
name = 'my_package',
packages = ['my_package'],
version = '0.1.dev4',
keywords = [ ... ],
install_requires=[
'requests',
'python-dateutil'
],
classifiers = [],
...etc
)
After installing my package on my system, I'm running into this error after starting python3:
>>> import my_package
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/matthew/Desktop/my_package/my_package/__init__.py", line 1, in <module>
from my_package.core import Class
File "/Users/matthew/Desktop/my_package/my_package/core.py", line 5, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
The requests module doesn't seem to be accessible by my package, why?

Resources