Importing a Class from another python file in Vscode - python-3.x

I have made this class in python3:
#!/usr/bin/env python3
class Post():
def __init__(self):
self.title="this is my title"
self.content= "this is some content"
self.author= "Ata"
And then wanted to import this class into another file named "app.py":
#!/usr/bin/env python
# importing class from post.py into app.py
from post import Post
post= Post()
post2= Post()
print (post.content)
but get this error:
ImportError: cannot import name 'Post'
what is the problem with the code?

Finally, I got my answer... The only thing that I had to do in order to import my class into another python file was that I should have saved it. that's all and after this everything works fine.

Related

How can I fix a very basic ImportError in Python3?

Similar to ImportError: Cannot import name X, I have a main.py file and, in the same folder, a few other python files bill.py and flatmate.py.
bill.py and flatmate.py each contain classes, nothing more.
Directory structure:
parentfolder
- main.py
- bill.py
- flatmate.py
- files
- pic.png
- doc.pdf
Sample code:
main.py:
import bill # won't work
from flatmate import Flatmate # won't work
...
bill.py:
class Bill:
def __init__(self, ...):
...
flatmate.py:
class Flatmate:
def __init__(self, ...):
...
I get an ImportError: cannot import name 'Bill' from 'bill' (c:\Users\lavid\Desktop\Python Experimente\Python OOP Kurs\App-2-Flatmates-Bill\bill.py). I am pretty sure I don't have circular references. What could be the cause of this problem?
Make sure that all files are next each others
try:
from . import module
if this doesn't work , add your files dir to the Question
Saving the referenced files before accessing them helped a lot.

ImportError importing class from file in same directory Python

I have two files
import_1.py
class Test:
pass
import_2.py
from import_1 import Test
Directory structure is:
Python_Testing
import_1
import_2
When I run import_2.py I get the error
File "c:\Users\61403\Desktop\Python_Testing\import_2.py", line 1, in <module>
from import_1 import Test
ImportError: cannot import name 'Test' from 'import_1' (c:\Users\61403\Desktop\Python_Testing\import_1.py)
Hovering over the Test on VSCode, it knows that it is a class.
Why is this error popping up?
Extremely silly. Auto Save wasn't turned on on my new laptop, so apparently import_1.py wasn't getting saved or sth and so could never be imported.

Django unable to get models module

I am learning Django and not sure what is causing the error 'ModuleNotFoundError: No module named 'models''
even though the files are in the same folder
files are available here
File "/Users/mayanksharma/Library/Mobile Documents/com~apple~CloudDocs/for Stackoverflow/app-7-django/blogs/urls.py", line 1, in <module>
from . import views
File "/Users/mayanksharma/Library/Mobile Documents/com~apple~CloudDocs/for Stackoverflow/app-7-django/blogs/views.py", line 2, in <module>
from models import Post
ModuleNotFoundError: No module named 'models'
Do you mean by?
from .models import Post
Replace from . import views with:
import sys
sys.append('..')
import views
This was something to do with python reading path instead of from .models import Post or from . import views I gave from app.models import Post and that made it work

Python import files from 3 layers

I have the following file structure
home/user/app.py
home/user/content/resource.py
home/user/content/call1.py
home/user/content/call2.py
I have imported resources.py in app.py as below:
import content.resource
Also, I have imported call1 and call2 in resource.py
import call1
import call2
The requirement is to run two tests individually.
run app.py
run resource.py
When I run app.py, it says cannot find call1 and call2.
When run resource.py, the file is running without any issues. How to run app.py python file to call import functions in resource.py and also call1.py and call2.py files?
All the 4 files having __init__ main function.
In your __init__ files, just create a list like this for each init, so for your user __init__: __all__ = ["app", "content"]
And for your content __init__: __all__ = ["resource", "call1", "call2"]
First try: export PYTHONPATH=/home/user<-- Make sure this is the correct absolute path.
If that doesn't solve the issue, try adding content to the path as well.
try: export PYTHONPATH=/home/user/:/home/user/content/
This should definitely work.
You will then import like so:
import user.app
import user.content.resource
NOTE
Whatever you want to use, you must import in every file. Don't bother importing in __init__. Just mention whatever modules that __init__ includes by doing __all__ = []
You have to import call1 and call2 in app.py if you want to call them there.

Kivy window dont't show up - AttributeError: module 'collections' has no attribute 'namedtuple'

Why I'm getting this error --> AttributeError: module 'collections' has no attribute 'namedtuple'. Whats wrong with my code..
I'm using python 3.5.2 on windows. When I execute my code on IDLE it can run - show up the kivy window, but when I execute C:\POS\operator\python operator.py it failed.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class OperatorWindow(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
class OperatorApp(App):
def build(self):
return OperatorWindow()
if __name__ == "__main__":
OperatorApp().run()
It looks like your file name is at fault here since there's also a standard library module called operator. I've got the same error but when I renamed your sample code from operator.py to test.py, it worked normally.

Resources