-1

Can't import the function "ident" from "modulename"

def ident(key):
    import pandas as pd
    data = pd.read_csv("dicc.csv")
    data = data.T
    data = data.to_dict()
    print(data[0]["{}".format(clau)])

to my script

from modulename import ident

ImportError: cannot import name 'ident' from 'modulename' (/home/. . .)

Poojan
  • 3,366
  • 2
  • 17
  • 33
Davitens
  • 33
  • 1
  • 1
  • 5
  • to import module, the script your are importing it into and the module must me at the same directory – finally Nov 20 '19 at 16:01
  • 2
    Is filename correct? Is the file you are calling in path? Please post your working directory structure for debugging. – Poojan Nov 20 '19 at 16:01
  • @finally That's not true. The module just must be on `sys.path`. – Iguananaut Nov 20 '19 at 16:02
  • @Iguananaut ye, u`r right, but it`s easier to say like that, cause i think he works with a project so – finally Nov 20 '19 at 16:04
  • "script" and "modulename" are in the same directory. The filename is correct too. – Davitens Nov 20 '19 at 16:04
  • Is it actually called "modulename" or something else? What is the filename? What happens if you just run `python -c 'import modulename; print(modulename)'`? – Iguananaut Nov 20 '19 at 16:05
  • Works for me. Please provide name of directories/files your are working with. – Tim Nov 20 '19 at 16:07
  • @Iguananaut its called modulename. It returns me "" – Davitens Nov 20 '19 at 16:09
  • 1
    Is your current working directory the one that your module is in? Having your script in the same directory makes no difference if that directory isn't on your `sys.path`. Try, for now, putting `import os, sys; sys.path.insert(0, os.path.dirname(__file__))` in your script. – Iguananaut Nov 20 '19 at 16:10
  • @Iguananaut It gives me this error [ TypeError: expected str, bytes or os.PathLike object, not module ] and i don't know how to solve it... i searched but did not find a solution. I'm totally a beginner – Davitens Nov 20 '19 at 16:22
  • @Davitens What gives you that error? You have a typo or something. – Iguananaut Nov 20 '19 at 16:27
  • i think i was doing it the wrong way. I tried again, and sys.path.insert gives me [" NameError: name '__file__' is not defined"]. Searched again and couldn't find or understand the solution. – Davitens Nov 20 '19 at 16:48
  • Are you sure you used `__file__` with two underscores? Nevertheless, it doesn't matter; per my answer you shouldn't have to do that. If it still doesn't work I'd have to see more.... – Iguananaut Nov 20 '19 at 16:59

1 Answers1

3

Since you're a total beginner, here's a complete example:

// modulename.py

import pandas as pd


def ident(key):
    data = pd.read_csv("dicc.csv")
    data = data.T
    data = data.to_dict()
    # Note: Here 'clau' is undefined, but maybe you have it elsewhere in your code
    print(data[0]["{}".format(clau)])

// script.py

#!/usr/bin/env python
from modulename import ident

print('look; I imported a function:', ident)

Now you can run:

$ python script.py

or from some arbitrary directory it will work the same:

$ cd ~
$ python path/to/script.py

Earlier I suggested adding something like this at the top of your script:

import os
import sys

sys.path.insert(0, os.path.dirname(__file__))

However, this should no longer be necessary, since when you run a Python script as your main module its directory automatically gets inserted at the beginning of sys.path, so it should just work.

I suggest studying more about Python modules and the module search path (I realize it's annoying technical overhead when you just want to process some data, but if you're going to be using Python you'll thank yourself later by learning these concepts). Here's one such tutorial (no affiliation): https://realpython.com/python-modules-packages/

Iguananaut
  • 21,810
  • 5
  • 50
  • 63