I am using python 3.6 version. I have 2 .py files as:
modbody.py
def test():
print("Statement goes here")
moduse.py
import modbody
test()
I am trying to execute moduse.py file in python interpreter using command "Python moduse.py" however, it gives me below error:
File "C:\users\Program\moduse.py", line 2, in
test() NameError: name 'test' is not defined
Please help guide me how to execute the script and call the test function here? I have also tried adding a blank init.py file to solve this but with no luck. Please not all of my files are in same directory only.
I think you meant:
import modbody
modbody.test()
Or:
from modbody import test
test()
Or:
from modbody import *
test()
Related
I am attempting to import a class named MainMenus from this file
B:\Programming\Python\RaceDash\src\UIModules\Menus.py
here is the code for the class
class MainMenus:
def StartUp():
#do stuff
def MainMenu():
#doing other stuff
I also have the _init_.py file in this path
B:\Programming\Python\RaceDash\src\UIModules\__init__.py
my main python file is here
B:\Programming\Python\RaceDash\src\Main.Py
and looks like this
from .UIModules.Menus import MainMenus
def Main():
MainMenus.StartUp()
while True:
MainMenus.MainMenu()
userSelect = input(": ")
Main()
pylance gives no errors when but when I attempt to run the program I get this error:
ile "b:\Programming\Python\RaceDash\src\Main.Py", line 1, in <module>
from .UIModules.Menus import MainMenus
ImportError: attempted relative import with no known parent package
When I remove the leading period pylance shows this error
Import "UIModules.Menus" could not be resolved
The application runs fine but I lose intellisense for any function from the other class.
What could be causing this issue?
You should move the package folder to a directory that is already in PATH
export PYTHONPATH="${PYTHONPATH}:B:\Programming\Python\RaceDash\src\
python3 Main.py
My issue is solved by going to the folder of my program and changing the extension. My file were not register as py file because I have created them in my VScode folder.
my_main_file.py:
from bfile import bfunc
def fna():
bfunc(sth)
if __name__ == "__main__":
fna()
bfile.py:
def bfunc(sth):
#bla bla..
Error:
name 'bfunc' is not defined
Both files are under same directory
PS:
I have tried everythig here Call a function from another file in Python
Add __init__.py python file to your current working directory.So python will treat directory as containing package.You can see documentation here
Try
import bfile
def fna():
bfile.bfunc(sth)
adding export PYTHONPATH="."
to bash_profile solved the issue
I have a very simple script that i wish to test out.
Master script (caller.py):
#!/usr/bin/env python2.7
import test2
test2.printMe()
Function/Module Script (test2.py):
def printMe():
print 'this actually works'
When I run caller.py, it works. Because the test2.py script exists in the same directory from which I'm calling caller.py.
What happens if i need to make the function(s) defined in test2.py available to the caller.py script, through a variable? There are times when the content of the test2.py function script wont always be in a file. Sometimes, it's inside a variable.
So basically, im looking for a way to access the functions that are in a variable just as import would access functions that are in a file.
I wish to be able to do something like this:
from commands import *
def run_command(cmd):
status, text = getstatusoutput(cmd)
print text
run_command('python /tmp/test2.py')
test2.printMe()
Please advise.
Try this:
from sys import path as sys_path
from os import path
def import_module(path_to_py_file):
dir_name, module_name = path.split(path.splitext(path_to_py_file)[0])
sys_path.append(dir_name)
return __import__(module_name)
Now you can do this:
test2 = import_module(r'/tmp/test2.py')
test2.printMe()
I am trying to import class called storePass() from test2 into test
I have done this->
test1->
import smtplib
from test2 import storePass
Gmail = storePass()
a = Gmail.returnPass()
test2->
class storePass():
Gmail_pass = "xcmsijw19021"
def returnPass(self):
return self.Gmail_pass
However I am getting the following error ->
TypeError: returnPass() takes 0 positional arguments but 1 was given
When I try to write the code as follows ->
class storePass():
Gmail_pass = "xcmsijw19021"
def returnPass(self):
return self.Gmail_pass
Gmail = storePass()
a = Gmail.returnPass()
I am getting no errors and I can execute print(a) without any problem.
So It's certainly something wrong with my import !
EDIT : Both test1 and test2 are in same directory !
I just tried to run your code and it works on my python 2.7.6 interpreter.
I print a and it gives me as a result xcmsijw19021.
This is the code I used:
file.py
import smtplib
from test2 import storePass
Gmail = storePass()
a = Gmail.returnPass()
print a
test2.py
class storePass():
Gmail_pass = "xcmsijw19021"
def returnPass(self):
return self.Gmail_pass
Then I did run python file.py and it prints me correctly the output.
I think it's some interpreter/ide problem maybe?
Python3 works aswell, by using print(a)
i tried and ran it on Ubuntu 16.04 python 2.7 and it worked as expected. make sure your two files are at the same folder or that the test2 path is in PYTHONPATH.
anyway to F.Leone python does not have a compiler and from what i know it does not depend on specific IDE.
I have two modules misc.py and main.py and would like to define all classes present in misc.py in main.py.
Below is the code
#misc.py
class dummy:
def __init__(self):
pass
def dummyPrint(self):
print "Welcome to python"
#main.py
import misc
dummyObj = dummy()
dummyObj.dummyPrint()
Is this the right way to go ? I do not see any output i.e., Welcome to python
$python misc_main.py misc.py
EDIT: I added the statement from misc import dummy and i am getting the following error
$python misc_main.py main.py
Traceback (most recent call last):
File "misc_main.py", line 5, in <module>
dummyObj = dummmy()
NameError: name 'dummmy' is not defined
When you do the following command, you are calling misc_main.py from the interpreter with misc.py as an argument.
python misc_main.py misc.py
Since misc_main is not reading command line arguments, this is equivalent to
python misc_main.py
I am surprised that you do not get errors, in either case. You need to import the actual class if you want to get output.
from misc import dummy
dummyObj = dummy()
dummyObj.dummyPrint()
Note, I am assuming your main file is actually in called misc_main.py rather than main.py as you have stated in your question. Otherwise you are not invoking the correct file.