In module MainClass.py which is in c:\MyProject\ModelClasses I'm
trying import module MainHelper.py from c:\MyProject\Helpers
from Helpers.MainHelper import MainHelper
x=MainHelper()
and I get error
ImportError: No module named Helpers.MainHelper
In WIng IDe I've added path c:\MyProject\Helpers Project->ProjectProperties\PythonPath
I'm using windows xp
If c:\MyProject\Helpers is in PYTHONPATH, you should write
from MainHelper import MainHelper
in the MainClass.py module.
If you want to treat the Helpers directory as a package, do the following:
PYTHONPATH should be c:\MyProject
The Helpers directory should have a (possibly empty) __init__.py file in it.
You would then use import statements like:
import Helpers.MainHelper
from Helpers.MainHelper import MainHelper
from Helpers import MainHelper
Documentation links:
http://docs.python.org/tutorial/modules.html
http://docs.python.org/tutorial/modules.html#packages
Related
I'm trying to run a code that imports .py files form another folder. The hierarchy is as follows:
Here's the section of the code on my AppiumTest file where I call the imports:
from appium import webdriver
import unittest
from test.pageObj.LoginPage import LoginActivity
from test.pageObj.HomePage import HomeActivity
When attempting to run I get this error message:
ModuleNotFoundError: No module named 'test.pageObj'
What am I missing?
Could you try the below code instead and see whether it works?
from pageObj.LoginPage import LoginActivity
from pageObj.HomePage import HomeActivity
Try:
from pageObj.LoginPage import LoginActivity
from pageObj.HomePage import HomeActivity
__ init __.py in pageObj
from .HomePage import HomeActivity
from .LoginPage import LoginActivity
AppiumTest.py
from test.pageObj import HomePage
from test.pageObj import LoginPage
package and __ init __
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.
You do not need to include the parent directory in the import statement. from pageobj import LoginActivity should work.
I have a git project ('submodule_folder') cloned as a submodule inside my git project ('my_project') with the following file structure:
my_project
submodule_folder
submodule_folder2
__init__.py
submodule_file.py
file_to_import.py
my_file1.py
my_file2.py
I want to import 'file_to_import.py' from 'my_file1.py' but 'file_to_import.py' imports 'submodule_file.py'.
When doing the following import from 'my_file1.py' I get a ModuleNotFoundError: No module named 'submodule_folder2'.
from submodule_folder import file_to_import
Using Python3, is there any way to do this import without writing sys.path.append('submodule_folder/') in each of my files? I'd prefer not to modify the content of 'submodule_folder'.
If file_to_import.py has
from submodule_folder2 import submodule_file
it worked in Python 2 because in Py2 it was implicit relative import. Python 3 disabled this import mode, it only allows absolute import or explicit relative import. Either
from submodule_folder.submodule_folder2 import submodule_file
or
from .submodule_folder2 import submodule_file
I am not a proficient Python coder, hence this might be a basic question:
In my main python code, I load a python code file using (dynamically)
import imp
model = imp.load_source('name','c:/modeldir/modelfile.py')
modelfile.py does an import on the top:
from MyLib import MyLib
MyLib.py is in the same folder as modelfile.py
I get:
ImportError: No module named 'MyLib'
I have also tried:
import os
os.chdir('c:/modeldir')
just before the imp.load_source, did not help.
EDIT:
I am using Python 3.5.2
I've added an empty __init__.py file in 'c:/modeldir'
How to solve this?
The following made it work
sys.path.append('c:/modeldir')
This adds the folder to the python import path and then MyLib can be found
I have following directory structure,
/
__init__.py
__meta__.py
I tried to import __meta__ in a file but python complains about not finding a module named __meta__. I checked the current working directory for the file useing os.getcwd() which printed the directory with __meta__.py.
But I can import the module from python interpreter.
Append it to sys path first, and then try to import your module
import os
import sys
sys.path.append(os.getcwd())
import myModule as myModule
with this code works import and I can make my doc
import myPackage.myModule as myModule
with this I get
"No module named myPackage.myModule"
Doesn't matter if this file exist in root directory or in myPackage directory.
In RST-File I have not mentioned about myModule, I want to document other file that just import this module.
Sphinx needs to be able to import your code, to generate documentation for classes and functions. You probably need to add your project's root folder to sys.path in Sphinx. You can do this from the Sphinx conf.py file:
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
Replace '..' with the relative path to the project root.