0

I have a file gufu.py, which has about 4 functions:

def parse1():
    ...
def parse2():
    ...
def parse3():
    ...
def parse4():
    ...

but when I run the following in python shell via:

>>> from gufu import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'gufu'
  • Although, I am running python from the same directory.

  • I'm VERIFYING that I am in the same directory as the file via:
    import os; os.chdir("/path/to/thefile"); os.getcwd() as well.


UPDATE

>>> import os; os.chdir("/thepath/to/the/file")
>>> os.getcwd()
'/thepath/to/the/file'
>>> from gufu import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'gufu'
>>> os.path.isfile('gufu.py')
False
>>> os.getcwd()
'/thepath/to/the/file'
>>> os.listdir()
['tests', 'urls.py', '__init__.py', 'models.py', 'tests.py', 'views.py', 'database.py', 'models.py.bak', 'hashers.py', 'apps.py', 'injection__backup.py', '__pycache__', 'urldata.py', 'Injection.py', 'gufu.py', 'Xss.py', 'templates', 'helpers', 'migrations', 'forms.py', 'models.bakFULLTBL', 'static', 'admin.py']

Q: Can someone please help me understand why this is not working?

Andy K
  • 4,944
  • 10
  • 53
  • 82
John
  • 155
  • 4
  • 16
  • 1
    `parse4():` isn't a syntax I recognise as python. Don't you mean `def parse4():`? – John May 06 '18 at 16:14
  • 1
    @John is your file in your root folder? – Andy K May 06 '18 at 16:15
  • @John - I don't see how that affects the problem – tdelaney May 06 '18 at 16:16
  • @AndyK - from the question _I am running python from the same directory._ – tdelaney May 06 '18 at 16:17
  • @all - yes, it has def() ... updating – John May 06 '18 at 16:18
  • @tdelaney sometimes you assume it is and when you double check, you are not in the same directory... – Andy K May 06 '18 at 16:18
  • How are you running python? Is that a command line python shell, or perhaps IDLE or some IDE? Perhaps your current directory isn't what you think it is. – tdelaney May 06 '18 at 16:18
  • 1
    @tdelaney - python shell. Im explictly making sure im in the same directory...via `import os; os.chdir("path/to/the/file"); os.getcwd()` – John May 06 '18 at 16:19
  • 1
    @AndyK - well, yes, and that was the question I was writing when I saw yours. The problem is that "root" directory is very ambiguous. – tdelaney May 06 '18 at 16:19
  • anyone? Updated more debugging ive done in my question. – John May 06 '18 at 16:22
  • 1
    Python typically puts `''` in `sys.path` so that local .py files import. You could check that and also whether `os.path.isfile('gufu.py')` is true, in case there is some strange misspelling. It should work. – tdelaney May 06 '18 at 16:22
  • 1
    @all - see **UPDATE** above...this is insane! – John May 06 '18 at 16:30
  • 2
    given **`os.path.isfile()` has returned FALSE**, *Houston, we have a problem here...* ( how about also showing printscreen with the **`ls -la`** ... showing all attributes and ownerships of the said file? ) – user3666197 May 06 '18 at 16:33
  • 1
    Is it possible your file's name is not what you think? `os.listdir()[14]` should be `gufu.py` and its length 7... can you test `x = os.listdir()[14]; print(x); print(len(x)); print(x == "gufu.py")`? Could be some invisible characters snuck in? Can you print it out from your shell with `cat gufu.py` (without using autocompletion, typing the name yourself)? – Amadan May 06 '18 at 16:37
  • Huh, this looks like the inverse problem to https://stackoverflow.com/q/29548587/674064 ... (so, **not** a duplicate of that.) – das-g May 06 '18 at 16:44
  • This might be an issue with importing modules in shell, see here https://stackoverflow.com/questions/7971744/importing-a-module-in-idle-shell although I do find it weird that `False` is returned on the `.isfile` method. – Generic Snake May 06 '18 at 16:44
  • have you executed `gufu.py` to create the byte code file to be imported? – Alan Hoover May 06 '18 at 16:48
  • Is it a permission issue perhaps?? Further, what permission do files need to be to be run in python shell e.g. `python` ? – John May 06 '18 at 16:56

0 Answers0