0

My problem is very similar to the one here (yes, I know, sorry), but none of the suggested solutions have any effect.

I am trying to use a python3 module located in the same directory as the main program. I am launching from within it, and even though I have also added it to the sys.path (using this for help).

>>> import sys
>>> sys.path.append("/home/name/Documents/PythonFiles")

I still receive an ImportError no matter what I do. This has remained true both for my actual program and a simplified version using "hello.py3" and "test.py3" identical to the ones used in the earlier question (see below).

hello.py3:

# !/usr/local/bin/python
# -*- coding: utf-8 -*-

def hello1():
    print('HelloWorld!')

test.py3:

# !/usr/local/bin/python
# -*- coding: utf-8 -*-

import os
print(os.path.abspath(__file__))

from hello import hello1

hello1()

This is the output of running test.py3:

 /home/name/Documents/PythonFiles/test.py3
    Traceback (most recent call last):
    File "trial.py3", line 7, in <module>
    from hello import hello1
    ImportError: No module named 'hello'

All the sources I have checked say that as long as either the directories of the files are the same or if the sys.path directs to their directory, the modules should import without issue, but then again I haven't used modules before so I'm open to the fact that I missed something basic.

Either way I apologize if this really seems like a duplicate question, I am at a loss for what to do here. What should I try next?

  • 2
    can you please show your code? – Haifeng Zhang May 31 '18 at 19:09
  • 2
    It could be a number of different issues. Do you have an `__init__.py` file in that directory? Are you using Spyder as an IDE which has an [open issue about not detecting `.py3` extensions](https://github.com/spyder-ide/spyder/issues/4604) – roganjosh May 31 '18 at 19:12
  • @roganjosh I am using Ubuntu, so I just run it from the terminal – creative_name May 31 '18 at 19:18
  • So you just type `./test.py` in terminal? Add `import os`, `print(os.path.abspath(__file__))` just before `from hello import hello1`. – CristiFati May 31 '18 at 19:19
  • What is the error? Is it `ModuleNotFoundError: No module named 'hello'` or `ImportError: cannot import name 'hello1'` or something else? Can you `import hello`? What does `import hello; print(hello)` print out? – zvone May 31 '18 at 19:59
  • and what does it output? – CristiFati May 31 '18 at 20:50

0 Answers0