Run python script from outer directory [duplicate] - python

How do I import files in Python? I want to import:
a file (e.g. file.py)
a folder
a file dynamically at runtime, based on user input
one specific part of a file (e.g. a single function)

There are many ways to import a python file, all with their pros and cons.
Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.
I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7
Example 1, Import a python module with python interpreter:
Put this in /home/el/foo/fox.py:
def what_does_the_fox_say():
print("vixens cry")
Get into the python interpreter:
el#apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>
You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.
Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:
Put this in /home/el/foo2/mylib.py:
def moobar():
print("hi")
Put this in /home/el/foo2/main.py:
execfile("/home/el/foo2/mylib.py")
moobar()
run the file:
el#apollo:/home/el/foo$ python main.py
hi
The function moobar was imported from mylib.py and made available in main.py
Example 3, Use from ... import ... functionality:
Put this in /home/el/foo3/chekov.py:
def question():
print "where are the nuclear wessels?"
Put this in /home/el/foo3/main.py:
from chekov import question
question()
Run it like this:
el#apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
If you defined other functions in chekov.py, they would not be available unless you import *
Example 4, Import riaa.py if it's in a different file location from where it is imported
Put this in /home/el/foo4/stuff/riaa.py:
def watchout():
print "computers are transforming into a noose and a yoke for humans"
Put this in /home/el/foo4/main.py:
import sys
import os
sys.path.append(os.path.abspath("/home/el/foo4/stuff"))
from riaa import *
watchout()
Run it:
el#apollo:/home/el/foo4$ python main.py
computers are transforming into a noose and a yoke for humans
That imports everything in the foreign file from a different directory.
Example 5, use os.system("python yourfile.py")
import os
os.system("python yourfile.py")
Example 6, import your file via piggybacking the python startuphook:
Update: This example used to work for both python2 and 3, but now only works for python2. python3 got rid of this user startuphook feature set because it was abused by low-skill python library writers, using it to impolitely inject their code into the global namespace, before all user-defined programs. If you want this to work for python3, you'll have to get more creative. If I tell you how to do it, python developers will disable that feature set as well, so you're on your own.
See: https://docs.python.org/2/library/user.html
Put this code into your home directory in ~/.pythonrc.py
class secretclass:
def secretmessage(cls, myarg):
return myarg + " is if.. up in the sky, the sky"
secretmessage = classmethod( secretmessage )
def skycake(cls):
return "cookie and sky pie people can't go up and "
skycake = classmethod( skycake )
Put this code into your main.py (can be anywhere):
import user
msg = "The only way skycake tates good"
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
Run it, you should get this:
$ python main.py
The only way skycake tates good is if.. up in the sky,
the skycookie and sky pie people can't go up and have the sky pie!
SKYCAKE!
If you get an error here: ModuleNotFoundError: No module named 'user' then it means you're using python3, startuphooks are disabled there by default.
Credit for this jist goes to: https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py Send along your up-boats.
Example 7, Most Robust: Import files in python with the bare import command:
Make a new directory /home/el/foo5/
Make a new directory /home/el/foo5/herp
Make an empty file named __init__.py under herp:
el#apollo:/home/el/foo5/herp$ touch __init__.py
el#apollo:/home/el/foo5/herp$ ls
__init__.py
Make a new directory /home/el/foo5/herp/derp
Under derp, make another __init__.py file:
el#apollo:/home/el/foo5/herp/derp$ touch __init__.py
el#apollo:/home/el/foo5/herp/derp$ ls
__init__.py
Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:
def skycake():
print "SkyCake evolves to stay just beyond the cognitive reach of " +
"the bulk of men. SKYCAKE!!"
The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;
from herp.derp.yolo import skycake
skycake()
Run it:
el#apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.
If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131

importlib was added to Python 3 to programmatically import a module.
import importlib
moduleName = input('Enter module name:')
importlib.import_module(moduleName)
The .py extension should be removed from moduleName. The function also defines a package argument for relative imports.
In python 2.x:
Just import file without the .py extension
A folder can be marked as a package, by adding an empty __init__.py file
You can use the __import__ function, which takes the module name (without extension) as a string extension
pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))
Type help(__import__) for more details.

First case
You want to import file A.py in file B.py, these two files are in the same folder, like this:
.
├── A.py
└── B.py
You can do this in file B.py:
import A
or
from A import *
or
from A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Second case
You want to import file folder/A.py in file B.py, these two files are not in the same folder, like this:
.
├── B.py
└── folder
└── A.py
You can do this in file B.py:
import folder.A
or
from folder.A import *
or
from folder.A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Summary
In the first case, file A.py is a module that you imports in file B.py, you used the syntax import module_name.
In the second case, folder is the package that contains the module A.py, you used the syntax import package_name.module_name.
For more info on packages and modules, consult this link.

To import a specific Python file at 'runtime' with a known name:
import os
import sys
...
scriptpath = "../Test/"
# Add the directory containing your module to the Python path (wants absolute paths)
sys.path.append(os.path.abspath(scriptpath))
# Do the import
import MyModule

You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type
from root.parent.folder.file import variable, class, whatever

Import doc .. -- Link for reference
The __init__.py files are required to make Python treat the directories as containing packages, this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
__init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable.
mydir/spam/__init__.py
mydir/spam/module.py
import spam.module
or
from spam import module

from file import function_name ######## Importing specific function
function_name() ######## Calling function
and
import file ######## Importing whole package
file.function1_name() ######## Calling function
file.function2_name() ######## Calling function
Here are the two simple ways I have understood by now and make sure your "file.py" file which you want to import as a library is present in your current directory only.

If the function defined is in a file x.py:
def greet():
print('Hello! How are you?')
In the file where you are importing the function, write this:
from x import greet
This is useful if you do not wish to import all the functions in a file.

I'd like to add this note I don't very clearly elsewhere; inside a module/package, when loading from files, the module/package name must be prefixed with the mymodule. Imagine mymodule being layout like this:
/main.py
/mymodule
/__init__.py
/somefile.py
/otherstuff.py
When loading somefile.py/otherstuff.py from __init__.py the contents should look like:
from mymodule.somefile import somefunc
from mymodule.otherstuff import otherfunc

the best way to import .py files is by way of __init__.py. the simplest thing to do, is to create an empty file named __init__.py in the same directory that your.py file is located.
this post by Mike Grouchy is a great explanation of __init__.py and its use for making, importing, and setting up python packages.

import sys
#print(sys.path)
sys.path.append('../input/tokenization')
import tokenization
To import any .py file, you can use above code.
First append the path and then import
Note:'../input/tokenization' directory contains tokenization.py file

Using Python 3.5 or later, you can use importlib.util to directly import a .py file in an arbitrary location as a module without needing to modify sys.path.
import importlib.util
import sys
def load_module(file_name, module_name)
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
The file_name parameter must be a string or a path-like object. The module_name parameter is required because all loaded Python modules must have a (dotted) module name (like sys, importlib, or importlib.util), but you can choose any available name you want for this new module.
You can use this function like this:
my_module = load_module("file.py", "mymod")
After it has been imported once into the Python process using the load_module() function, the module will be importable using the module name given to it.
file.py:
print(f"file.py was imported as {__name__}")
one.py:
print(f"one.py was imported as {__name__}")
load_module("file.py", "mymod")
import two
two.py:
print(f"two.py was imported as {__name__})")
import mymod
Given the files above, you can run the following command to see how file.py became importable.
$ python3 -m one
one.py was imported as __main__
two.py was imported as two
file.py was imported as mymod
This answer is based on the official Python documentation: importlib: Importing a source file directly.

How I import is import the file and use shorthand of it's name.
import DoStuff.py as DS
DS.main()
Don't forget that your importing file MUST BE named with .py extension

There are couple of ways of including your python script with name abc.py
e.g. if your file is called abc.py (import abc)
Limitation is that your file should be present in the same location where your calling python script is.
import abc
e.g. if your python file is inside the Windows folder. Windows folder is present at the same location where your calling python script is.
from folder import abc
Incase abc.py script is available insider internal_folder which is present inside folder
from folder.internal_folder import abc
As answered by James above, in case your file is at some fixed location
import os
import sys
scriptpath = "../Test/MyModule.py"
sys.path.append(os.path.abspath(scriptpath))
import MyModule
In case your python script gets updated and you don't want to upload - use these statements for auto refresh. Bonus :)
%load_ext autoreload
%autoreload 2

In case the module you want to import is not in a sub-directory, then try the following and run app.py from the deepest common parent directory:
Directory Structure:
/path/to/common_dir/module/file.py
/path/to/common_dir/application/app.py
/path/to/common_dir/application/subpath/config.json
In app.py, append path of client to sys.path:
import os, sys, inspect
sys.path.append(os.getcwd())
from module.file import MyClass
instance = MyClass()
Optional (If you load e.g. configs) (Inspect seems to be the most robust one for my use cases)
# Get dirname from inspect module
filename = inspect.getframeinfo(inspect.currentframe()).filename
dirname = os.path.dirname(os.path.abspath(filename))
MY_CONFIG = os.path.join(dirname, "subpath/config.json")
Run
user#host:/path/to/common_dir$ python3 application/app.py
This solution works for me in cli, as well as PyCharm.

This is how I did to call a function from a python file, that is flexible for me to call any functions.
import os, importlib, sys
def callfunc(myfile, myfunc, *args):
pathname, filename = os.path.split(myfile)
sys.path.append(os.path.abspath(pathname))
modname = os.path.splitext(filename)[0]
mymod = importlib.import_module(modname)
result = getattr(mymod, myfunc)(*args)
return result
result = callfunc("pathto/myfile.py", "myfunc", arg1, arg2)

