Can't create instance of class from other file in Atom/Hydrogen - python

I am having problems with Hydrogen in Atom on MacOS when trying to create an instance of a class from another file/module. The code does work when run in the command-line, but not in Atom. I have already set the directory to start kernel in to "Current directory of the file", which enables me to create an object of that other file and use functions, but not classes.
Here is the code in the two respective files. Again, importing File2 is not the problem, neither is using functions from File2 in File1. It is only classes that don't work.
#File1
import File2
from File2 import MyClass
y=MyClass('test')
print(y.name)
#File2
class MyClass:
def __init__(self, x):
self.name=x
Running File1 gives me the error message, when run in Atom.
ImportError Traceback (most recent call last)
<ipython-input-92-444367378d7c> in <module>
----> 1 from File2 import MyClass
ImportError: cannot import name 'MyClass' from 'File2' (/Users/.../File2.py)

Related

An idea of importing an user define python package

The structure needs for python 3.8+
That x.py contains x class with a 'display' method
concept one:
from p_abcd import a as A
''' call display '''
A.x.x().display()
Got an error
Traceback (most recent call last):
File "w.py", line 4, in
A.x.x().display()
AttributeError: module 'p_abcd.a' has no attribute 'x'
concept two:
import p_abcd.a as A
''' call display '''
A.x.x().display()
Got the same error
You can import only .py files. Not folders.
So you need something like
from p_abcd.a import x
x.display()

Python wont allow me to import .py files from same directory

So I'm just starting to learn Python, and I am learning classes and imports, but for some reason even when I follow the same code as my book say to do, I get a traceback error.
Traceback (most recent call last):
File "C:/Users/Programming/Desktop/Programs/EX40/main.py", line 1, in <module>
import objects.py
ModuleNotFoundError: No module named 'objects.py'; 'objects' is not a package
Here is both my Python files in which I'm trying to link:
main.py
import objects.py
print(MyStuff.tangerine)
objects.py
class MyStuff(object):
def __init__(self, arm):
self.tangerine = 'And now a thousand years between'
self.arm = arm
def apple(self):
print('I AM CLASSY APPLES!')
Try using
import objects
Because it takes py as a module in the objects file which does not exist
import objects.py is not a valid import
https://docs.python.org/3/reference/import.html
Try
from objects import MyStuff
Instantiating the class:
mystuff = MyStuff("arm value")
print(mystuff.tangerine)

Importing a subclass where it is under a package: ModuleNotFoundError

./package/test.py is working smoothly. I am expecting ./test.py would work smoothly just like that, but I am getting this running ./test.py:
Traceback (most recent call last):
File "test.py", line 2, in <module>
from package.subclass import Subclass
File ".../package/subclass.py", line 1, in <module>
from subclass import Subclass
ModuleNotFoundError: No module named 'subclass'
It is able to import class1. When it reads the 1st line of the subclass, it gives ModuleNotFoundError.
I have tried with ./package/__init__.py, a empty one gives the same error as above. When I have proper imports in ./package/__init__.py, the error becomes not being able to even find class1 in line 1 of __init__.py.
File directory as below:
./package/class1.py
./package/class2.py
./package/subclass.py
./package/test.py
./test.py
Code:
# ./package/class1.py
class Class1():
...
# ./package/class2.py
class Class2():
...
# ./package/subclass.py
from class2 import Class2
class Subclass(Class2):
...
# ./package/test.py
from class1 import Class1
from subclass import Subclass
...
# ./test.py
from package.class1 import Class1
from package.subclass import Subclass
...
Setting PYTHONPATH could be an option. Alternatively, given your current structure:
#subclass.py
from .class2 import Class2
class Subclass(Class2):
pass
#package/test.py
from .class1 import Class1
from .subclass import Subclass
#test.py
from package.class1 import Class1
from package.subclass import Subclass
To run test.py, simply run python test.py or python -m test
To run package/test.py, you will have to use python -m package.test
Try adding an empty __init__.py to the directory
The __init__.py tells the python interpreter that the directory it is dealing with is actually a module.
Hope this helps!
Aside from proper __init__.py, you'll also need to use:
from package.class1 import Class1
from package.subclass import Subclass
Note the added package.. Just a dot would also work, as that would be a relative import.

initializing yaml module in __init__.py

I'm using PyYAML, and would like to be able to use a string-concatenating constructor in my .yaml files.
This post shows how to add such constructors to PyYAML:
import yaml
## define custom tag handler
def join(loader, node):
seq = loader.construct_sequence(node)
return ''.join([str(i) for i in seq])
## register the tag handler
yaml.add_constructor('!join', join)
The above works when I enter it in a python terminal. However, I want to put the above in my_package's __init__.py file, so that I can do:
from my_package import yaml # my_package.__init__.py contains the above code
yaml.load("""
user_dir: &DIR /home/user
user_pics: !join [*DIR, /pics]
""")
However, this crashes with the message:
AttributeError Traceback (most recent call last)
<ipython-input-1-1107dafdb1d2> in <module>()
----> 1 import simplelearn.yaml
/home/mkg/projects/simplelearn/simplelearn/__init__.py in <module>()
----> 1 import yaml
2
3 def __join(loader, node):
4 '''
5 Concatenates a sequence of strings.
/home/mkg/projects/simplelearn/simplelearn/yaml.pyc in <module>()
AttributeError: 'module' object has no attribute 'add_constructor'
What's going on? Why can't python find yaml.add_constructor?
Your loading the module yaml from the file:
/home/mkg/projects/simplelearn/simplelearn/yaml.pyc
You either named one of your own files yaml.py, or you did have that file
before and renamed it, but forgot to remove the yaml.pyc. So you are not loading the PyYAML interpreter with import yaml.
The easiest way to verify is to include a temporary line after the import:
import yaml
print(yaml.__file__)
you should see something like .../site-packages/yaml/__init__.pyc.

Issue with importing my classes

Consider that I have 3 files.
I am trying to put different import function in one file and call that file i.e., File2 in this case directly.
File1.py
class generalTools():
def waitAppear():
wait()
File 11.py
class machinedata():
def max():
maxtime()
File2.py
import File1
import File11
self.generalTools = File1.generalTools()
self.machinedata=machinedata.max()
File3.py
from File2 import *
Note here I am not creating an object; just trying to call File1->waitAppear()
self.generalTools.waitAppear(self)
Whenever I execute above code, it throws error saying "generaTool has no instance"
What's wrong with the above code?

Categories