Python Forum
import not working properly - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: import not working properly (/thread-7621.html)



import not working properly - mepyyeti - Jan-18-2018

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...


RE: import not working properly - metulburr - Jan-18-2018

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-Namespace-flooding-with-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



RE: import not working properly - mepyyeti - Jan-18-2018

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?


RE: import not working properly - metulburr - Jan-18-2018

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.


RE: import not working properly - mepyyeti - Jan-18-2018

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...


RE: import not working properly - metulburr - Jan-18-2018

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.