Just to import python file in another python file
lets say I have helper.py python file which has a display function like,
def display():
print("I'm working sundar gsv")
Now in app.py, you can use the display function,
import helper
helper.display()
The output,
I'm working sundar gsv
NOTE: No need to specify the .py extension.

One very unknown feature of Python is the ability to import zip files:
library.zip
|-library
|--__init__.py
The file __init__.py of the package contains the following:
def dummy():
print 'Testing things out...'
We can write another script which can import a package from the zip archive. It is only necessary to add the zip file to the sys.path.
import sys
sys.path.append(r'library.zip')
import library
def run():
library.dummy()
run()

This helped me to structure my Python project with Visual Studio Code.
The problem could be caused when you don't declare __init__.py inside the directory. And the directory becomes implicit namespace package. Here is a nice summary about Python imports and project structure.
Also if you want to use the Visual Studio Code run button in the top bar with a script which is not inside the main package, you may try to run console from the actual directory.
For example, you want to execute an opened test_game_item.py from the tests package and you have Visual Studio Code opened in omission (main package) directory:
├── omission
│ ├── app.py
│ ├── common
│ │ ├── classproperty.py
│ │ ├── constants.py
│ │ ├── game_enums.py
│ │ └── __init__.py
│ ├── game
│ │ ├── content_loader.py
│ │ ├── game_item.py
│ │ ├── game_round.py
│ │ ├── __init__.py
│ │ └── timer.py
│ ├── __init__.py
│ ├── __main__.py
│ ├── resources
│ └── tests
│ ├── __init__.py
│ ├── test_game_item.py
│ ├── test_game_round_settings.py
│ ├── test_scoreboard.py
│ ├── test_settings.py
│ ├── test_test.py
│ └── test_timer.py
├── pylintrc
├── README.md
└── .gitignore
The directory structure is from [2].
You can try set this:
(Windows) Ctrl + Shift + P → Preferences: Open Settings (JSON).
Add this line to your user settings:
"python.terminal.executeInFileDir": true
A more comprehensive answer also for other systems is in this question.

This may sound crazy but you can just create a symbolic link to the file you want to import if you're just creating a wrapper script to it.

You can also do this: from filename import something
example: from client import Client
Note that you do not need the .py .pyw .pyui extension.

There are many ways, as listed above, but I find that I just want to import he contents of a file, and don't want to have to write lines and lines and have to import other modules. So, I came up with a way to get the contents of a file, even with the dot syntax (file.property) as opposed to merging the imported file with yours.
First of all, here is my file which I'll import, data.py
testString= "A string literal to import and test with"
Note: You could use the .txt extension instead.
In mainfile.py, start by opening and getting the contents.
#!usr/bin/env python3
Data=open('data.txt','r+').read()
Now you have the contents as a string, but trying to access data.testString will cause an error, as data is an instance of the str class, and even if it does have a property testString it will not do what you expected.
Next, create a class. For instance (pun intended), ImportedFile
class ImportedFile:
And put this into it (with the appropriate indentation):
exec(data)
And finally, re-assign data like so:
data=ImportedFile()
And that's it! Just access like you would for any-other module, typing print(data.testString) will print to the console A string literal to import and test with.
If, however, you want the equivalent of from mod import * just drop the class, instance assignment, and de-dent the exec.
Hope this helps:)
-Benji

from y import *
Say you have a file x and y.
You want to import y file to x.
then go to your x file and place the above command. To test this just put a print function in your y file and when your import was successful then in x file it should print it.

Related

Can I import my own custom modules without using os.chdir()? [duplicate]

