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...
Related
I'm working on migrating an exsting Python 2.7 project to Python 3.9. I'm facing a directory structure-related issue in Python 3.
My current project directory structure is:
├───project
│ ├───core
| +--__init__.py
| +--main.py
| +--settings.py
│ ├───jobs
| +--job.py
main.py:
import settings
class Main:
def __init__(self, a=None, b=settings.B):
self.a = a
self.b = b
def start(self):
print(self.a, self.b)
job.py:
import sys
# sys.path.insert(0, '../core/')
from core.main import Main
from core import settings
main = Main(settings.A)
main.start()
There is no issues with this structure when Python 2.7 interpreter is used, but in Python 3.9 I see the following error when job.py is executed:
File "project\core\main.py", line 1, in <module>
import settings
ModuleNotFoundError: No module named 'settings'
The issue is fixable by uncommenting the code on line #2 of the job.py script, but I would like to avoid hardcoding folder values like that. I would appreciate if someone could provide an alternative approach and an explanation why it's behaving this way in the newer Python version.
Due to ambiguity absolute import was removed in python3 for such use case. (This explains it very well: Changes in import statement python3)
You can use relative import for this use case maybe - https://docs.python.org/3/reference/import.html#package-relative-imports
below the folder structure of my software:
below the code of all the .py files:
run.py:
import modules.module_01.aa as a
a.test()
# test:
if __name__=="__main__":
pass
aa.py (module 1):
import libraries.qq as q
import libraries.zz as z
def test():
q.qq_fun()
z.zz_fun()
print("ciao")
qq.py (library used by aa.py):
def qq_fun():
pass
zz.py (library used by aa.py):
def zz_fun():
pass
my question is really simple, why when I run "run.py" Python say to me:
why "aa.py" can't import the module "qq.py" and "zz.py"? how can I fix this issue?
run.py
In run.py, the Python interpreter thinks you're trying to import module_01.aa from a module named module. To import aa.py, you'll need to add this code to the top of your file, which adds the directory aa.py is in to the system path, and change your import statement to import aa as a.
import sys
sys.path.insert(0, "./modules/module_01/")
aa.py
The same problem occurs in aa.py. To fix the problem in this file, you'll need to add this code to the top of aa.py, which adds the directory qq.py and zz.py are in, and remove the libraries. from both of your import statements.
import sys
sys.path.insert(0, "./modules/module_01/libraries")
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 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.