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
Related
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()
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.
I have the following 4 python files within the folder SubFolder, assembled as such:
CodeFolder
SubFolder
GeneticAlgorithm.py
main.py
heuristic1.py
heuristic2.py
I am trying to import the file GeneticAlgorithm.py within my main.py file, using the import statement at the beginning of the class:
import GeneticAlgorithm
The problem: PyCharm is highlighting this and says "no module named GA".
Any clues as to what may be causing this issue and how to solve it? Thank you!
Correct me if I'm wrong but you might not have GA module in your GeneticAlgorithm.py
If you do, then you can do similar to below:
Similar to your folder structure:
From main.py call GeneticAlgorithm.py. For example:
from GeneticAlgorithm import GA
def main():
ga_obj = GA(mutation_rate=0.5)
print("Call GA module")
if __name__ == '__main__':
main()
If we look at the GeneticAlgorithm.py
class GA:
def __init__(self, mutation_rate):
self.mutation = mutation_rate
We have GA class.
This is a simple demonstration of how you can use.
I am developping a new little project which need to run on Windows and Linux. To explain my problem I will use 3 files.
parser/__init__.py
from .toto import Parser as TotoParser
parser/toto.py
class Variable(object):
def __str__(self):
return "totoVariable"
class Parser(object):
#staticmethod
def parse(data):
return Variable()
main.py
#!/usr/bin/env python3
from parser import TotoParser
def main():
print(TotoParser.parse(""))
if __name__ == '__main__':
main()
In this project. I create several modules(file) into different packages(directory). The thing is I need to change the name of module imported. To do that I use aliasing into __init__ files.
My project run perfectly on Lunix but when I tried it on Windows this problem occurs !
ImportError: cannot import name 'TotoParser'
Sorry for my English, I am learning it...
Please rename init.py to __init__.py, I believe that it is work, case already named as __init__.py ignore this anwser...
I have the following project structure:
and I would like to be able to run my project from the command line using something like:
hotel-reservation> python hotel_reservation
However, when doing this I am receiving the following error:
ImportError: No module named 'hotel_reservation'
The code that I have in __main__.py is something as:
from hotel_reservation.utils import get_client_type_and_dates
from hotel_reservation.hotel import Hotel
from hotel_reservation.hotel_reservation_system import HotelReservationSystem
def main():
# CODE
if __name__ == "__main__":
main()
I guess I am missing how to indicate python that hotel_reservation is a the parent directory, so how can I solve this?
Thanks in advance.