Few my python packages are available in NFS share which gets mounted on my machine
Ex:
mount server:/exports /localmount
python package module PATH: /localmount/dir1/dir2/mypackages/hub
When I try to import the package from "/localmount/dir1/dir2/mypackages/hub", it says.
ImportError: No module named localmount
But If change the directory to "/localmount" its working
Please note: All directories have __init__.py file recursively.
code snippet:
>>> from localmount.dir1.dir2.mypackages.hub import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named localmount.dir1.dir2.mypackages.hub import *
>>>
>>> import os
>>> os.chdir('/localmount')
>>>
>>> from dir1.dir2.mypackages.hub import *
>>>
Please advise how to import from package without moving the dir : localmount
Here is the fix :
Added the NFS mount path to os.path, the issue got resolved.
>>> import os
>>> os.path.append('/localmount/dir1/dir2/mypackage')
>>> from hub import p1
Related
I try to import some custom modules/packages but get the error "ModuleNotFoundError: No module named 'reader'".
I made sure that the absolute path to my custom package directory is listed in sys.path:
>>> os.getcwd()
'C:\\Python Projects\\reader'
>>> sys.path.append('C:\\Python Projects\\reader')
>>>
>>> sys.path
['', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages', 'C:\\Python Projects\\reader']
>>>
>>> import reader
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'reader'
Also, all the paths listed in sys.path (except for '' and C:\Python Projects\reader) have been added in Environment variables > System variables (I am using Win10).
Is there something else I should to in order to successfully import custom packages in my projects?
Thank you
sys.path.append('C:\\Python Projects')
Then import reader will look for reader under 'C:\Python Projects', i.e. it will test for 'C:\Python Projects\reader'. Currently it's testing for 'C:\Python Projects\reader\reader'.
I'm having some troubles reloading a module that is in another directory but is in sys.path.
>>> from module_from_another_dir import *
>>> from importlib import reload
>>> reload(module_from_another_dir)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'module_from_another_dir' is not defined
So as you can see I can import it, but I can't reload it later.
When you import module like this
from module_from_another_dir import *
then Python knows only new methods and "forgets" about module name. However, you can import your module as
import module_from_another_dir
and you will be able to reload it easily.
I have a package my_scripting_library I want to use anywhere on machine. It has an init.py:
from silo_functions import *
and looks like
my_scripting_library
-__init__.py
-silo_functions.py
-test_stuff.py
test_stuff.py looks like:
#!/usr/bin/env python
from silo_functions import *
lines = read_lines('filepath.py')
print lines
in bashrc:
export PYTHONPATH="${PYTHONPATH}:$LIBRARY"
where LIBRARY is a correct filepath to my_scripting_library
In [1]: import sys
In [2]: sys.path
Out[2]:
['',
'/usr/bin',
'/usr/lib/python2.7/site-packages/lxml-3.3.3-py2.7-linux-x86_64.egg',
'/home/cchilders/scripts/python/my_scripting_library',
...
'/home/cchilders/.ipython']
running test_stuff with from .silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from .silo_functions import *
ValueError: Attempted relative import in non-package
running test_stuff with from my_scripting_library.silo_functions import * causes:
Traceback (most recent call last):
File "./test_stuff.py", line 3, in <module>
from my_scripting_library.silo_functions import *
ImportError: No module named my_scripting_library.silo_functions
but running test_stuff with from silo_functions import * works:
it prints the lines
Of course I can't use this package from other folders, which is the real issue- I don't want to be forced into throwing all scripts in this one place. This is causing huge problems as I am constantly reusing dozens of functions each script, and over 5 tutorials on making a folder a python package never have worked yet. Why is something on the python path with an init not a package? Thank you
May be it is because you've added '.../python/my_scripting_library' to your path. But there are no 'my_scripting_library.py' at this folder.
If you want to use 'my_scripting_library.silo_functions', try to add '/home/cchilders/scripts/python' (not '/home/cchilders/scripts/python/my_scripting_library') to path.
Because 'my_scripting_library' is module. Python will find this folder, find __init__.py in this folder and mark it as module.
when i am importing some modules from python shell, import work fine:
for example: (link for propy module https://code.google.com/p/protpy/downloads/list)
>>> import propy
>>>
but when i write script with python Default IDLE or with other IDE and save it to as .py script , import statements not work and generate error like this
python fragment_generator.py
>>>
Traceback (most recent call last):
File "J:\acetylome scripts\New folder\fragment_generator.py", line 1, in <module>
import propy
ImportError: No module named propy
>>>
please solve it
thanks in advance:
When you run the shell from the same directory the files are in the import will work. You are probably working from another directory when the import fails.
you can add a directory to the python path like this.
befor your import:
import sys
sys.path.append('/path/to/your/folder')
import propy
good luck.
I try to run Sympy, a library for Python but I got a problem...
When I import Sympy in the console
>>> import os
>>> from os import chdir
>>> chdir("C:/sympy-0.7.2")
>>> import sympy
>>>
It works, but if I make a script with this content...ERROR!
Why ?
This error
Traceback (most recent call last):
File "**", line 4, in <module>
import sympy
ImportError: No module named sympy
try this..
import sys
sys.path.append("C:/sympy-0.7.2")
import sympy
Run the script from C:/sympy-0.7.2.
Better yet, install sympy. It will go into your site-packages directory
and will be available from anywhere. Going into C:/sympy-0.7.2 and typing python setup.py install should work.