I have this folder structure:
application
├── app
│   └── folder
│   └── file.py
└── app2
└── some_folder
└── some_file.py
How can I import a function from file.py, from within some_file.py? I tried:
from application.app.folder.file import func_name
but it doesn't work.
Note: This answer was intended for a very specific question. For most programmers coming here from a search engine, this is not the answer you are looking for. Typically you would structure your files into packages (see other answers) instead of modifying the search path.
By default, you can't. When importing a file, Python only searches the directory that the entry-point script is running from and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).
However, you can add to the Python path at runtime:
# some_file.py
import sys
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
Nothing wrong with:
from application.app.folder.file import func_name
Just make sure folder also contains an __init__.py, this allows it to be included as a package. Not sure why the other answers talk about PYTHONPATH.
When modules are in parallel locations, as in the question:
application/app2/some_folder/some_file.py
application/app2/another_folder/another_file.py
This shorthand makes one module visible to the other:
import sys
sys.path.append('../')
First import sys in name-file.py
import sys
Second append the folder path in name-file.py
sys.path.insert(0, '/the/folder/path/name-package/')
Third Make a blank file called __ init __.py in your subdirectory (this tells Python it is a package)
name-file.py
name-package
__ init __.py
name-module.py
Fourth import the module inside the folder in name-file.py
from name-package import name-module
I think an ad-hoc way would be to use the environment variable PYTHONPATH as described in the documentation: Python2, Python3
# Linux & OSX
export PYTHONPATH=$HOME/dirWithScripts/:$PYTHONPATH
# Windows
set PYTHONPATH=C:\path\to\dirWithScripts\;%PYTHONPATH%
Your problem is that Python is looking in the Python directory for this file and not finding it. You must specify that you are talking about the directory that you are in and not the Python one.
To do this you change this:
from application.app.folder.file import func_name
to this:
from .application.app.folder.file import func_name
By adding the dot you are saying look in this folder for the application folder instead of looking in the Python directory.
Try Python's relative imports:
from ...app.folder.file import func_name
Every leading dot is another higher level in the hierarchy beginning with the current directory.
Problems? If this isn't working for you then you probably are getting bit by the many gotcha's relative imports has.
Read answers and comments for more details:
How to fix "Attempted relative import in non-package" even with __init__.py
Hint: have __init__.py at every directory level. You might need python -m application.app2.some_folder.some_file (leaving off .py) which you run from the top level directory or have that top level directory in your PYTHONPATH. Phew!
The answers here are lacking in clarity, this is tested on Python 3.6
With this folder structure:
main.py
|
---- myfolder/myfile.py
Where myfile.py has the content:
def myfunc():
print('hello')
The import statement in main.py is:
from myfolder.myfile import myfunc
myfunc()
and this will print hello.
In Python 3.4 and later, you can import from a source file directly (link to documentation). This is not the simplest solution, but I'm including this answer for completeness.
Here is an example. First, the file to be imported, named foo.py:
def announce():
print("Imported!")
The code that imports the file above, inspired heavily by the example in the documentation:
import importlib.util
def module_from_file(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
foo = module_from_file("foo", "/path/to/foo.py")
if __name__ == "__main__":
print(foo)
print(dir(foo))
foo.announce()
The output:
<module 'foo' from '/path/to/foo.py'>
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'announce']
Imported!
Note that the variable name, the module name, and the filename need not match. This code still works:
import importlib.util
def module_from_file(module_name, file_path):
spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
baz = module_from_file("bar", "/path/to/foo.py")
if __name__ == "__main__":
print(baz)
print(dir(baz))
baz.announce()
The output:
<module 'bar' from '/path/to/foo.py'>
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'announce']
Imported!
Programmatically importing modules was introduced in Python 3.1 and gives you more control over how modules are imported. Refer to the documentation for more information.
From what I know, add an __init__.py file directly in the folder of the functions you want to import will do the job.
Using sys.path.append with an absolute path is not ideal when moving the application to other environments. Using a relative path won't always work because the current working directory depends on how the script was invoked.
Since the application folder structure is fixed, we can use os.path to get the full path of the module we wish to import. For example, if this is the structure:
/home/me/application/app2/some_folder/vanilla.py
/home/me/application/app2/another_folder/mango.py
And let's say that you want to import the mango module. You could do the following in vanilla.py:
import sys, os.path
mango_dir = (os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
+ '/another_folder/')
sys.path.append(mango_dir)
import mango
Of course, you don't need the mango_dir variable.
To understand how this works look at this interactive session example:
>>> import os
>>> mydir = '/home/me/application/app2/some_folder'
>>> newdir = os.path.abspath(os.path.join(mydir, '..'))
>>> newdir
'/home/me/application/app2'
>>> newdir = os.path.abspath(os.path.join(mydir, '..')) + '/another_folder'
>>>
>>> newdir
'/home/me/application/app2/another_folder'
>>>
And check the os.path documentation.
Also worth noting that dealing with multiple folders is made easier when using packages, as one can use dotted module names.
I was faced with the same challenge, especially when importing multiple files, this is how I managed to overcome it.
import os, sys
from os.path import dirname, join, abspath
sys.path.insert(0, abspath(join(dirname(__file__), '..')))
from root_folder import file_name
Considering application as the root directory for your python project, create an empty __init__.py file in application, app and folder folders. Then in your some_file.py make changes as follows to get the definition of func_name:
import sys
sys.path.insert(0, r'/from/root/directory/application')
from application.app.folder.file import func_name ## You can also use '*' wildcard to import all the functions in file.py file.
func_name()
Worked for me in python3 on linux
import sys
sys.path.append(pathToFolderContainingScripts)
from scriptName import functionName #scriptName without .py extension
The best practice for creating a package can be running and accessing the other modules from a module like main_module.py at highest level directory.
This structure demonstrates you can use and access sub package, parent package, or same level packages and modules by using a top level directory file main_module.py.
Create and run these files and folders for testing:
package/
|
|----- __init__.py (Empty file)
|------- main_module.py (Contains: import subpackage_1.module_1)
|------- module_0.py (Contains: print('module_0 at parent directory, is imported'))
|
|
|------- subpackage_1/
| |
| |----- __init__.py (Empty file)
| |----- module_1.py (Contains: print('importing other modules from module_1...')
| | import module_0
| | import subpackage_2.module_2
| | import subpackage_1.sub_subpackage_3.module_3)
| |----- photo.png
| |
| |
| |----- sub_subpackage_3/
| |
| |----- __init__.py (Empty file)
| |----- module_3.py (Contains: print('module_3 at sub directory, is imported'))
|
|------- subpackage_2/
| |
| |----- __init__.py (Empty file)
| |----- module_2.py (Contains: print('module_2 at same level directory, is imported'))
Now run main_module.py
the output is
>>>'importing other modules from module_1...'
'module_0 at parent directory, is imported'
'module_2 at same level directory, is imported'
'module_3 at sub directory, is imported'
Opening pictures and files note:
In a package structure if you want to access a photo, use absolute directory from highest level directory.
let's Suppose you are running main_module.py and you want to open photo.png inside module_1.py.
what module_1.py must contain is:
Correct:
image_path = 'subpackage_1/photo.png'
cv2.imread(image_path)
Wrong:
image_path = 'photo.png'
cv2.imread(image_path)
although module_1.py and photo.png are at same directory.
├───root
│ ├───dir_a
│ │ ├───file_a.py
│ │ └───file_xx.py
│ ├───dir_b
│ │ ├───file_b.py
│ │ └───file_yy.py
│ ├───dir_c
│ └───dir_n
You can add the parent directory to PYTHONPATH, in order to achieve that, you can use OS depending path in the "module search path" which is listed in sys.path. So you can easily add the parent directory like following:
# file_b.py
import sys
sys.path.insert(0, '..')
from dir_a.file_a import func_name
This works for me on windows
# some_file.py on mainApp/app2
import sys
sys.path.insert(0, sys.path[0]+'\\app2')
import some_file
In my case I had a class to import. My file looked like this:
# /opt/path/to/code/log_helper.py
class LogHelper:
# stuff here
In my main file I included the code via:
import sys
sys.path.append("/opt/path/to/code/")
from log_helper import LogHelper
I bumped into the same question several times, so I would like to share my solution.
Python Version: 3.X
The following solution is for someone who develops your application in Python version 3.X because Python 2 is not supported since Jan/1/2020.
Project Structure
In python 3, you don't need __init__.py in your project subdirectory due to the Implicit Namespace Packages. See Is init.py not required for packages in Python 3.3+
Project
├── main.py
├── .gitignore
|
├── a
| └── file_a.py
|
└── b
└── file_b.py
Problem Statement
In file_b.py, I would like to import a class A in file_a.py under the folder a.
Solutions
#1 A quick but dirty way
Without installing the package like you are currently developing a new project
Using the try catch to check if the errors. Code example:
import sys
try:
# The insertion index should be 1 because index 0 is this file
sys.path.insert(1, '/absolute/path/to/folder/a') # the type of path is string
# because the system path already have the absolute path to folder a
# so it can recognize file_a.py while searching
from file_a import A
except (ModuleNotFoundError, ImportError) as e:
print("{} fileure".format(type(e)))
else:
print("Import succeeded")
#2 Install your package
Once you installed your application (in this post, the tutorial of installation is not included)
You can simply
try:
from __future__ import absolute_import
# now it can reach class A of file_a.py in folder a
# by relative import
from ..a.file_a import A
except (ModuleNotFoundError, ImportError) as e:
print("{} fileure".format(type(e)))
else:
print("Import succeeded")
Happy coding!
I'm quite special : I use Python with Windows !
I just complete information : for both Windows and Linux, both relative and absolute path work into sys.path (I need relative paths because I use my scripts on the several PCs and under different main directories).
And when using Windows both \ and / can be used as separator for file names and of course you must double \ into Python strings,
some valid examples :
sys.path.append('c:\\tools\\mydir')
sys.path.append('..\\mytools')
sys.path.append('c:/tools/mydir')
sys.path.append('../mytools')
(note : I think that / is more convenient than \, event if it is less 'Windows-native' because it is Linux-compatible and simpler to write and copy to Windows explorer)
If the purpose of loading a module from a specific path is to assist you during the development of a custom module, you can create a symbolic link in the same folder of the test script that points to the root of the custom module. This module reference will take precedence over any other modules installed of the same name for any script run in that folder.
I tested this on Linux but it should work in any modern OS that supports symbolic links.
One advantage to this approach is that you can you can point to a module that's sitting in your own local SVC branch working copy which can greatly simplify the development cycle time and reduce failure modes of managing different versions of the module.
I was working on project a that I wanted users to install via pip install a with the following file list:
.
├── setup.py
├── MANIFEST.in
└── a
├── __init__.py
├── a.py
└── b
├── __init__.py
└── b.py
setup.py
from setuptools import setup
setup (
name='a',
version='0.0.1',
packages=['a'],
package_data={
'a': ['b/*'],
},
)
MANIFEST.in
recursive-include b *.*
a/init.py
from __future__ import absolute_import
from a.a import cats
import a.b
a/a.py
cats = 0
a/b/init.py
from __future__ import absolute_import
from a.b.b import dogs
a/b/b.py
dogs = 1
I installed the module by running the following from the directory with MANIFEST.in:
python setup.py install
Then, from a totally different location on my filesystem /moustache/armwrestle I was able to run:
import a
dir(a)
Which confirmed that a.cats indeed equalled 0 and a.b.dogs indeed equalled 1, as intended.
Instead of just doing an import ..., do this :
from <MySubFolder> import <MyFile>
MyFile is inside the MySubFolder.
In case anyone still looking for a solution. This worked for me.
Python adds the folder containing the script you launch to the PYTHONPATH, so if you run
python application/app2/some_folder/some_file.py
Only the folder application/app2/some_folder is added to the path (not the base dir that you're executing the command in). Instead, run your file as a module and add a __init__.py in your some_folder directory.
python -m application.app2.some_folder.some_file
This will add the base dir to the python path, and then classes will be accessible via a non-relative import.
The code below imports the Python script given by it's path, no matter where it is located, in a Python version-safe way:
def import_module_by_path(path):
name = os.path.splitext(os.path.basename(path))[0]
if sys.version_info[0] == 2:
# Python 2
import imp
return imp.load_source(name, path)
elif sys.version_info[:2] <= (3, 4):
# Python 3, version <= 3.4
from importlib.machinery import SourceFileLoader
return SourceFileLoader(name, path).load_module()
else:
# Python 3, after 3.4
import importlib.util
spec = importlib.util.spec_from_file_location(name, path)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
I found this in the codebase of psutils, at line 1042 in psutils.test.__init__.py (most recent commit as of 09.10.2020).
Usage example:
script = "/home/username/Documents/some_script.py"
some_module = import_module_by_path(script)
print(some_module.foo())
Important caveat: The module will be treated as top-level; any relative imports from parent packages in it will fail.
Wow, I did not expect to spend so much time on this. The following worked for me:
OS: Windows 10
Python: v3.10.0
Note: Since I am Python v3.10.0, I am not using __init__.py files, which did not work for me anyway.
application
├── app
│ └── folder
│ └── file.py
└── app2
└── some_folder
└── some_file.py
WY Hsu's 1st solution worked for me. I have reposted it with an absolute file reference for clarity:
import sys
sys.path.insert(1, 'C:\\Users\\<Your Username>\\application')
import app2.some_folder.some_file
some_file.hello_world()
Alternative Solution: However, this also worked for me:
import sys
sys.path.append( '.' )
import app2.some_folder.some_file
some_file.hello_world()
Although, I do not understand why it works. I thought the dot is a reference to the current directory. However, when printing out the paths to the current folder, the current directory is already listed at the top:
for path in sys.path:
print(path)
Hopefully, someone can provide clarity as to why this works in the comments. Nevertheless, I also hope it helps someone.
This problem may be due Pycharm
I had the same problem while using Pycharm. I had this project structure
skylake\
backend\
apps\
example.py
configuration\
settings.py
frontend\
...some_stuff
and code from configuration import settings in example.py raised import error
the problem was that when I opened Pycharm, it considered that skylake is root path and ran this code
sys.path.extend(['D:\\projects\\skylake', 'D:/projects/skylake'])
To fix this I just marked backend directory as source root
And it's fixed my problem
My solution for people. who have all the necessary __init__.py in the package, but import still doesn't work.
import sys
import os
sys.path.insert(0, os.getcwd())
import application.app.folder.file as file
You can use importlib to import modules where you want to import a module from a folder using a string like so:
import importlib
scriptName = 'Snake'
script = importlib.import_module('Scripts\\.%s' % scriptName)
This example has a main.py which is the above code then a folder called Scripts and then you can call whatever you need from this folder by changing the scriptName variable. You can then use script to reference to this module. such as if I have a function called Hello() in the Snake module you can run this function by doing so:
script.Hello()
I have tested this in Python 3.6
I usually create a symlink to the module I want to import. The symlink makes sure Python interpreter can locate the module inside the current directory (the script you are importing the other module into); later on when your work is over, you can remove the symlink. Also, you should ignore symlinks in .gitignore, so that, you wouldn't accidentally commit symlinked modules to your repo. This approach lets you even successfully work with modules that are located parallel to the script you are executing.
ln -s ~/path/to/original/module/my_module ~/symlink/inside/the/destination/directory/my_module

Import a list of modules in a file in Python [duplicate]

How do I import files in Python? I want to import:
a file (e.g. file.py)
a folder
a file dynamically at runtime, based on user input
one specific part of a file (e.g. a single function)
There are many ways to import a python file, all with their pros and cons.
Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.
I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7
Example 1, Import a python module with python interpreter:
Put this in /home/el/foo/fox.py:
def what_does_the_fox_say():
print("vixens cry")
Get into the python interpreter:
el#apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>
You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.
Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:
Put this in /home/el/foo2/mylib.py:
def moobar():
print("hi")
Put this in /home/el/foo2/main.py:
execfile("/home/el/foo2/mylib.py")
moobar()
run the file:
el#apollo:/home/el/foo$ python main.py
hi
The function moobar was imported from mylib.py and made available in main.py
Example 3, Use from ... import ... functionality:
Put this in /home/el/foo3/chekov.py:
def question():
print "where are the nuclear wessels?"
Put this in /home/el/foo3/main.py:
from chekov import question
question()
Run it like this:
el#apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
If you defined other functions in chekov.py, they would not be available unless you import *
Example 4, Import riaa.py if it's in a different file location from where it is imported
Put this in /home/el/foo4/stuff/riaa.py:
def watchout():
print "computers are transforming into a noose and a yoke for humans"
Put this in /home/el/foo4/main.py:
import sys
import os
sys.path.append(os.path.abspath("/home/el/foo4/stuff"))
from riaa import *
watchout()
Run it:
el#apollo:/home/el/foo4$ python main.py
computers are transforming into a noose and a yoke for humans
That imports everything in the foreign file from a different directory.
Example 5, use os.system("python yourfile.py")
import os
os.system("python yourfile.py")
Example 6, import your file via piggybacking the python startuphook:
Update: This example used to work for both python2 and 3, but now only works for python2. python3 got rid of this user startuphook feature set because it was abused by low-skill python library writers, using it to impolitely inject their code into the global namespace, before all user-defined programs. If you want this to work for python3, you'll have to get more creative. If I tell you how to do it, python developers will disable that feature set as well, so you're on your own.
See: https://docs.python.org/2/library/user.html
Put this code into your home directory in ~/.pythonrc.py
class secretclass:
def secretmessage(cls, myarg):
return myarg + " is if.. up in the sky, the sky"
secretmessage = classmethod( secretmessage )
def skycake(cls):
return "cookie and sky pie people can't go up and "
skycake = classmethod( skycake )
Put this code into your main.py (can be anywhere):
import user
msg = "The only way skycake tates good"
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
Run it, you should get this:
$ python main.py
The only way skycake tates good is if.. up in the sky,
the skycookie and sky pie people can't go up and have the sky pie!
SKYCAKE!
If you get an error here: ModuleNotFoundError: No module named 'user' then it means you're using python3, startuphooks are disabled there by default.
Credit for this jist goes to: https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py Send along your up-boats.
Example 7, Most Robust: Import files in python with the bare import command:
Make a new directory /home/el/foo5/
Make a new directory /home/el/foo5/herp
Make an empty file named __init__.py under herp:
el#apollo:/home/el/foo5/herp$ touch __init__.py
el#apollo:/home/el/foo5/herp$ ls
__init__.py
Make a new directory /home/el/foo5/herp/derp
Under derp, make another __init__.py file:
el#apollo:/home/el/foo5/herp/derp$ touch __init__.py
el#apollo:/home/el/foo5/herp/derp$ ls
__init__.py
Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:
def skycake():
print "SkyCake evolves to stay just beyond the cognitive reach of " +
"the bulk of men. SKYCAKE!!"
The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;
from herp.derp.yolo import skycake
skycake()
Run it:
el#apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.
If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131
importlib was added to Python 3 to programmatically import a module.
import importlib
moduleName = input('Enter module name:')
importlib.import_module(moduleName)
The .py extension should be removed from moduleName. The function also defines a package argument for relative imports.
In python 2.x:
Just import file without the .py extension
A folder can be marked as a package, by adding an empty __init__.py file
You can use the __import__ function, which takes the module name (without extension) as a string extension
pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))
Type help(__import__) for more details.
First case
You want to import file A.py in file B.py, these two files are in the same folder, like this:
.
├── A.py
└── B.py
You can do this in file B.py:
import A
or
from A import *
or
from A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Second case
You want to import file folder/A.py in file B.py, these two files are not in the same folder, like this:
.
├── B.py
└── folder
└── A.py
You can do this in file B.py:
import folder.A
or
from folder.A import *
or
from folder.A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Summary
In the first case, file A.py is a module that you imports in file B.py, you used the syntax import module_name.
In the second case, folder is the package that contains the module A.py, you used the syntax import package_name.module_name.
For more info on packages and modules, consult this link.
To import a specific Python file at 'runtime' with a known name:
import os
import sys
...
scriptpath = "../Test/"
# Add the directory containing your module to the Python path (wants absolute paths)
sys.path.append(os.path.abspath(scriptpath))
# Do the import
import MyModule
You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type
from root.parent.folder.file import variable, class, whatever
Import doc .. -- Link for reference
The __init__.py files are required to make Python treat the directories as containing packages, this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
__init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable.
mydir/spam/__init__.py
mydir/spam/module.py
import spam.module
or
from spam import module
from file import function_name ######## Importing specific function
function_name() ######## Calling function
and
import file ######## Importing whole package
file.function1_name() ######## Calling function
file.function2_name() ######## Calling function
Here are the two simple ways I have understood by now and make sure your "file.py" file which you want to import as a library is present in your current directory only.
If the function defined is in a file x.py:
def greet():
print('Hello! How are you?')
In the file where you are importing the function, write this:
from x import greet
This is useful if you do not wish to import all the functions in a file.
I'd like to add this note I don't very clearly elsewhere; inside a module/package, when loading from files, the module/package name must be prefixed with the mymodule. Imagine mymodule being layout like this:
/main.py
/mymodule
/__init__.py
/somefile.py
/otherstuff.py
When loading somefile.py/otherstuff.py from __init__.py the contents should look like:
from mymodule.somefile import somefunc
from mymodule.otherstuff import otherfunc
Using Python 3.5 or later, you can use importlib.util to directly import a .py file in an arbitrary location as a module without needing to modify sys.path.
import importlib.util
import sys
def load_module(file_name, module_name)
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
The file_name parameter must be a string or a path-like object. The module_name parameter is required because all loaded Python modules must have a (dotted) module name (like sys, importlib, or importlib.util), but you can choose any available name you want for this new module.
You can use this function like this:
my_module = load_module("file.py", "mymod")
After it has been imported once into the Python process using the load_module() function, the module will be importable using the module name given to it.
file.py:
print(f"file.py was imported as {__name__}")
one.py:
print(f"one.py was imported as {__name__}")
load_module("file.py", "mymod")
import two
two.py:
print(f"two.py was imported as {__name__})")
import mymod
Given the files above, you can run the following command to see how file.py became importable.
$ python3 -m one
one.py was imported as __main__
two.py was imported as two
file.py was imported as mymod
This answer is based on the official Python documentation: importlib: Importing a source file directly.
the best way to import .py files is by way of __init__.py. the simplest thing to do, is to create an empty file named __init__.py in the same directory that your.py file is located.
this post by Mike Grouchy is a great explanation of __init__.py and its use for making, importing, and setting up python packages.
import sys
#print(sys.path)
sys.path.append('../input/tokenization')
import tokenization
To import any .py file, you can use above code.
First append the path and then import
Note:'../input/tokenization' directory contains tokenization.py file
How I import is import the file and use shorthand of it's name.
import DoStuff.py as DS
DS.main()
Don't forget that your importing file MUST BE named with .py extension
There are couple of ways of including your python script with name abc.py
e.g. if your file is called abc.py (import abc)
Limitation is that your file should be present in the same location where your calling python script is.
import abc
e.g. if your python file is inside the Windows folder. Windows folder is present at the same location where your calling python script is.
from folder import abc
Incase abc.py script is available insider internal_folder which is present inside folder
from folder.internal_folder import abc
As answered by James above, in case your file is at some fixed location
import os
import sys
scriptpath = "../Test/MyModule.py"
sys.path.append(os.path.abspath(scriptpath))
import MyModule
In case your python script gets updated and you don't want to upload - use these statements for auto refresh. Bonus :)
%load_ext autoreload
%autoreload 2
In case the module you want to import is not in a sub-directory, then try the following and run app.py from the deepest common parent directory:
Directory Structure:
/path/to/common_dir/module/file.py
/path/to/common_dir/application/app.py
/path/to/common_dir/application/subpath/config.json
In app.py, append path of client to sys.path:
import os, sys, inspect
sys.path.append(os.getcwd())
from module.file import MyClass
instance = MyClass()
Optional (If you load e.g. configs) (Inspect seems to be the most robust one for my use cases)
# Get dirname from inspect module
filename = inspect.getframeinfo(inspect.currentframe()).filename
dirname = os.path.dirname(os.path.abspath(filename))
MY_CONFIG = os.path.join(dirname, "subpath/config.json")
Run
user#host:/path/to/common_dir$ python3 application/app.py
This solution works for me in cli, as well as PyCharm.
This is how I did to call a function from a python file, that is flexible for me to call any functions.
import os, importlib, sys
def callfunc(myfile, myfunc, *args):
pathname, filename = os.path.split(myfile)
sys.path.append(os.path.abspath(pathname))
modname = os.path.splitext(filename)[0]
mymod = importlib.import_module(modname)
result = getattr(mymod, myfunc)(*args)
return result
result = callfunc("pathto/myfile.py", "myfunc", arg1, arg2)
Just to import python file in another python file
lets say I have helper.py python file which has a display function like,
def display():
print("I'm working sundar gsv")
Now in app.py, you can use the display function,
import helper
helper.display()
The output,
I'm working sundar gsv
NOTE: No need to specify the .py extension.
One very unknown feature of Python is the ability to import zip files:
library.zip
|-library
|--__init__.py
The file __init__.py of the package contains the following:
def dummy():
print 'Testing things out...'
We can write another script which can import a package from the zip archive. It is only necessary to add the zip file to the sys.path.
import sys
sys.path.append(r'library.zip')
import library
def run():
library.dummy()
run()
This helped me to structure my Python project with Visual Studio Code.
The problem could be caused when you don't declare __init__.py inside the directory. And the directory becomes implicit namespace package. Here is a nice summary about Python imports and project structure.
Also if you want to use the Visual Studio Code run button in the top bar with a script which is not inside the main package, you may try to run console from the actual directory.
For example, you want to execute an opened test_game_item.py from the tests package and you have Visual Studio Code opened in omission (main package) directory:
├── omission
│ ├── app.py
│ ├── common
│ │ ├── classproperty.py
│ │ ├── constants.py
│ │ ├── game_enums.py
│ │ └── __init__.py
│ ├── game
│ │ ├── content_loader.py
│ │ ├── game_item.py
│ │ ├── game_round.py
│ │ ├── __init__.py
│ │ └── timer.py
│ ├── __init__.py
│ ├── __main__.py
│ ├── resources
│ └── tests
│ ├── __init__.py
│ ├── test_game_item.py
│ ├── test_game_round_settings.py
│ ├── test_scoreboard.py
│ ├── test_settings.py
│ ├── test_test.py
│ └── test_timer.py
├── pylintrc
├── README.md
└── .gitignore
The directory structure is from [2].
You can try set this:
(Windows) Ctrl + Shift + P → Preferences: Open Settings (JSON).
Add this line to your user settings:
"python.terminal.executeInFileDir": true
A more comprehensive answer also for other systems is in this question.
This may sound crazy but you can just create a symbolic link to the file you want to import if you're just creating a wrapper script to it.
You can also do this: from filename import something
example: from client import Client
Note that you do not need the .py .pyw .pyui extension.
There are many ways, as listed above, but I find that I just want to import he contents of a file, and don't want to have to write lines and lines and have to import other modules. So, I came up with a way to get the contents of a file, even with the dot syntax (file.property) as opposed to merging the imported file with yours.
First of all, here is my file which I'll import, data.py
testString= "A string literal to import and test with"
Note: You could use the .txt extension instead.
In mainfile.py, start by opening and getting the contents.
#!usr/bin/env python3
Data=open('data.txt','r+').read()
Now you have the contents as a string, but trying to access data.testString will cause an error, as data is an instance of the str class, and even if it does have a property testString it will not do what you expected.
Next, create a class. For instance (pun intended), ImportedFile
class ImportedFile:
And put this into it (with the appropriate indentation):
exec(data)
And finally, re-assign data like so:
data=ImportedFile()
And that's it! Just access like you would for any-other module, typing print(data.testString) will print to the console A string literal to import and test with.
If, however, you want the equivalent of from mod import * just drop the class, instance assignment, and de-dent the exec.
Hope this helps:)
-Benji
from y import *
Say you have a file x and y.
You want to import y file to x.
then go to your x file and place the above command. To test this just put a print function in your y file and when your import was successful then in x file it should print it.

