Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import not working properly
#1
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...
Reply
#2
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
Recommended Tutorials:
Reply
#3
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?
Reply
#4
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.
Recommended Tutorials:
Reply
#5
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...
Reply
#6
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.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 564 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  Pathlib import not working chriswrcg 9 3,519 May-29-2022, 07:37 PM
Last Post: snippsat
  fpdf orientation not working properly KatMac 1 3,257 May-02-2021, 10:47 AM
Last Post: Pedroski55
Lightbulb Jupyter is not working properly brunolelli 3 2,931 Apr-23-2021, 03:22 AM
Last Post: Larz60+
  Properly import from sub/parent folders? oclmedyb 2 2,015 Dec-14-2020, 12:22 AM
Last Post: snippsat
  How to keep a loop containing a web import working in python executable? coder1384 3 2,820 Feb-22-2020, 06:49 PM
Last Post: snippsat
  file.write not working properly mnh001 11 4,336 Nov-09-2019, 10:20 PM
Last Post: mnh001
  hatching not working properly with matplotlib Staph 3 2,948 Jul-28-2019, 07:17 AM
Last Post: ThomasL
  import logging not working segerhult 3 13,120 Feb-07-2019, 05:31 PM
Last Post: buran
  CSV import is not working gehrenfeld 7 4,156 Dec-12-2018, 12:26 PM
Last Post: gehrenfeld

Forum Jump:

User Panel Messages

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