Take a look at the Packages documentation (Section 6.4).
In short, you need to put a blank file named
__init__.py
in the lib directory.
Take a look at the Packages documentation (Section 6.4).
In short, you need to put a blank file named
__init__.py
in the lib directory.
- Create a subdirectory named
lib. - Create an empty file named
lib\__init__.py. In
lib\BoxTime.py, write a functionfoo()like this:def foo(): print "foo!"In your client code in the directory above
lib, write:from lib import BoxTime BoxTime.foo()Run your client code. You will get:
foo!
Much later -- in linux, it would look like this:
% cd ~/tmp
% mkdir lib
% touch lib/__init__.py
% cat > lib/BoxTime.py << EOF
heredoc> def foo():
heredoc> print "foo!"
heredoc> EOF
% tree lib
lib
├── BoxTime.py
└── __init__.py
0 directories, 2 files
% python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from lib import BoxTime
>>> BoxTime.foo()
foo!
Is there a cleaner way of importing modules from a different subfolder within a repository?
python - Import module from subfolder - Stack Overflow
Importing function from separate file in subfolder
import - Importing from subfolder in Python - Stack Overflow
How do I import a module from a subdirectory in Python?
Should I modify sys.path to import a subdirectory?
What does __init__.py do?
I currently have a project which is just a repository of various scripts for various purposes, all organized by folder structure. Unfortunately, with some of the custom modules I have written, Ive run into the problem of importing them properly, since they are nested within a different folder higher up the folder tree.
My current solution is hideous, which adds these lines at the top of every script:
import os, sys
print(os.path.dirname(sys.argv[0]))
root_folder = str(os.path.dirname(sys.argv[0])).split("REPONAME")[0] + "REPONAME"
sys.path.insert(0, root_folder)It crawls up the file tree to find the 'root folder' of my repository, and adds it to my path before my other imports so that it can be seen by the script when it runs.
A few suggestions Ive seen as I was searching for solutions was to set them up in the site-packages folder or to use pipenv install -e . inside of the folder of modules.
My concern with those solutions is the 'portability' of it, as this repository is being deployed across a department of less technical users who are trained on simply running the scripts. I need something simple that we can manage as the ones who create the script without adding an extra step of technical setup for people on the other end.
There's no need to mess with your PYTHONPATH or sys.path here.
To properly use absolute imports in a package you should include the "root" packagename as well, e.g.:
from dirFoo.dirFoo1.foo1 import Foo1
from dirFoo.dirFoo2.foo2 import Foo2
Or you can use relative imports:
from .dirfoo1.foo1 import Foo1
from .dirfoo2.foo2 import Foo2
Just to notify here. (from a newbee, keviv22)
Never and ever for the sake of your own good, name the folders or files with symbols like "-" or "_". If you did so, you may face few issues. like mine, say, though your command for importing is correct, you wont be able to successfully import the desired files which are available inside such named folders.
Invalid Folder namings as follows:
- Generic-Classes-Folder
- Generic_Classes_Folder
valid Folder namings for above:
- GenericClassesFolder or Genericclassesfolder or genericClassesFolder (or like this without any spaces or special symbols among the words)
What mistake I did:
consider the file structure.
Parent
. __init__.py
. Setup
.. __init__.py
.. Generic-Class-Folder
... __init__.py
... targetClass.py
. Check
.. __init__.py
.. testFile.py
What I wanted to do?
- from testFile.py, I wanted to import the 'targetClass.py' file inside the Generic-Class-Folder file to use the function named "functionExecute" in 'targetClass.py' file
What command I did?
- from 'testFile.py', wrote command,
from Core.Generic-Class-Folder.targetClass import functionExecute - Got errors like
SyntaxError: invalid syntax
Tried many searches and viewed many stackoverflow questions and unable to decide what went wrong. I cross checked my files multiple times, i used __init__.py file, inserted environment path and hugely worried what went wrong......
And after a long long long time, i figured this out while talking with a friend of mine. I am little stupid to use such naming conventions. I should never use space or special symbols to define a name for any folder or file. So, this is what I wanted to convey. Have a good day!
(sorry for the huge post over this... just letting my frustrations go.... :) Thanks!)
Note that Folder1 is a directory, the .py scripts are your modules.
In pyscript3 you should be able to go:
from Folder1 import pyscript1
Then you can access a method with name methodname like:
pyscript1.methodname()
Otherwise you can import the method directly like:
from Folder1.pyscript1 import methodname
and use it like:
methodname()
EDIT:
For your program to see Folder1 and Folder2, you need to run your program from the MainProject folder.
Either move pyscript3 to your MainFolder or write another script, let's call it main.py, and call the necessary code to instantiate the class/call the function you want in pyscript3.py.
To summarize, you always want to run the entry module from the base folder of your project.
If still not working, check the PYTHONPATH Environment Variable you defined for your project. Sometimes one defines a PYTHONPATH just for the root folder of the project but wants to import modules from a folder that is inside the PYTHONPATH that is defined.
Options to solve this:
- Import the module adding the parent folder name, for example:
import MainProject.Folder1.pyscript1instead ofimport Folder1.pyscript1. - Add another PYTHONPATH for the subfolder in which you have the modules to import.
I'm familiar with creating files and importing them, but I've gotten to a point where I want to keep my code in different folders in order to avoid clutter.
I have, however, been incapable of doing so.
The main folder is called chapters. It has a sub folder named code, and inside it is a file named info.py
I've tried using "from code import info" and similar phrases, to no avail. I've also tried importing sys and appending to path, which a google search said was the way to go. No success there, either. Finally, I tried another suggested solution, which was to create an empty filed named __init__.py , which didn't work either.
Any help would be greatly appreciated :D