python Call Tkinter Function from different python script [duplicate]

How do I import files in Python? I want to import:
a file (e.g. file.py)
a folder
a file dynamically at runtime, based on user input
one specific part of a file (e.g. a single function)
There are many ways to import a python file, all with their pros and cons.
Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.
I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7
Example 1, Import a python module with python interpreter:
Put this in /home/el/foo/fox.py:
def what_does_the_fox_say():
print("vixens cry")
Get into the python interpreter:
el#apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>
You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.
Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:
Put this in /home/el/foo2/mylib.py:
def moobar():
print("hi")
Put this in /home/el/foo2/main.py:
execfile("/home/el/foo2/mylib.py")
moobar()
run the file:
el#apollo:/home/el/foo$ python main.py
hi
The function moobar was imported from mylib.py and made available in main.py
Example 3, Use from ... import ... functionality:
Put this in /home/el/foo3/chekov.py:
def question():
print "where are the nuclear wessels?"
Put this in /home/el/foo3/main.py:
from chekov import question
question()
Run it like this:
el#apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
If you defined other functions in chekov.py, they would not be available unless you import *
Example 4, Import riaa.py if it's in a different file location from where it is imported
Put this in /home/el/foo4/stuff/riaa.py:
def watchout():
print "computers are transforming into a noose and a yoke for humans"
Put this in /home/el/foo4/main.py:
import sys
import os
sys.path.append(os.path.abspath("/home/el/foo4/stuff"))
from riaa import *
watchout()
Run it:
el#apollo:/home/el/foo4$ python main.py
computers are transforming into a noose and a yoke for humans
That imports everything in the foreign file from a different directory.
Example 5, use os.system("python yourfile.py")
import os
os.system("python yourfile.py")
Example 6, import your file via piggybacking the python startuphook:
Update: This example used to work for both python2 and 3, but now only works for python2. python3 got rid of this user startuphook feature set because it was abused by low-skill python library writers, using it to impolitely inject their code into the global namespace, before all user-defined programs. If you want this to work for python3, you'll have to get more creative. If I tell you how to do it, python developers will disable that feature set as well, so you're on your own.
See: https://docs.python.org/2/library/user.html
Put this code into your home directory in ~/.pythonrc.py
class secretclass:
def secretmessage(cls, myarg):
return myarg + " is if.. up in the sky, the sky"
secretmessage = classmethod( secretmessage )
def skycake(cls):
return "cookie and sky pie people can't go up and "
skycake = classmethod( skycake )
Put this code into your main.py (can be anywhere):
import user
msg = "The only way skycake tates good"
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
Run it, you should get this:
$ python main.py
The only way skycake tates good is if.. up in the sky,
the skycookie and sky pie people can't go up and have the sky pie!
SKYCAKE!
If you get an error here: ModuleNotFoundError: No module named 'user' then it means you're using python3, startuphooks are disabled there by default.
Credit for this jist goes to: https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py Send along your up-boats.
Example 7, Most Robust: Import files in python with the bare import command:
Make a new directory /home/el/foo5/
Make a new directory /home/el/foo5/herp
Make an empty file named __init__.py under herp:
el#apollo:/home/el/foo5/herp$ touch __init__.py
el#apollo:/home/el/foo5/herp$ ls
__init__.py
Make a new directory /home/el/foo5/herp/derp
Under derp, make another __init__.py file:
el#apollo:/home/el/foo5/herp/derp$ touch __init__.py
el#apollo:/home/el/foo5/herp/derp$ ls
__init__.py
Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:
def skycake():
print "SkyCake evolves to stay just beyond the cognitive reach of " +
"the bulk of men. SKYCAKE!!"
The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;
from herp.derp.yolo import skycake
skycake()
Run it:
el#apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.
If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131
importlib was added to Python 3 to programmatically import a module.
import importlib
moduleName = input('Enter module name:')
importlib.import_module(moduleName)
The .py extension should be removed from moduleName. The function also defines a package argument for relative imports.
In python 2.x:
Just import file without the .py extension
A folder can be marked as a package, by adding an empty __init__.py file
You can use the __import__ function, which takes the module name (without extension) as a string extension
pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))
Type help(__import__) for more details.
First case
You want to import file A.py in file B.py, these two files are in the same folder, like this:
.
├── A.py
└── B.py
You can do this in file B.py:
import A
or
from A import *
or
from A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Second case
You want to import file folder/A.py in file B.py, these two files are not in the same folder, like this:
.
├── B.py
└── folder
└── A.py
You can do this in file B.py:
import folder.A
or
from folder.A import *
or
from folder.A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Summary
In the first case, file A.py is a module that you imports in file B.py, you used the syntax import module_name.
In the second case, folder is the package that contains the module A.py, you used the syntax import package_name.module_name.
For more info on packages and modules, consult this link.
To import a specific Python file at 'runtime' with a known name:
import os
import sys
...
scriptpath = "../Test/"
# Add the directory containing your module to the Python path (wants absolute paths)
sys.path.append(os.path.abspath(scriptpath))
# Do the import
import MyModule
You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type
from root.parent.folder.file import variable, class, whatever
Import doc .. -- Link for reference
The __init__.py files are required to make Python treat the directories as containing packages, this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
__init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable.
mydir/spam/__init__.py
mydir/spam/module.py
import spam.module
or
from spam import module
from file import function_name ######## Importing specific function
function_name() ######## Calling function
and
import file ######## Importing whole package
file.function1_name() ######## Calling function
file.function2_name() ######## Calling function
Here are the two simple ways I have understood by now and make sure your "file.py" file which you want to import as a library is present in your current directory only.
If the function defined is in a file x.py:
def greet():
print('Hello! How are you?')
In the file where you are importing the function, write this:
from x import greet
This is useful if you do not wish to import all the functions in a file.
I'd like to add this note I don't very clearly elsewhere; inside a module/package, when loading from files, the module/package name must be prefixed with the mymodule. Imagine mymodule being layout like this:
/main.py
/mymodule
/__init__.py
/somefile.py
/otherstuff.py
When loading somefile.py/otherstuff.py from __init__.py the contents should look like:
from mymodule.somefile import somefunc
from mymodule.otherstuff import otherfunc
Using Python 3.5 or later, you can use importlib.util to directly import a .py file in an arbitrary location as a module without needing to modify sys.path.
import importlib.util
import sys
def load_module(file_name, module_name)
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
The file_name parameter must be a string or a path-like object. The module_name parameter is required because all loaded Python modules must have a (dotted) module name (like sys, importlib, or importlib.util), but you can choose any available name you want for this new module.
You can use this function like this:
my_module = load_module("file.py", "mymod")
After it has been imported once into the Python process using the load_module() function, the module will be importable using the module name given to it.
file.py:
print(f"file.py was imported as {__name__}")
one.py:
print(f"one.py was imported as {__name__}")
load_module("file.py", "mymod")
import two
two.py:
print(f"two.py was imported as {__name__})")
import mymod
Given the files above, you can run the following command to see how file.py became importable.
$ python3 -m one
one.py was imported as __main__
two.py was imported as two
file.py was imported as mymod
This answer is based on the official Python documentation: importlib: Importing a source file directly.
the best way to import .py files is by way of __init__.py. the simplest thing to do, is to create an empty file named __init__.py in the same directory that your.py file is located.
this post by Mike Grouchy is a great explanation of __init__.py and its use for making, importing, and setting up python packages.
import sys
#print(sys.path)
sys.path.append('../input/tokenization')
import tokenization
To import any .py file, you can use above code.
First append the path and then import
Note:'../input/tokenization' directory contains tokenization.py file
How I import is import the file and use shorthand of it's name.
import DoStuff.py as DS
DS.main()
Don't forget that your importing file MUST BE named with .py extension
There are couple of ways of including your python script with name abc.py
e.g. if your file is called abc.py (import abc)
Limitation is that your file should be present in the same location where your calling python script is.
import abc
e.g. if your python file is inside the Windows folder. Windows folder is present at the same location where your calling python script is.
from folder import abc
Incase abc.py script is available insider internal_folder which is present inside folder
from folder.internal_folder import abc
As answered by James above, in case your file is at some fixed location
import os
import sys
scriptpath = "../Test/MyModule.py"
sys.path.append(os.path.abspath(scriptpath))
import MyModule
In case your python script gets updated and you don't want to upload - use these statements for auto refresh. Bonus :)
%load_ext autoreload
%autoreload 2
In case the module you want to import is not in a sub-directory, then try the following and run app.py from the deepest common parent directory:
Directory Structure:
/path/to/common_dir/module/file.py
/path/to/common_dir/application/app.py
/path/to/common_dir/application/subpath/config.json
In app.py, append path of client to sys.path:
import os, sys, inspect
sys.path.append(os.getcwd())
from module.file import MyClass
instance = MyClass()
Optional (If you load e.g. configs) (Inspect seems to be the most robust one for my use cases)
# Get dirname from inspect module
filename = inspect.getframeinfo(inspect.currentframe()).filename
dirname = os.path.dirname(os.path.abspath(filename))
MY_CONFIG = os.path.join(dirname, "subpath/config.json")
Run
user#host:/path/to/common_dir$ python3 application/app.py
This solution works for me in cli, as well as PyCharm.
This is how I did to call a function from a python file, that is flexible for me to call any functions.
import os, importlib, sys
def callfunc(myfile, myfunc, *args):
pathname, filename = os.path.split(myfile)
sys.path.append(os.path.abspath(pathname))
modname = os.path.splitext(filename)[0]
mymod = importlib.import_module(modname)
result = getattr(mymod, myfunc)(*args)
return result
result = callfunc("pathto/myfile.py", "myfunc", arg1, arg2)
Just to import python file in another python file
lets say I have helper.py python file which has a display function like,
def display():
print("I'm working sundar gsv")
Now in app.py, you can use the display function,
import helper
helper.display()
The output,
I'm working sundar gsv
NOTE: No need to specify the .py extension.
One very unknown feature of Python is the ability to import zip files:
library.zip
|-library
|--__init__.py
The file __init__.py of the package contains the following:
def dummy():
print 'Testing things out...'
We can write another script which can import a package from the zip archive. It is only necessary to add the zip file to the sys.path.
import sys
sys.path.append(r'library.zip')
import library
def run():
library.dummy()
run()
This helped me to structure my Python project with Visual Studio Code.
The problem could be caused when you don't declare __init__.py inside the directory. And the directory becomes implicit namespace package. Here is a nice summary about Python imports and project structure.
Also if you want to use the Visual Studio Code run button in the top bar with a script which is not inside the main package, you may try to run console from the actual directory.
For example, you want to execute an opened test_game_item.py from the tests package and you have Visual Studio Code opened in omission (main package) directory:
├── omission
│ ├── app.py
│ ├── common
│ │ ├── classproperty.py
│ │ ├── constants.py
│ │ ├── game_enums.py
│ │ └── __init__.py
│ ├── game
│ │ ├── content_loader.py
│ │ ├── game_item.py
│ │ ├── game_round.py
│ │ ├── __init__.py
│ │ └── timer.py
│ ├── __init__.py
│ ├── __main__.py
│ ├── resources
│ └── tests
│ ├── __init__.py
│ ├── test_game_item.py
│ ├── test_game_round_settings.py
│ ├── test_scoreboard.py
│ ├── test_settings.py
│ ├── test_test.py
│ └── test_timer.py
├── pylintrc
├── README.md
└── .gitignore
The directory structure is from [2].
You can try set this:
(Windows) Ctrl + Shift + P → Preferences: Open Settings (JSON).
Add this line to your user settings:
"python.terminal.executeInFileDir": true
A more comprehensive answer also for other systems is in this question.
This may sound crazy but you can just create a symbolic link to the file you want to import if you're just creating a wrapper script to it.
You can also do this: from filename import something
example: from client import Client
Note that you do not need the .py .pyw .pyui extension.
There are many ways, as listed above, but I find that I just want to import he contents of a file, and don't want to have to write lines and lines and have to import other modules. So, I came up with a way to get the contents of a file, even with the dot syntax (file.property) as opposed to merging the imported file with yours.
First of all, here is my file which I'll import, data.py
testString= "A string literal to import and test with"
Note: You could use the .txt extension instead.
In mainfile.py, start by opening and getting the contents.
#!usr/bin/env python3
Data=open('data.txt','r+').read()
Now you have the contents as a string, but trying to access data.testString will cause an error, as data is an instance of the str class, and even if it does have a property testString it will not do what you expected.
Next, create a class. For instance (pun intended), ImportedFile
class ImportedFile:
And put this into it (with the appropriate indentation):
exec(data)
And finally, re-assign data like so:
data=ImportedFile()
And that's it! Just access like you would for any-other module, typing print(data.testString) will print to the console A string literal to import and test with.
If, however, you want the equivalent of from mod import * just drop the class, instance assignment, and de-dent the exec.
Hope this helps:)
-Benji
from y import *
Say you have a file x and y.
You want to import y file to x.
then go to your x file and place the above command. To test this just put a print function in your y file and when your import was successful then in x file it should print it.

