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

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)

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()

Freshman AttributeError: No attribute in a module

just new to python, and I believe this is not a big deal but because I am a freshman.
Basically, this is a simple program plotting a FM signal in time domain. I write a module by myself.
def FreqMod (fc,fm,t_domain)
pi=py.pi
if fc>fm:
delta=fc-fm
else:
delta=fm-fc
return py.cos(2*pi*fc*t_domain+ (delta/fm)*py.sin(2*pi*fm*t_domain))
def AmpMod(fc,fm,t_domain):
pi=py.pi
return py.cos(2*pi*fc*t_domain)*py.cos(2*pi*fm*t_domain)
And import it in another program
import numpy as py
import mylib
import matplotlib.pyplot as plt
pi=py.pi
y=mylib.FreqMod(5,1000,t=py.arange(0,2*pi,pi/4000))
plt.plot(y)
The lib file is located as the same directory as the program. But I Got this later:
Traceback (most recent call last):
File "...(The directory)...", line 14, in <module>
y=mylib.FreqMod(5,1000,t=py.arange(0,2*pi,pi/4000))
AttributeError: module 'mylib' has no attribute 'FreqMod'
It seems like I didn't import the module successfully. I have compared it with examples in how to write and import a module, but yet can't figure out why. This really confuse me as a beginner in python.

Creating importable Python 3 package / module

I am having trouble creating an importable Python package/library/module or whatever the right nomenclature is. I am using Python 3.7
The file structure I am using is:
Python37//Lib//mypackage
mypackage
__init__.py
mypackage_.py
The code in __init__.py is:
from mypackage.mypackage_ import MyClass
The code in mypackage_.py is:
class MyClass:
def __init__(self, myarg = None):
self.myvar = myarg
And from my desktop I try running the following code:
import mypackage
x = MyClass(None)
But get the following error:
Traceback (most recent call last):
File "C:\Users\***\Desktop\importtest.py", line 3, in <module>
x = MyClass(None)
NameError: name 'MyClass' is not defined
You haven't imported the name MyClass into your current namespace. You've imported mypackage. To access anything within mypackage, you need to prefix the name with mypackage.<Name>
import mypackage
x = mypackage.MyClass(None)
As #rdas says, you need to prefix the name with mypackage.<Name>.
I don't recommend doing this, but you can wildcard import in order to make x = MyClass(None) work:
from mypackage import *
Now, everything from mypackage is imported and usable in the current namespace. However, you have to be careful with wildcard imports because they can create definition conflictions (if multiple modules have the same name for different things).

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

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)

Error when importing python module from folders

I have a following directory structure:
source
source_1.py
__init__.py
source1.py has class Source defined
source1.py
class Source(object):
pass
I am able to import using this
>>> from source.source1 import Source
>>> Source
<class 'source.source1.Source'>
However when trying to import using the below method it fails.
>>> from source import *
>>> source1.Source
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'source1' is not defined
Please let me know how can we use the 2nd import ?
For importing from a package (unlike importing from a module) you need to specify what * means. To do that, in __init__.py add a line like this:
__all__ = ["source1"]
See the Python documentation for Importing * From a Package.

Categories