Python Forum
Super simple import doesn't work. Please help!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Super simple import doesn't work. Please help!
#1
(Python 3.6.5)
I've searched this forum and google as well. Lot's of seemingly related problems but nothing really quite like mine.
My problem is earth-shatteringly simple, which makes it all the more frustrating...

I got two python scripts in the same folder, like so:
C:/Projects/Interner
auxScript.py
interner.py
(nothing else in that folder)

interner.py, the "main" program contains only this:
import auxScript
auxScript.py contains only this:
def printman():
    print("Aux is here.")
When I run interner.py (from the Interner folder!), I get:
Output:
Traceback (most recent call last): File "interner.py", line 1, in <module> import auxScript ModuleNotFoundError: No module named 'auxScript'
Additional info:
1. After the error, I did:
>>> import os; print(os.listdir("."))
['auxScript.py', 'interner.py']

2. From python interactive console:
>>> import sys; print(sys.path)
['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']

3. Printing system path from inside the main script (had to remove the import of auxScript)
C:\Projects\Interner>python interner.py
['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']

Soooo, while all the manuals on the internet say I can just 'import auxScript' because "script's path is added to sys path", I actually can't and I can easily see that neither my present working dir nor my script's location (which happen to be the same) are added to the sys.path.

Worth mentioning I installed python by unzipping a portable version and adding the executable to the path.
Maybe my installation method was wrong?
Reply
#2
I'm on the same version, also on Windows. Here's basically what it looks like you're doing:
E:\Projects\etc>python -V
Python 3.6.5

E:\Projects\etc>echo import other > spam.py

E:\Projects\etc>echo def something(): print("hi") > other.py

E:\Projects\etc>python spam.py

E:\Projects\etc>echo import other; other.something() > spam.py

E:\Projects\etc>python spam.py
hi
So what you're doing clearly works. Try renaming the folder/file. I don't know enough about the module system to be sure, but the file having the same name as the folder could possibly be confusing the importer? From what I know, the folder is only considered a module if it contains a file named __init__.py, but like I said, I don't know that for sure.
Reply
#3
Check spelling of auxScript.py i can only get exact same error with that file misspelled(or it could be missing).
E:\div_code\Interner
λ ls
auxScript.py  interner.py

# Run no errors
E:\div_code\Interner
λ python interner.py

# Rename
E:\div_code\Interner
λ rename auxScript.py auxScript9.py

E:\div_code\Interner
λ ls
__pycache__/  auxScript9.py  interner.py 

# Now get your error
E:\div_code\Interner
λ python interner.py
Traceback (most recent call last):
  File "interner.py", line 1, in <module>
    import auxScript
ModuleNotFoundError: No module named 'auxScript'
You do not need to think of sys.path when the 2 files are in same folder.
nilamo Wrote:From what I know, the folder is only considered a module if it contains a file named __init__.py, but like I said, I don't know that for sure.
No it's a module when just doing a import of one file,like this case.
__init__.py comes in when talking about package.
The __init__.py files are required to make Python treat directories as containing packages.
Reply
#4
I seem to have had the best direction of all the replies :)

Look at my last paragraph in the original post.

Turns out the embeddable python installation has a different default path setting. If you look at what I reported about system error, you will notice it's missing "" for the current path.

I scrapped that folder, installed regular python and everything works the same way that the tutorials talk about.

1. Very disappointing there's no Readme.txt in the embeddable version to mark the differences between it and the full Python (yes, I'd happily write one when I become a python expert)
2. Kinda disappointing that the tutorials don't mention different ways of installing python
Reply
#5
(Jul-24-2018, 05:29 PM)Mustey Wrote: 2. Kinda disappointing that the tutorials don't mention different ways of installing python

Is it? If every tutorial went into detail about every way to do any particular task, then every tutorial would just be about getting started, and would never get past that.
Reply
#6
(Jul-18-2018, 02:53 PM)Mustey Wrote: Worth mentioning I installed python by unzipping a portable version and adding the executable to the path.
well, which 'portable' version did you use? where did you get it from?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Jul-24-2018, 05:29 PM)Mustey Wrote: 2. Kinda disappointing that the tutorials don't mention different ways of installing python
Can look at Python 3.6/3.7 and pip installation under Windows.
Anaconda and other ways to run Python.
Reply
#8
(Jul-24-2018, 05:32 PM)nilamo Wrote:
(Jul-24-2018, 05:29 PM)Mustey Wrote: 2. Kinda disappointing that the tutorials don't mention different ways of installing python

Is it? If every tutorial went into detail about every way to do any particular task, then every tutorial would just be about getting started, and would never get past that.

Absolutely. If one does not understand something basic like how it integrates with the OS, then their work is usually something that shouldn't be accepted (but it does get accepted because of lack of good people and general buzz around anyone who puts Python in their CV).

Reminds me when I tried to learn stuff from the Selenium family and 95% of the people who offered to teach me (for money) were hilariously tackled out of the job at the very first "lesson", by simple questions.
Luckily I was able to lay my hands on someone who studied Selenium (later WebDriver) at university, by a computer science professor, rather than watching some outdated, barely audible, youtube video of the type "look, I can automate my browser this way, therefore everything you could possibly do with Selenium has to be done this way"

:D
Reply
#9
You do understand that if every/most of tutorials start with basics - e.g. how to install python, they will not get far, do you?
Normal learning process starts with basics, and when you, the student, are comfortable with your progress, move to next/new material/tutorial that builds on the previous knowledge you have gained.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
The tutorials don't get anywhere. It's the student who reads them that gets somewhere. If there was a good source that gave me confidence I can spend my time there and learn the technology properly, I would happily read even 2000 pages. I'd happily pay 2000 pounds as well.

BTW, the tone of this discussion is maybe a little argumentative. I would like to reiterate my appreciation towards your help and my general love towards everyone in this forum!

(think we exhausted the original topic, BTW)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020