How do I import other Python files?

How do I import files in Python? I want to import:
a file (e.g. file.py)
a folder
a file dynamically at runtime, based on user input
one specific part of a file (e.g. a single function)
There are many ways to import a python file, all with their pros and cons.
Don't just hastily pick the first import strategy that works for you or else you'll have to rewrite the codebase later on when you find it doesn't meet your needs.
I'll start out explaining the easiest example #1, then I'll move toward the most professional and robust example #7
Example 1, Import a python module with python interpreter:
Put this in /home/el/foo/fox.py:
def what_does_the_fox_say():
print("vixens cry")
Get into the python interpreter:
el#apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
>>> import fox
>>> fox.what_does_the_fox_say()
vixens cry
>>>
You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.
Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:
Put this in /home/el/foo2/mylib.py:
def moobar():
print("hi")
Put this in /home/el/foo2/main.py:
execfile("/home/el/foo2/mylib.py")
moobar()
run the file:
el#apollo:/home/el/foo$ python main.py
hi
The function moobar was imported from mylib.py and made available in main.py
Example 3, Use from ... import ... functionality:
Put this in /home/el/foo3/chekov.py:
def question():
print "where are the nuclear wessels?"
Put this in /home/el/foo3/main.py:
from chekov import question
question()
Run it like this:
el#apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
If you defined other functions in chekov.py, they would not be available unless you import *
Example 4, Import riaa.py if it's in a different file location from where it is imported
Put this in /home/el/foo4/stuff/riaa.py:
def watchout():
print "computers are transforming into a noose and a yoke for humans"
Put this in /home/el/foo4/main.py:
import sys
import os
sys.path.append(os.path.abspath("/home/el/foo4/stuff"))
from riaa import *
watchout()
Run it:
el#apollo:/home/el/foo4$ python main.py
computers are transforming into a noose and a yoke for humans
That imports everything in the foreign file from a different directory.
Example 5, use os.system("python yourfile.py")
import os
os.system("python yourfile.py")
Example 6, import your file via piggybacking the python startuphook:
Update: This example used to work for both python2 and 3, but now only works for python2. python3 got rid of this user startuphook feature set because it was abused by low-skill python library writers, using it to impolitely inject their code into the global namespace, before all user-defined programs. If you want this to work for python3, you'll have to get more creative. If I tell you how to do it, python developers will disable that feature set as well, so you're on your own.
See: https://docs.python.org/2/library/user.html
Put this code into your home directory in ~/.pythonrc.py
class secretclass:
def secretmessage(cls, myarg):
return myarg + " is if.. up in the sky, the sky"
secretmessage = classmethod( secretmessage )
def skycake(cls):
return "cookie and sky pie people can't go up and "
skycake = classmethod( skycake )
Put this code into your main.py (can be anywhere):
import user
msg = "The only way skycake tates good"
msg = user.secretclass.secretmessage(msg)
msg += user.secretclass.skycake()
print(msg + " have the sky pie! SKYCAKE!")
Run it, you should get this:
$ python main.py
The only way skycake tates good is if.. up in the sky,
the skycookie and sky pie people can't go up and have the sky pie!
SKYCAKE!
If you get an error here: ModuleNotFoundError: No module named 'user' then it means you're using python3, startuphooks are disabled there by default.
Credit for this jist goes to: https://github.com/docwhat/homedir-examples/blob/master/python-commandline/.pythonrc.py Send along your up-boats.
Example 7, Most Robust: Import files in python with the bare import command:
Make a new directory /home/el/foo5/
Make a new directory /home/el/foo5/herp
Make an empty file named __init__.py under herp:
el#apollo:/home/el/foo5/herp$ touch __init__.py
el#apollo:/home/el/foo5/herp$ ls
__init__.py
Make a new directory /home/el/foo5/herp/derp
Under derp, make another __init__.py file:
el#apollo:/home/el/foo5/herp/derp$ touch __init__.py
el#apollo:/home/el/foo5/herp/derp$ ls
__init__.py
Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:
def skycake():
print "SkyCake evolves to stay just beyond the cognitive reach of " +
"the bulk of men. SKYCAKE!!"
The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;
from herp.derp.yolo import skycake
skycake()
Run it:
el#apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
The empty __init__.py file communicates to the python interpreter that the developer intends this directory to be an importable package.
If you want to see my post on how to include ALL .py files under a directory see here: https://stackoverflow.com/a/20753073/445131
importlib was added to Python 3 to programmatically import a module.
import importlib
moduleName = input('Enter module name:')
importlib.import_module(moduleName)
The .py extension should be removed from moduleName. The function also defines a package argument for relative imports.
In python 2.x:
Just import file without the .py extension
A folder can be marked as a package, by adding an empty __init__.py file
You can use the __import__ function, which takes the module name (without extension) as a string extension
pmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))
Type help(__import__) for more details.
First case
You want to import file A.py in file B.py, these two files are in the same folder, like this:
.
├── A.py
└── B.py
You can do this in file B.py:
import A
or
from A import *
or
from A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Second case
You want to import file folder/A.py in file B.py, these two files are not in the same folder, like this:
.
├── B.py
└── folder
└── A.py
You can do this in file B.py:
import folder.A
or
from folder.A import *
or
from folder.A import THINGS_YOU_WANT_TO_IMPORT_IN_A
Then you will be able to use all the functions of file A.py in file B.py
Summary
In the first case, file A.py is a module that you imports in file B.py, you used the syntax import module_name.
In the second case, folder is the package that contains the module A.py, you used the syntax import package_name.module_name.
For more info on packages and modules, consult this link.
To import a specific Python file at 'runtime' with a known name:
import os
import sys
...
scriptpath = "../Test/"
# Add the directory containing your module to the Python path (wants absolute paths)
sys.path.append(os.path.abspath(scriptpath))
# Do the import
import MyModule
You do not have many complex methods to import a python file from one folder to another. Just create a __init__.py file to declare this folder is a python package and then go to your host file where you want to import just type
from root.parent.folder.file import variable, class, whatever
Import doc .. -- Link for reference
The __init__.py files are required to make Python treat the directories as containing packages, this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path.
__init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable.
mydir/spam/__init__.py
mydir/spam/module.py
import spam.module
or
from spam import module
from file import function_name ######## Importing specific function
function_name() ######## Calling function
and
import file ######## Importing whole package
file.function1_name() ######## Calling function
file.function2_name() ######## Calling function
Here are the two simple ways I have understood by now and make sure your "file.py" file which you want to import as a library is present in your current directory only.
If the function defined is in a file x.py:
def greet():
print('Hello! How are you?')
In the file where you are importing the function, write this:
from x import greet
This is useful if you do not wish to import all the functions in a file.
I'd like to add this note I don't very clearly elsewhere; inside a module/package, when loading from files, the module/package name must be prefixed with the mymodule. Imagine mymodule being layout like this:
/main.py
/mymodule
/__init__.py
/somefile.py
/otherstuff.py
When loading somefile.py/otherstuff.py from __init__.py the contents should look like:
from mymodule.somefile import somefunc
from mymodule.otherstuff import otherfunc
Using Python 3.5 or later, you can use importlib.util to directly import a .py file in an arbitrary location as a module without needing to modify sys.path.
import importlib.util
import sys
def load_module(file_name, module_name)
spec = importlib.util.spec_from_file_location(module_name, file_name)
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
return module
The file_name parameter must be a string or a path-like object. The module_name parameter is required because all loaded Python modules must have a (dotted) module name (like sys, importlib, or importlib.util), but you can choose any available name you want for this new module.
You can use this function like this:
my_module = load_module("file.py", "mymod")
After it has been imported once into the Python process using the load_module() function, the module will be importable using the module name given to it.
file.py:
print(f"file.py was imported as {__name__}")
one.py:
print(f"one.py was imported as {__name__}")
load_module("file.py", "mymod")
import two
two.py:
print(f"two.py was imported as {__name__})")
import mymod
Given the files above, you can run the following command to see how file.py became importable.
$ python3 -m one
one.py was imported as __main__
two.py was imported as two
file.py was imported as mymod
This answer is based on the official Python documentation: importlib: Importing a source file directly.
the best way to import .py files is by way of __init__.py. the simplest thing to do, is to create an empty file named __init__.py in the same directory that your.py file is located.
this post by Mike Grouchy is a great explanation of __init__.py and its use for making, importing, and setting up python packages.
import sys
#print(sys.path)
sys.path.append('../input/tokenization')
import tokenization
To import any .py file, you can use above code.
First append the path and then import
Note:'../input/tokenization' directory contains tokenization.py file
How I import is import the file and use shorthand of it's name.
import DoStuff.py as DS
DS.main()
Don't forget that your importing file MUST BE named with .py extension
There are couple of ways of including your python script with name abc.py
e.g. if your file is called abc.py (import abc)
Limitation is that your file should be present in the same location where your calling python script is.
import abc
e.g. if your python file is inside the Windows folder. Windows folder is present at the same location where your calling python script is.
from folder import abc
Incase abc.py script is available insider internal_folder which is present inside folder
from folder.internal_folder import abc
As answered by James above, in case your file is at some fixed location
import os
import sys
scriptpath = "../Test/MyModule.py"
sys.path.append(os.path.abspath(scriptpath))
import MyModule
In case your python script gets updated and you don't want to upload - use these statements for auto refresh. Bonus :)
%load_ext autoreload
%autoreload 2
In case the module you want to import is not in a sub-directory, then try the following and run app.py from the deepest common parent directory:
Directory Structure:
/path/to/common_dir/module/file.py
/path/to/common_dir/application/app.py
/path/to/common_dir/application/subpath/config.json
In app.py, append path of client to sys.path:
import os, sys, inspect
sys.path.append(os.getcwd())
from module.file import MyClass
instance = MyClass()
Optional (If you load e.g. configs) (Inspect seems to be the most robust one for my use cases)
# Get dirname from inspect module
filename = inspect.getframeinfo(inspect.currentframe()).filename
dirname = os.path.dirname(os.path.abspath(filename))
MY_CONFIG = os.path.join(dirname, "subpath/config.json")
Run
user#host:/path/to/common_dir$ python3 application/app.py
This solution works for me in cli, as well as PyCharm.
This is how I did to call a function from a python file, that is flexible for me to call any functions.
import os, importlib, sys
def callfunc(myfile, myfunc, *args):
pathname, filename = os.path.split(myfile)
sys.path.append(os.path.abspath(pathname))
modname = os.path.splitext(filename)[0]
mymod = importlib.import_module(modname)
result = getattr(mymod, myfunc)(*args)
return result
result = callfunc("pathto/myfile.py", "myfunc", arg1, arg2)
Just to import python file in another python file
lets say I have helper.py python file which has a display function like,
def display():
print("I'm working sundar gsv")
Now in app.py, you can use the display function,
import helper
helper.display()
The output,
I'm working sundar gsv
NOTE: No need to specify the .py extension.
One very unknown feature of Python is the ability to import zip files:
library.zip
|-library
|--__init__.py
The file __init__.py of the package contains the following:
def dummy():
print 'Testing things out...'
We can write another script which can import a package from the zip archive. It is only necessary to add the zip file to the sys.path.
import sys
sys.path.append(r'library.zip')
import library
def run():
library.dummy()
run()
This helped me to structure my Python project with Visual Studio Code.
The problem could be caused when you don't declare __init__.py inside the directory. And the directory becomes implicit namespace package. Here is a nice summary about Python imports and project structure.
Also if you want to use the Visual Studio Code run button in the top bar with a script which is not inside the main package, you may try to run console from the actual directory.
For example, you want to execute an opened test_game_item.py from the tests package and you have Visual Studio Code opened in omission (main package) directory:
├── omission
│ ├── app.py
│ ├── common
│ │ ├── classproperty.py
│ │ ├── constants.py
│ │ ├── game_enums.py
│ │ └── __init__.py
│ ├── game
│ │ ├── content_loader.py
│ │ ├── game_item.py
│ │ ├── game_round.py
│ │ ├── __init__.py
│ │ └── timer.py
│ ├── __init__.py
│ ├── __main__.py
│ ├── resources
│ └── tests
│ ├── __init__.py
│ ├── test_game_item.py
│ ├── test_game_round_settings.py
│ ├── test_scoreboard.py
│ ├── test_settings.py
│ ├── test_test.py
│ └── test_timer.py
├── pylintrc
├── README.md
└── .gitignore
The directory structure is from [2].
You can try set this:
(Windows) Ctrl + Shift + P → Preferences: Open Settings (JSON).
Add this line to your user settings:
"python.terminal.executeInFileDir": true
A more comprehensive answer also for other systems is in this question.
This may sound crazy but you can just create a symbolic link to the file you want to import if you're just creating a wrapper script to it.
You can also do this: from filename import something
example: from client import Client
Note that you do not need the .py .pyw .pyui extension.
There are many ways, as listed above, but I find that I just want to import he contents of a file, and don't want to have to write lines and lines and have to import other modules. So, I came up with a way to get the contents of a file, even with the dot syntax (file.property) as opposed to merging the imported file with yours.
First of all, here is my file which I'll import, data.py
testString= "A string literal to import and test with"
Note: You could use the .txt extension instead.
In mainfile.py, start by opening and getting the contents.
#!usr/bin/env python3
Data=open('data.txt','r+').read()
Now you have the contents as a string, but trying to access data.testString will cause an error, as data is an instance of the str class, and even if it does have a property testString it will not do what you expected.
Next, create a class. For instance (pun intended), ImportedFile
class ImportedFile:
And put this into it (with the appropriate indentation):
exec(data)
And finally, re-assign data like so:
data=ImportedFile()
And that's it! Just access like you would for any-other module, typing print(data.testString) will print to the console A string literal to import and test with.
If, however, you want the equivalent of from mod import * just drop the class, instance assignment, and de-dent the exec.
Hope this helps:)
-Benji
from y import *
Say you have a file x and y.
You want to import y file to x.
then go to your x file and place the above command. To test this just put a print function in your y file and when your import was successful then in x file it should print it.

