module import in windows - python

I am new to windows and I am working on setting up code and modules with visual studio.
I have the following folder structure for my code:
myModule
__init__.py
mymodule.py
myScript
myscript.py
In myscript.py I have the following command:
from myModule import *
In visual studio this command works, but when I run the script command line I get the following error:
ModuleNotFoundError: No Module named myModule
Is there a quick trick in windows to do the job w/o having to install myModule as a package?
Any of the usual tricks that work in linux don't seem to work in windows. I.e.,
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'myModule'))
or,
import ..myModule

The path should have entries that point to the module. Your path entry is pointing inside the module.
What you had:
os.path.join(os.path.dirname(__file__), r"..", 'myModule')
Instead, you want to point to the directory containing myModule, which would be:
os.path.join(os.path.dirname(__file__), r"..")

Related

python module not found error no module named

I have a few seperate pythone file and I am using them to import another py file. Modules that trying to import them are in seperate folder I code sample is below
from tez.library.image_crop import ImageCrop
from tez.library.image_process import ImageProcess
from tez.library.image_features import ImageFeatures
from tez.const.application_const import ApplicationConst
from tez.library.file_operation import FileOperation
this code is in where I want to start the py file using commond line as "python samples1.py" and thrown an error as below
Traceback (most recent call last): File "samples1.py", line 1, in
from tez.library.image_crop import ImageCrop ModuleNotFoundError: No module named 'tez'
folder structure :
.tez
-- library
---- image_crop.py
---- image_process.py
---- image_features.py
--src
---- samples1.py
Python version : 3.8
Pip : 20.0.2
Windows 10 Pro 1909
If you are building a package called tez (and since you tried to import it I think you are). Then everything with tez needs to refer to itself locally. All the files in the tez package need to refer to each other with the "." and ".." imports.
In samples1.py:
from ..library.image_crop import <something>
EDIT:
It sounds like you are misunderstanding how python imports things. When you run "import X" in a python script, then python looks for a package named X under sys.path. You can append to sys.path at the top of your script if you have a custom package to look for.
import sys
sys.path.append(<directory of tez>)
import tez
However, it is strongly recommended that you should not be importing from a file that is under the directory structure of the package name. If "examples" is a directory of examples that use the package "tez" then "examples" should be located outside the package "tez". If "examples" is inside the package "tez", then "examples" should be doing local imports "with-in" the package.
Getting a handle on package use can be tricky.
sample.py can't see above of src folder, but you can tell Python to do this.:
import sys
import os
tez = os.path.dirname(os.path.dirname(__file__))
# __file__ is path of our file (samples.py)
# dirname of __file__ is "src" in our state
# dirname of "src" is "tez" in our state
sys.path.append(tez) # append tez to sys.path, python will look at here when you try import something
import library.image_crop # dont write "tez"
But this is not a very good design I think.

Python submodules not recognized when run in unix

I have a python/pyspark project with the following structure:
project
__ini__.py
module1
__ini__.py
file1.py
file_run1.py
module2
__ini.py
file2.py
file_run2.py
shared_allmodules
__ini__.py
func1.py
func2.py
File_run1.py:
from shared_allmodules import func1, func2
from module1 import file1
File2.py:
from shared_allmodules import func2
I have thia structure in CDSW and it works there. But now i have to move all the files into a unix server and run from there.
But when i run
spark2-submit file_run1.py
From module1 directory i have an error that "no module named shared_allmodules".
I'm new in python/pyspark and i don't know what i have to do so that my submodules to be recognized in unix.
I don't have a main.py because i don't know how to use it.
Also i don't have the condition with if name=main.
My py files have a lot of pyspark code, i just wrote here a part of the directories structure.
Do you know what i have to do in order to run py files in unix that import modules from other directories?
You need to specify the environment variable PYTHONPATH which defines visible for the python interpreter directories (ones outside site-packages) or install your modules in the system using setuptools [1].
Example:
export PYTHONPATH=/path/to/project:$PYTHONPATH

py2exe ImportError: no module named <package I have impemented>

I have implemented a package names myUtils, it consists of folder 'myUtils', file 'init.py' and a number of *.py files with names != 'myUtils'. This package is included in myOtherProject.py and can be found/used when I run them from Eclipse.
However, when I run py2exe on myOtherProject.py, resulting exe cannot find this module(error message "ImportError: no module named myUtils"). Trimmed version of my setup.exe:
from distutils.core import setup
import py2exe, sys
sys.path.append(pathTo_myUtils)
import myUtils # this line works fine even if I comment out sys.path.append(...)
data_files_ = (('.', ["C:\\Python27\\DLLs\\MSVCP90.dll",
"C:\\Python27\\lib\\site-packages\\Pythonwin\\mfc90.dll"]))
setup(windows=['myOtherProject.py'], options={'py2exe': {'excludes': ['tcl'], 'includes': ['myUtils'], 'dll_excludes': ['tk85.dll', 'tcl85.dll'] }}, data_files=data_files_)
How could I fix this? I am using Python 2.7 on WinXP.
put your sys.path.append() line BEFORE the import statement. Better yet, modify your PYTHONPATH (i'm not sure how to do this on windows, but i'm sure Google can tell you how)
I did not define PYTHONPATH properly; there were spaces after semicolons. Instead of
c:\aa\; c:\bb\; c:\cc\
it needed to be
c:\aa;c:\bb;c:\cc
For packages that are defined using init.py (package MyPackage corresponds to a folder MyPackage, that contains init.py and some other files, without MyPackage.py), path that I needed to add to PYTHONPATH was not
<path_to_MyPackage>\MyPackage
but just
<path_to_MyPackage>
...

