Python Forum

Full Version: import not working properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I am having difficulty import my files. I can import standard library modules with 0 problem
import foo does not work but from foo import * gets the job done. My concern is that the * method is basically unpythonic.

Also, my shebangs don't work...could these two issues be related?
for example #! usr/bin/env python3 at the top of my files does nothing I still have to
Output:
me@me-ubuntu:~/Documents/py/cs$ name0.py name0.py: command not found me@me-ubuntu:~/Documents/py/cs$ python3 name0.py __main__
Can anyone PLEASE offer some insight on this? It's very frustrating...
What is the content of the module foo?

(Jan-18-2018, 05:24 AM)mepyyeti Wrote: [ -> ]My concern is that the * method is basically unpythonic.
it is not a good habit to get into
https://python-forum.io/Thread-Basic-Nam...th-imports

the shebang is only needed if you are making it an executable.
sudo chmod +x name0.py
the ./ is required to say the file in this directory
./name0.py
only classes (logic/data) aka 'bulk'. standard library modules work fine, like os, sys, and so on...

with the shebangs...I have used sudo chmod u+x file.py ...safer in a way?
what do you define as import foo does not work? Do you get an error on import or does your classes just get a NameError when you try to use them? Need more detail.
ok...for example foo.py
class Foo():
   def __init__(self):
       print('foo')
When I import it into bar.py with import foo
I get a NameError that my class is undefined

[inline]
me@me-ubuntu:~/Documents/py/cs$ python3 bar0.py
Traceback (most recent call last):
File "bar0.py", line 5, in <module>
x=Foo()
NameError: name 'Foo' is not defined
me@me-ubuntu:~/Documents/py/cs$
[/inline]

when I use from foo import *
I get the expected print out of 'foo'

I find this very odd...
you need to reference it via

x=foo.Foo()
modulename.Classname()
Star import imports all from the module into your namespace. When you import foo, you have to refer to the module first.