What is __init__.py for?

What is __init__.py for in a Python source directory?
It used to be a required part of a package (old, pre-3.3 "regular package", not newer 3.3+ "namespace package").
Here's the documentation.
Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The __init__.py file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.
But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without __init__.py.
Files named __init__.py are used to mark directories on disk as Python package directories.
If you have the files
mydir/spam/__init__.py
mydir/spam/module.py
and mydir is on your path, you can import the code in module.py as
import spam.module
or
from spam import module
If you remove the __init__.py file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.
The __init__.py file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc.
Given the example above, the contents of the init module can be accessed as
import spam
based on this
In addition to labeling a directory as a Python package and defining __all__, __init__.py allows you to define any variable at the package level. Doing so is often convenient if a package defines something that will be imported frequently, in an API-like fashion. This pattern promotes adherence to the Pythonic "flat is better than nested" philosophy.
An example
Here is an example from one of my projects, in which I frequently import a sessionmaker called Session to interact with my database. I wrote a "database" package with a few modules:
database/
__init__.py
schema.py
insertions.py
queries.py
My __init__.py contains the following code:
import os
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
engine = create_engine(os.environ['DATABASE_URL'])
Session = sessionmaker(bind=engine)
Since I define Session here, I can start a new session using the syntax below. This code would be the same executed from inside or outside of the "database" package directory.
from database import Session
session = Session()
Of course, this is a small convenience -- the alternative would be to define Session in a new file like "create_session.py" in my database package, and start new sessions using:
from database.create_session import Session
session = Session()
Further reading
There is a pretty interesting reddit thread covering appropriate uses of __init__.py here:
http://www.reddit.com/r/Python/comments/1bbbwk/whats_your_opinion_on_what_to_include_in_init_py/
The majority opinion seems to be that __init__.py files should be very thin to avoid violating the "explicit is better than implicit" philosophy.
There are 2 main reasons for __init__.py
For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).
your_package/
__init__.py
file1.py
file2.py
...
fileN.py
# in __init__.py
from .file1 import *
from .file2 import *
...
from .fileN import *
# in file1.py
def add():
pass
then others can call add() by
from your_package import add
without knowing file1's inside functions, like
from your_package.file1 import add
If you want something to be initialized; for example, logging (which should be put in the top level):
import logging.config
logging.config.dictConfig(Your_logging_config)
The __init__.py file makes Python treat directories containing it as modules.
Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.
Since Python 3.3, __init__.py is no longer required to define directories as importable Python packages.
Check PEP 420: Implicit Namespace Packages:
Native support for package directories that don’t require __init__.py marker files and can automatically span multiple path segments (inspired by various third party approaches to namespace packages, as described in PEP 420)
Here's the test:
$ mkdir -p /tmp/test_init
$ touch /tmp/test_init/module.py /tmp/test_init/__init__.py
$ tree -at /tmp/test_init
/tmp/test_init
├── module.py
└── __init__.py
$ python3
>>> import sys
>>> sys.path.insert(0, '/tmp')
>>> from test_init import module
>>> import test_init.module
$ rm -f /tmp/test_init/__init__.py
$ tree -at /tmp/test_init
/tmp/test_init
└── module.py
$ python3
>>> import sys
>>> sys.path.insert(0, '/tmp')
>>> from test_init import module
>>> import test_init.module
references:
https://docs.python.org/3/whatsnew/3.3.html#pep-420-implicit-namespace-packages
https://www.python.org/dev/peps/pep-0420/
Is __init__.py not required for packages in Python 3?
Although Python works without an __init__.py file you should still include one.
It specifies that the directory should be treated as a package, so therefore include it (even if it is empty).
There is also a case where you may actually use an __init__.py file:
Imagine you had the following file structure:
main_methods
|- methods.py
And methods.py contained this:
def foo():
return 'foo'
To use foo() you would need one of the following:
from main_methods.methods import foo # Call with foo()
from main_methods import methods # Call with methods.foo()
import main_methods.methods # Call with main_methods.methods.foo()
Maybe there you need (or want) to keep methods.py inside main_methods (runtimes/dependencies for example) but you only want to import main_methods.
If you changed the name of methods.py to __init__.py then you could use foo() by just importing main_methods:
import main_methods
print(main_methods.foo()) # Prints 'foo'
This works because __init__.py is treated as part of the package.
Some Python packages actually do this. An example is with JSON, where running import json is actually importing __init__.py from the json package (see the package file structure here):
Source code: Lib/json/__init__.py
In Python the definition of package is very simple. Like Java the hierarchical structure and the directory structure are the same. But you have to have __init__.py in a package. I will explain the __init__.py file with the example below:
package_x/
|-- __init__.py
|-- subPackage_a/
|------ __init__.py
|------ module_m1.py
|-- subPackage_b/
|------ __init__.py
|------ module_n1.py
|------ module_n2.py
|------ module_n3.py
__init__.py can be empty, as long as it exists. It indicates that the directory should be regarded as a package. Of course, __init__.py can also set the appropriate content.
If we add a function in module_n1:
def function_X():
print "function_X in module_n1"
return
After running:
>>>from package_x.subPackage_b.module_n1 import function_X
>>>function_X()
function_X in module_n1
Then we followed the hierarchy package and called module_n1 the function. We can use __init__.py in subPackage_b like this:
__all__ = ['module_n2', 'module_n3']
After running:
>>>from package_x.subPackage_b import *
>>>module_n1.function_X()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named module_n1
Hence using * importing, module package is subject to __init__.py content.
__init__.py will treat the directory it is in as a loadable module.
For people who prefer reading code, I put Two-Bit Alchemist's comment here.
$ find /tmp/mydir/
/tmp/mydir/
/tmp/mydir//spam
/tmp/mydir//spam/__init__.py
/tmp/mydir//spam/module.py
$ cd ~
$ python
>>> import sys
>>> sys.path.insert(0, '/tmp/mydir')
>>> from spam import module
>>> module.myfun(3)
9
>>> exit()
$
$ rm /tmp/mydir/spam/__init__.py*
$
$ python
>>> import sys
>>> sys.path.insert(0, '/tmp/mydir')
>>> from spam import module
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named spam
>>>
It facilitates importing other python files. When you placed this file in a directory (say stuff)containing other py files, then you can do something like import stuff.other.
root\
stuff\
other.py
morestuff\
another.py
Without this __init__.py inside the directory stuff, you couldn't import other.py, because Python doesn't know where the source code for stuff is and unable to recognize it as a package.
An __init__.py file makes imports easy. When an __init__.py is present within a package, function a() can be imported from file b.py like so:
from b import a
Without it, however, you can't import directly. You have to amend the system path:
import sys
sys.path.insert(0, 'path/to/b.py')
from b import a
One thing __init__.py allows is converting a module to a package without breaking the API or creating extraneous nested namespaces or private modules*. This helps when I want to extend a namespace.
If I have a file util.py containing
def foo():
...
then users will access foo with
from util import foo
If I then want to add utility functions for database interaction, and I want them to have their own namespace under util, I'll need a new directory**, and to keep API compatibility (so that from util import foo still works), I'll call it util/. I could move util.py into util/ like so,
util/
__init__.py
util.py
db.py
and in util/__init__.py do
from util import *
but this is redundant. Instead of having a util/util.py file, we can just put the util.py contents in __init__.py and the user can now
from util import foo
from util.db import check_schema
I think this nicely highlights how a util package's __init__.py acts in a similar way to a util module
* this is hinted at in the other answers, but I want to highlight it here
** short of employing import gymnastics. Note it won't work to create a new package with the same name as the file, see this
If you're using Python 2 and want to load siblings of your file you can simply add the parent folder of your file to your system paths of the session. It will behave about the same as if your current file was an init file.
import os
import sys
dir_path = os.path.dirname(__file__)
sys.path.insert(0, dir_path)
After that regular imports relative to the file's directory will work just fine. E.g.
import cheese
from vehicle_parts import *
# etc.
Generally you want to use a proper init.py file instead though, but when dealing with legacy code you might be stuck with f.ex. a library hard-coded to load a particular file and nothing but. For those cases this is an alternative.
__init__.py : It is a Python file found in a package directory, it is invoked when the package or a module in the package is imported. You can use this to execute package initialization code, i.e. whenever the package is imported the python statements are executed first before the other modules in this folder gets executed. It is similar to main function of c or Java program, but this exists in the Python package module (folder) rather than in the core Python file.
also it has access to global variables defined in this __init__.py file as when the module is imported into Python file.
for eg.
I have a __init__.py file in a folder called pymodlib, this file contains the following statements:
print(f'Invoking __init__.py for {__name__}')
pystructures = ['for_loop', 'while__loop', 'ifCondition']
When I import this package pymodlib in my solution module or notebook or python console:
These two statements get executed while importing.
So in the log or console you would see the following output:
>>> import pymodlib
Invoking __init__.py for pymodlib
in the next statement of python console: I can access the global variable:
>> pymodlib.pystructures
it gives the following output:
['for_loop', 'while__loop', 'ifCondition']
Now, from Python 3.3 onwards the use of this file has been optional to make folder a Python module. So you can skip from including it in the python module folder.

Categories

Resources