I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.
Related
I've checked other answers on SO as well, but they don't seem to be like mine.
So I'm playing around with imports, and I decided to put my module custom.py into a nested folder like this:
/Users/alex/Desktop/Learn/mods/onemore/custom.py
My main script file is in the following location:
/Users/alex/Desktop/Learn/main.py
So when I do import custom in main.py, I get the error ModuleNotFoundError: No module named 'custom'
This happens despite the fact that /Users/alex/Desktop/Learn/mods/onemore is in PYTHONPATH:
> echo $PYTHONPATH
> /Users/alex/Desktop/Learn/mods/onemore
and also the path is visible in sys.path (the second one):
> ['', '/Users/alex/Desktop/Learn/mods/onemore', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python310.zip', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages']
Why can't I still import the module?
You can import it with folder name
Ex: from onemore import custom
You need to add an empty __init__.py file at /Users/alex/Desktop/Learn/mods/onemore to call in this manner (as a regular package).
else if it's in the python path, call it as a namespace package (no need for init):
from custom import <func-you-need>
The standard for imports is currently from PEP420.
as the question, i have my directory map like this
the formatOutput contain some function for better print
now, i want use a function in module printChecked.py in package formatOutput from findInfoSystem.py
i have tried create __init__.py in all folder to treat python it is a package (the advice i get from previous answer other post) but it always failed.
case 1: from formatOutput.printChecked import print_checked_box
error is: ModuleNotFoundError: No module named 'formatOutput'
case 2: from dttn.formatOutput.printChecked import print_checked_box
error is ModuleNotFoundError: No module named 'dttn'
case 3: from ..formatOutput.printChecked import print_checked_box
error is ImportError: attempted relative import with no known parent package
i don't want to use the sys.path method because i think it is not the good way to solve problem.
Help me please !
There are two classes of solutions to this problem:
modify the python import path
use the various setup tools or manually install .egg-links or symbolic links
to "install" your development modules in the python/lib/site-packages
The first approach is simple, but the second involves delving deeper
into a number of related topics. You'd probably want a virtual environment,
and a setup.py, etc.
So for the quick and dirty, two ways to modify the python import path:
set the PYTHONPATH= environment variable to include the parent directory of your packages
in findInfoSystem before importing formatOutput.printChecked, add this:
import sys
sys.path.append("..")
i found something very weird. I'm not import direct in module file but import to init.py file of package and then import to module file from packages name and it work. it like that:
from __init__.py of systemInfo package:
import sys,os
from formatOutput import prettyAnnounce,prettyParseData
current_directory = os.getcwd()
# sys.path.insert(1, current_directory+"/formatOutput/")
# sys.path.insert(1,current_directory+"/")
then in findInfoSystem.py module, im imported again like this:
from systemInfo import current_directory, prettyAnnounce as prCh , prettyParseData as prPD
yeah, now it's work like a charm. :))) i even not sure how
I have the following directory structure, in Ubuntu:
/Test/Foo
/Test/Foo/foo.py
If I am in /Test, and I run python from the command line, followed by from Foo import foo, I get the following error: ImportError: No module named Foo.
But this is very confusing, since according to here, one of the directories used to search when importing is the directory from which the script was invoked. If I print out sys.path though, it does not include /Test, it just includes other standard Python directories.
Any idea what is going on?
If I'm getting you right, what you are trying to achieve here is the Foo to be a package and foo be a module.
Since Foo is not made into a package (You don't have __init__.py in the directory), it is not recognized as a package and thus not imported.
When you move into /Test/Foo, then you are simply importing the module foo, which will work.
What you possibly need to do here is to create an __init__.py file inside /Test/Foo and then import the module from the package.
Or you can try relative imports. Something like from .Foo import foo.
If you just need a function try this (python 2.7):
sys.path.insert() inserts the directory specified in the path python uses to find files.
commify.py is a file in subdirectory xyz that contains a function commify(value)
import os
import sys
sys.path.insert(0, os.getcwd() + r'\xyz')
from commify import commify
print commify(12345678)
output: 12,345,678
I got a strange issue and have no idea about the reason.
As you can see from the picture, folder fuzzier and parser are under the same parent folder, and both of them have the file __init__.py (both empty because I am not using from xxx import *, and code is based on Python 3.6).
And in another module (under the same parent folder with fuzzier and parser), there is a file doing some import like this:
import fuzzier.jison
import parser.annoying_char
The first line is good, but the second line is with an error ModuleNotFoundError: No module named 'parser.annoying_char'; 'parser' is not a package
I wasted hours on this and wish someone can help with this, Thanks!
parser is an in-built library in Python.
Python is trying to find annoying_char inside that library instead of your module.
You should use some other name.
Source - https://docs.python.org/2/library/parser.html
I have trouble importing package.
My file structure is like this:
filelib/
__init__.py
converters/
__init__.py
cmp2locus.py
modelmaker/
__init__.py
command_file.py
In module command_file.py I have a class named CommandFile which i want to call in the cmp2locus.py module.
I have tried the following in cmp2locus.py module:
import filelib.modelmaker.command_file
import modelmaker.command_file
from filelib.modelmaker.command_file import CommandFile
All these options return ImportError: No modules named ...
Appreciate any hint on solving this. I do not understand why this import does not work.
To perform these imports you have 3 options, I'll list them in the order I'd prefer. (For all of these options I will be assuming python 3)
Relative imports
Your file structure looks like a proper package file structure so this should work however anyone else trying this option should note that it requires you to be in a package; this won't work for some random script.
You'll also need to run the script doing the importing from outside the package, for example by importing it and running it from there rather than just running the cmp2locus.py script directly
Then you'll need to change your imports to be relative by using ..
So:
import filelib.modelmaker.command_file
becomes
from ..modelmaker import command_file
The .. refers to the parent folder (like the hidden file in file systems).
Also note you have to use the from import syntax because names starting with .. aren't valid identifiers in python. However you can of course import it as whatever you'd like using from import as.
See also the PEP
Absolute imports
If you place your package in site-packages (the directories returned by site.getsitepackages()) you will be able to use the format of imports that you were trying to use in the question. Note that this requires any users of your package to install it there too so this isn't ideal (although they probably would, relying on it is bad).
Modifying the python path
As Meera answered you can also directly modify the python path by using sys.
I dislike this option personally as it feels very 'hacky' but I've been told it can be useful as it gives you precise control of what you can import.
To import from another folder, you have to append that path of the folder to sys.path:
import sys
sys.path.append('path/filelib/modelmaker')
import command_file