I have my application folder with my main.py and a lib folder with some modules:
LeapMotionInterpreter --> DataExtractor.py
lib --> Leap.py
I've already looked at how to do this and they all say to use sys.path.insert or sys.path.append but nothing seems to work, python doesn't even seem to know there is a lib folder there even though printing the sys.path does show the new added path to lib.
Here is the code I have tried:
1.
import sys
sys.path.insert(0, 'C:/Users/winba/Desktop/TYP/LeapMotionInterpreter/lib')
print (sys.path)
outputs:
['C:/Users/winba/Desktop/TYP/LeapMotionInterpreter/lib', 'C:\\Users\\winba\\Desktop\\TYP\\LeapMotionInterpreter'
import sys
sys.path.append('C:/Users/winba/Desktop/TYP/LeapMotionInterpreter/lib')
print (sys.path)
This one just outputs the lib path at the end of the array, I have also tried writing the paths with single \ or double \ instead of / but all of these just give me a 'No module named 'Leap' error if I try to import it and doesn't even acknowledge the existence of the lib folder. Also, putting my module in the same file seems to work as python is able to find the Leap.py file instantly however this is going to be a fairly big project and I wanted to keep it neat using folders so any help would be appreciated. Thank you.
If I'm understanding correctly, the directory you're targeting is a sub-directory of the current directory. You can add the file to the Python path at runtime by giving it it's relative path:
# DataExtractor.py
import sys
sys.path.insert(1, 'lib')
import Leap
This should solve the problem
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 am currently working in the following directory ( my script and associated files are here)
/scratch/conte/v/vprithiv/Gmsh
While I give this line , it throws me that error mentioned
from fenpy.sirah.Sira import Sira
Sira is located in the following path
/home/vprithiv/Fen/Utils/fenpy/sirah
If I am running my script from /home/vprithiv/Fen/Utils
I am able to obtain the output, But can I do it from my current working directory and somehow import sira??
When you try and import a module (eg import foo) in Python, the interpreter first checks the list of built in modules, and then checks the list of directories given in the sys.path variable for a file called foo.py.
sys.path typically contains your current working directory as it's first entry, and then a list of standard package locations on your system.
If you are only going to be working on your system, and you know the path to your package is going to stay constant, then you can just add your package directory to sys.path.
import sys
sys.path.append('/home/vprithiv/Fen/Utils')
from fendpy.sirah.Sira import Sira
I have a folder called Script and inside I have temp.py script. My temp script imports module from sub-folder called lib.
Lib folder has inside empty __init__.py and my parent_computer_test.py script.
in my temp.py script is the following code:
import lib.parent_computer_test as parent_computer_test
parent_computer_test.mainChunk()
parent_computer_test.splitChunks()
I managed to import module from sub-folder without any bigger problems.
This workflow/script works fine, BUT for a specific reason, my lib folder has to be somewhere else on my computer. There is a long story why, but it has to be that way.
Long story short. I want that my temp.py from the /Script folder imports modules from folder lib (or any other name) with parent_computer_test.py, but at the same time this folder is no sub-folder of /Script - so it is somewhere else in the computer. It can be C:/development/... or something.
So my question is how to import a module from a specific folder?
Append the path to lib folder to the SYS PATH Environment Variable. Then it can be imported from anywhere
import os, sys
lib_path = os.path.abspath(os.path.join('..', '..', '..', 'lib'))
sys.path.append(lib_path)
import mymodule
import imp
yourModule = imp.load_source('yourModuleName', '/path/to/yourModule.py')
foo = yourModule.YourFunction("You", "get", "the", "idea.")
I realize this is a special case, but in general, I would avoid stuff like this. Things can get ugly when you use absolute paths, especially if you move things around, and I would only use this for throwaway scripts or on systems that aren't going to change very much.
EDIT: Aswin's answer is a much better longterm solution.
You have to use sys.path.append("path") . But use this just one time. Then try
import "my_module". IT should be fine.
If you want to remove the path appended you can use,
sys.path.remove("path").
I know that there are plenty of similar questions on stack overflow. But the common answer doesn't seem to be working for me.
I have a file structure like this
proj/
lib/
__init__.py
aa.py
bb.py
test/
__init__.py
aa_test.py
I figured that if I include the code in my test.py
import lib.aa
or
from lib import aa
I would be able to reference the modules in the lib/ directory. But that did not work.
So I tried to add to path, and it adds it correctly:
os.environ["PATH"] += ":%s" % os.path.abspath(os.path.join("..",""))
print os.environ["PATH"]
but even now when I try the import statements above... I keep getting the error
ImportError: No module named aa
or
ImportError: Importing from non-package <Something...>
Is there something obvious I am missing?
Is there a way to check if I have configured my __init__.py files correctly, or to see my package hierarchy?
You need to update your sys.path, which is where python looks for modules, as opposed to your system's path in the current environment, which is what os.environ["PATH"] is referring to.
Example:
import os, sys
sys.path.insert(0, os.path.abspath(".."))
import aa
After doing this, you can use your functions in aa like this: aa.myfunc()
There's some more information in the accepted answer for python: import a module from a directory
The lib directory needs to be in your python module search path, which isn't the same things as the search path used by your shell.
This will probably work for you:
import sys, os
sys.path.append(os.path.abspath(".."))
However, it is probably better to run your code from a context where the lib package is already on the path. Such as from the 'proj' directory.
Where is the code that you're trying to import lib.aa from? I'm guessing /proj/ is not your working directory and it would need to be as it's setup right now. Instead of PATH, you would want to add your directory to PYTHONPATH so it appears in the search path for an import. See http://docs.python.org/tutorial/modules.html#the-module-search-path
Also, please take a look at http://as.ynchrono.us/2007/12/filesystem-structure-of-python-project_21.html It strongly recommends you put an extra level of directory in place so instead of lib.aa, you would refer to it as my_proj.lib.aa.
I had similar problems and here is my advice.
Instead of changing sys.path, better run your test.py from being in proj (i.e. project root) directory. This way project dir will automatically be in sys.path and you will be able to import lib package.
And use absolute imports.
System PATH variable is not used by python import statement. It uses PYTHONPATH, but best way to add new directory to import search path is to modify sys.path.
If this does not help, add to the question your value of sys.path and value returned by os.getcwd().
In Sublime Text 3, abspath it didn't work for me.
I use this instead in the top "__ init __.py" file
Hope it works for you.
from os.path import dirname
from sys import path
path.insert( 0 , dirname( __file__ ) ) ;
from test import aa_test
I have a python file "testHTTPAuth.py" which uses module deliciousapi and is kept in "deliciousapi.py".
I have kept the files like
testHTTPAuth.py
lib
deliciousapi.py
But when i run: "python testHTTPAuth.py" it's giving error
import deliciousapi
ImportError: No module named deliciousapi
How can handle these python libraries? Because later I have put the code together with libraries as Google app. So I can't keep the library in normal library path.
You need to add the 'lib' directory to your path - otherwise, Python can't find your source. The following (included in a module such as testHTTPAuth.py) will do that:
sys.path.append(os.path.join(os.path.dirname(__file__), 'lib')
Ned's suggestion of changing your imports may work, but if anything in the lib directory imports submodules with absolute paths (most large modules do this), then it'll break.
If you add an empty __init__.py to your lib directory, you can change your import statement to:
from lib import deliciousapi