I have the following directory structure, in Ubuntu:
/Test/Foo
/Test/Foo/foo.py
If I am in /Test, and I run python from the command line, followed by from Foo import foo, I get the following error: ImportError: No module named Foo.
But this is very confusing, since according to here, one of the directories used to search when importing is the directory from which the script was invoked. If I print out sys.path though, it does not include /Test, it just includes other standard Python directories.
Any idea what is going on?
If I'm getting you right, what you are trying to achieve here is the Foo to be a package and foo be a module.
Since Foo is not made into a package (You don't have __init__.py in the directory), it is not recognized as a package and thus not imported.
When you move into /Test/Foo, then you are simply importing the module foo, which will work.
What you possibly need to do here is to create an __init__.py file inside /Test/Foo and then import the module from the package.
Or you can try relative imports. Something like from .Foo import foo.
If you just need a function try this (python 2.7):
sys.path.insert() inserts the directory specified in the path python uses to find files.
commify.py is a file in subdirectory xyz that contains a function commify(value)
import os
import sys
sys.path.insert(0, os.getcwd() + r'\xyz')
from commify import commify
print commify(12345678)
output: 12,345,678
Related
As per screen print, import shows error in Python 3.7 version, earlier it was working fine in version Python 2.7 and I am using IntelliJ Idea.
If you see, EOC related .py files are in the same folder and have classes which are being called in Main_EOC.py by passing objects which are inter-related. It's amazing to see the red line while importing files from same folder.
Please help me why it's showing such error
"This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.`"
Also, if you see the line which have full path, is not showing error
from EOC_Module.eoc.script.config import Config
Please help me if there is a way to add this full path on top of the code or other option.
The behavior of import path search changed between python2 and python3. The import path always includes the directory from which the main module was loaded, but it no longer includes directories from which modules were imported.
You need to change your import statement syntax as follows, if you want to import a module that lives in the same directory as the module in which you do the import:
# old way, import works if the named module is in this module's directory
import x
# new (Python3) way:
from . import x
For the second part: adding a path so all code can import from a certain directory: if that directory is (and will always be) relative to your main: you can add a few lines in the main module to make it available. Something like this:
import sys # if you haven't imported it already
import os.path
home = os.path.dirname(sys.argv[0])
sys.path.append( os.path.join(home, "EOC_Module/eoc/script") )
# now, you can import straight from the script directory
import EOC_Intraction
When using pycharm the root directory for your python executable is the same as the root directory of your project, this means that python will start looking for files in the root directory with this files:
.idea/
EOC_module/
logs/
reports/
sql/
This is the reason of why: from EOC_Module.eoc.script.config import Config works.
If you execute your code from the terminal with: python3 Main_EOC.py (not pycharm) the root directory for your python will be the same as the one containing the file, all the other imports will work but from EOC_Module.eoc.script.config import Config not.
So you need to make your imports from project directory if you are using pycharm.
I'm trying to modularize my code and I'm having issues with it.
My folder is constructed like this:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
Now my main.py contains these lines:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
The following error comes:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
You don't need to specify .py while importing modules. Python knows your modules are Python code only. So remove .py while importing modules in Python.
Test1 is a folder or directory and you are trying to access it like a package.
If you want to access it that way you have to insert init.py file in your folder. And also you don't need to specify .py when importing!
add __init__.py inside your directories
You can do like this
from test1.calcA import calcA where calcA can be a method.
I have the following files:
./ElementExtractor.py
./test/ElementExtractorTest.py
In ElementExtractorTest.py, I am trying to import ElementExtractor.py like this:
import sys
sys.path.append('../')
import ElementExtractor
However, I am getting:
ImportError: No module named 'ElementExtractor'
How come it's not seen?
Is there a simple way to import another class with a relative reference?
The general answer to this question should be don't, I guess. Messing with relative paths means that the path is relative to the place from where you're calling it. That's why PYTHONPATH is worth embracing instead.
Let's assume, your directory structure looks like this:
./projects/myproject/ElementExtractor.py
./projects/myproject/test/ElementExtractorTest.py
Now, you're calling your script like this:
[./projects/myproject]$ python3.5 ./test/ElementExtractorTest.py
Your current directory is myproject and in ElementExtractorTest.py you're adding ../ directory to sys.path. This means, that ./projects/myproject/../ (i.e.: ./projects) is effectively added to your PYTHONPATH. That' why Python is unable to locate your module.
Your code would work from test directory though:
[./projects/myproject/test]$ python3.5 ./ElementExtractorTest.py
Now, by adding .. to sys.path you are effectively adding ./projects/myproject/test/../ (i.e. ./projects/myproject) to sys.path, so the module can be found and imported.
my folder structure
./test.py
./Data.py
from test.py
import Data
obj = Data.Myclass()
update
in your case
from ..ElementExtractor import MyClass
Hope this helps :)
I have created a custom libraries which have following folder structure:
DS
-lib
-init__.py
-db.py
-dispatch.py
-links.py
Now I want to consume files under lib in the following folder structure:
apps
- framework
-- test.py
When I do from DS.lib.dispatch import * in test.py it gives error:
from lib.db import Links ImportError: No module named 'lib'
Update
dispatch.py
from lib.db import Links
from lib.links import Link
import numpy as np
from urllib.parse import urlparse
class Dispatch:
"""
This will pull an individual unprocessed link and pass to dispatcher
"""
_idle_link = None
...
There is more to this topic than I can explain here, but in the simple case of python packages and modules on the file system, it works roughly as follows. Given the following files:
Modules
A module is a simple .py file. These files can be located anywhere on the file system, and do not have to be in a package. A module usually contains class and function declarations. Any code outside of a function/class definition is executed when the module is run/imported.
/someplace/foo.py
def func():
print "I am foo.func"
print "I run at module import"
Packages
A package is a directory containing an __init__.py. A package can also be located anywhere in the filesystem. The __init__.py module is executed when the package is imported and the contents of this module becomes the contents of the package.
/anotherplace/bar/__init__.py
def func():
print "I am bar.func"
print "I run at package import"
A package contains modules or other child packages. Child modules are not automatically imported.
/anotherplace/bar/child.py
def func():
print "I am bar.baz.func"
print "I run at module import"
Imports
Both modules and packages can be imported from other modules.
When python sees an import statement, it searches certain directories for the code. These directories are the module search path. The search path is made up of:
the current working directory
the PYTHONPATH environment variable
system defaults
You can inspect the current module search path at runtime:
import sys
print sys.path
You will note that in the examples above, I have put foo in /someplace/ and bar in /anotherplace/.
If I were to run the python interpreter in /someplace/ then import foo would succeed (because it's found at the current working directory). However import bar would fail, because /anotherplace/ is not on the search path.
For both imports to work, both source folders must be on the search path. Note that the correct source folder for a package is the directory above the one with __init__.py. In this case, it should be:
/someplace/
/anotherplace/
Desiring to improve my Python style, I ran a PEP-8 style checker on one of my script and it complained about something I don't know how to fix. The prologue of the script is something like:
#! /bin/env python3
import sys
import os
exe_name = os.path.basename(os.path.realpath(__file__))
bin_dir = os.path.dirname(os.path.realpath(__file__))
inst_dir = os.path.dirname(bin_dir)
sys.path.insert(0, inst_dir+'/path/to/packages')
import mypackage.mymodule
and the style checker complain on the import mymodule line, stating that it should be a top of file. But I obviously can't move it before setting the path where it will be found. Is there a good way to achieve this (mandating an environment variable or a shell wrapper are not what I find better than my current code) while respecting PEP-8 recommendations at the same time?
If you want to avoid path manipulation, you may be able to do so by using the under-known .pth feature.
sys.path should begin with the directory containing the main program either by name or by reference as ''. I assume that the file importing mymodule is not part of mypackage, so that the '' entry is not useful for importing mymodule.
sys.path should end with the site-packages directory for the executing binary. That is the normal place for added packages. If you do not want to move mypackage into site-packages, you can extend the latter 'vitually' by putting a mystuff.pth file in it. It should contain one line: the path to the directory containing mypackage. Call it myprojects. Then mypackage and any other package in myprojects can be imported as if they were in site-packages.
One advantage of .pth files is that you can put identical copies in multiple site-packages directories. For instance, I have multiple projects in F:/python. I have multiple versions of Python installed. So I have put python.pth containing that one line in the site-packages for each.
The best strategy would be to put the sys.path related code in separate file and import it in working code file.
So, I will split above code in two files. One named my_module.py and other named working_module.py
Now my_module will contain below lines
import sys
import os
exe_name = os.path.basename(os.path.realpath(__file__))
bin_dir = os.path.dirname(os.path.realpath(__file__))
inst_dir = os.path.dirname(bin_dir)
sys.path.insert(0, inst_dir+'/path/to/packages')
and working_module will contain
import my_module
import mypackage.mymodule
because we are importing my_module before mypackage, it will execute the path related code before and your package will be available in path.
You can use importlib module (python 3.5) or imp for python 2.7 to load mypackage.mymodule programatically. Both have the same purpose:
mechanisms used to implement the import statement
This question might help you more:
How to import a module given the full path?
https://docs.python.org/3/library/importlib.html#examples
https://docs.python.org/2/library/imp.html