ImportError: No module named - Python

I have a python application with the following directory structure:
src
|
+---- main
|
+---- util
|
+---- gen_py
|
+---- lib
In the package main, I have a python module named MyServer.py which has an import statement like:
from gen_py.lib import MyService
In order for this statement to work, I placed the following line at the beginning of MyServer.py:
import sys
sys.path.append('../gen_py/lib')
When I run MyServer.py in the terminal, I get the following error:
ImportError: No module named gen_py.lib
What I am missing here?
Your modification of sys.path assumes the current working directory is always in main/. This is not the case. Instead, just add the parent directory to sys.path:
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import gen_py.lib
Don't forget to include a file __init__.py in gen_py and lib - otherwise, they won't be recognized as Python modules.
For the Python module import to work, you must have "src" in your path, not "gen_py/lib".
When processing an import like import gen_py.lib, it looks for a module gen_py, then looks for a submodule lib.
As the module gen_py won't be in "../gen_py/lib" (it'll be in ".."), the path you added will do nothing to help the import process.
Depending on where you're running it from, try adding the relative path to the "src" folder. Perhaps it's sys.path.append('..'). You might also have success running the script while inside the src folder directly, via relative paths like python main/MyServer.py
from ..gen_py.lib import MyService
or
from main.gen_py.lib import MyService
Make sure you have a (at least empty) __init__.py file on each directory.
make sure to include __init__.py, which makes Python know that those directories containpackages
This is if you are building a package and you are finding error in imports. I learnt it the hard way.The answer isn't to add the package to python path or to do it programatically (what if your module gets installed and your command adds it again?) thats a bad way.
The right thing to do is:
1) Use virtualenv pyvenv-3.4 or something similar
2) Activate the development mode - $python setup.py develop
Make sure if root project directory is coming up in sys.path output.
If not, please add path of root project directory to sys.path.

How to import a module from a directory on level above the current script

For my Python application, I have the following directories structure:
\myapp
\myapp\utils\
\myapp\utils\GChartWrapper\
\myapp\model\
\myapp\view\
\myapp\controller\
One of my class in \myapp\view\ must import a class called GChartWrapper. However, I am getting an import error...
myview.py
from myapp.utils.GChartWrapper import *
Here is the error:
<type 'exceptions.ImportError'>: No module named GChartWrapper.GChart
args = ('No module named GChartWrapper.GChart',)
message = 'No module named GChartWrapper.GChart'
What am I doing wrong? I really have a hard time to import modules/classes in Python...
The __init__.py file of the GChartWrapper package expects the GChartWrapper package on PYTHONPATH. You can tell by the first line:
from GChartWrapper.GChart import *
Is it necessary to have the GChartWrapper included package in your package directory structure?
If so, then one thing you could do is adding the path where the package resides to sys.path at run time. I take it myview.py is in the myapp\view directory? Then you could do this before importing GChartWrapper:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'utils')))
If it is not necessary to have it in your directory structure, it could be easier to have it installed at the conventional location. You can do that by running the setup.py script that's included in the GChartWrapper source distribution.
You don't import modules and packages from arbritary paths. Instead, in python you use packages and absolute imports. That'll avoid all future problems.
Example:
create the following files:
MyApp\myapp\__init__.py
MyApp\myapp\utils\__init__.py
MyApp\myapp\utils\charts.py
MyApp\myapp\model\__init__.py
MyApp\myapp\view\__init__.py
MyApp\myapp\controller\__init__.py
MyApp\run.py
MyApp\setup.py
MyApp\README
The files should be empty except for those:
MyApp\myapp\utils\charts.py:
class GChartWrapper(object):
def __init__(self):
print "DEBUG: An instance of GChartWrapper is being created!"
MyApp\myapp\view\__init__.py:
from myapp.utils.charts import GChartWrapper
def start():
c = GChartWrapper() # creating instance of the class
MyApp\run.py:
from myapp.view import start
start()
That's all! When you run your entry point (run.py) it calls a function on the view, and that creates an instance of the GChartWrapper class. Using this structure you can import anything anywhere and use it.
To complement, in MyApp\setup.py you write an installation program for the MyApp\myapp package. Use distutils to write it:
from distutils.core import setup
setup(name='MyApp',
version='1.0',
description='My Beautiful Application',
author='Martin',
author_email='martin#xxxxxxx.com',
url='http://stackoverflow.com/questions/1003843/',
packages=['myapp'],
scripts=['run.py']
)
That is enough. Now when people download the MyApp folder, they can just install it using setup.py and run it using run.py. Distutils can generate packages in a number of formats including windows installable .EXE
It's the standard way of distributing python packages/applications.
You can change the path where python looks for files.
At the top of your source file, add:
import sys
sys.path.append("..")
Or alternatively change the environment variable:
export PYTHONPATH=..
Or starting in python 2.5 (again assuming myview is in myapp\view:
from __future__ import absolute_import
from ..utils.GChartWrapper import *
See: http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
GChartWrapper is also available from PyPI so you can use easy_install or pip to install the module:
sudo pip install GChartWrapper==0.9
It will then be automatically added to your PYTHONPATH and then you can remove it from your /myapp/utils directory. If you can't use sudo, look at using virtualenv (and virtualenvwrapper).

Categories