I am trying to use Lumerical in python. Lumerical is an electromagnetic simulation software that allows access via python. For more information, please refer to https://support.lumerical.com/hc/en-us/articles/360041873053
Anyway, to make it run, I need to import a module called lumapi. It's located in the program's directory:
C:\Program Files\Lumerical\v202\api\python\lumapi.py
So, naturally, to import it into my current script file, I used the following code:
import sys
sys.path.append(r"C:\Program Files\Lumerical\v202\api\python\lumapi.py")
import lumapi
But I got:
ModuleNotFoundError: No module named 'lumapi'
What I have tested
When I save the script file directly inside the folder where
lumapi is stored and just write import lumapi it works.
With sys.path I have tested if the directory was indeed added to
the path, and it seems to be the case
I also made sure that I have full write and read permission to the
folder where lumapi is stored
Related
I created a py file called orderbook.py which contains a class called OrderbookLoader. Everything is in a directory called loaders. I added that path to the ipythonpath, so the following: /home/ec2-user/SageMaker/research/data_manager/loaders
In my directory I have multiple other files as well:
But when I go my root directory and try the following:
from loaders.orderbook import OrderbookLoader it throws the following error message: ModuleNotFoundError: No module named 'loaders.orderbook'; 'loaders' is not a package
But I can do the following:
from loaders import OrderbookLoader which is a class from the loaders.py file. Do you have any idea what I did wrong? thanks!
This has to do with interactions between your work directory (not specified in your question), the Python path (also not specified), and the files in the supposed package directory.
To make a directory a package, you should:
Ensure that is resides directly a directory that is in the Python path (sys.path).
Ensure that it includes a file named __init__.py (which you did). That file may be empty, or contain legal Python code.
What is probably happening here is that you made the directory loaders, itself, part of the Python path. If you added its parent directory (as you should have), it was probably placed after loaders. What happens next is that when Python tries to import loaders, it imports the file loaders.py inside the loaders directory, before finding the loaderspackage directory inside its parent. This means that loaders, specified to the import mechanism, refers to the module, rather than the package, hence the results of your two import attempts.
This placement of a module inside a package, both with the same name, is not a good idea, as it may lead to the situation you encounter here.
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 use python 3.5 with Anaconda (python 3.6) and I have a problem with importing a class within the same folder, even though I did exactly as explained in other places.
In 04-Convolutional Neural Network Folder I have
04-Convolutional Neural Network.ipynb and logger.ipynb files.
I want to import Logger class in logger.ipynbto 04-Convolutional Neural Network.ipynb
First, I created a blank __init__.ipynb file in the same folder, I used:
from .user import User
from .dir import Dir
But I get the following error
No module named '__main__.user'; '__main__' is not a package
Any idea why do I have this problem? maybe because of the ipynb file system?
Edit: The files are in Desktop/NN/.... if it's important
It seems to refer to the file your running (namely main). I don't use relative import much. But if user and dir are in the same directory as the file your running you should try (i.e. they are in the root of your python module):
from user import User
from dir import Dir
You should probably also rename the dir module, as dir is a builtin function in python.
I came across a Python 'ImportError: No module named...' error attempting to import a Python module which resides in another Python package from a module which resides within another Python package. The following image shows the directory structure:
It has to be noted that I get this error only when I run the script from my terminal whereas when executing through PyCharm, this script successfully runs. The error when executing from terminal is as follows:
Traceback (most recent call last):
File "social_networks/linked_data.py", line 15, in <module>
from text_analysis.text_refinement import camel_case_split
ImportError: No module named 'text_analysis'
I have tried different ways of importing such as the following without any success:
Method-1:
sys.path.insert(0, os.path.realpath('../text_analysis'))
from text_analysis.text_refinement import camel_case_split
Method-2:
from text_analysis.text_refinement import camel_case_split
What is the solution for this issue?
Short version
Change it to:
sys.path.insert(0, os.path.realpath('./'))
from text_analysis.text_refinement import camel_case_split
Or:
sys.path.insert(0, os.path.realpath('./text_analysis'))
from text_refinement import camel_case_split
Long version
I have recreated your project structure on my machine and managed to make it work. Let's go step by step, so that we can figure out what's happening.
First of all, I see you're working on your project in PyCharm. It automatically adds a project root to PYTHONPATH. You can read about this in detail in this thread. Since PyCharm takes care of path business for you, you do not really need
sys.path.insert(0, os.path.realpath('../text_analysis'))
for your code to run. The path will still be added, but it will not be used to locate the package. Try it on your machine. I think you will find it to be true. You can easily check out paths by running
for path in sys.path:
print(path)
While this is interesting information, it does not answer your question how to run it from a terminal. To understand why it is not run from the script, lets see what Python paths you would have upon executing the (slightly modified) commands in method-1:
sys.path.insert(0, os.path.realpath('../text_analysis'))
try:
from text_analysis.text_refinement import camel_case_split
camel_case_split()
except:
for path in sys.path:
print(path)
# output:
# ~/text_analysis (where ../text_analysis path points to)
# ~/social-network-interest-engine/social_networks (where your file is)
# ... (several other irrelevant paths) ...
We can see that '../text_analysis' points one directory above what you need. What would happen if we deleted one of the full stops, and instead wrote './text_analysis'? Output seems to be what we need:
# output:
# ~/social-network-interest-engine/text_analysis
# ~/social-network-interest-engine/social_networks
But we still have not imported the function. We know this because we reach the except part, which prints the paths. Looking at import, we can see that we have text_analysis.text_refinement. Do we really need to state the directory name if we already added it to the path? No, we do not. If we write
from text_refinement import camel_case_split
instead, we find that the function has finally been imported. Following this logic, and assuming we wanted to leave text_analysis.text_refinement in import statement (for whatever reason), we could add path differently, too:
sys.path.insert(0, os.path.realpath('./'))
Note, however, that this way of inserting the path is somewhat brittle. The starting location is the path from which you call python python_file.py If you navigated to different directory, you would need to adjust os.path.realpath accordingly. What you can do instead:
sys.path.insert(0, 'full/path/to/application/app/folder')
Though this assumes that the directory/structure of your project will not change.
For a more in-depth overview of paths and imports, you can read more about importing stuff from different folders here, and if you prefer relative path imports, this is a useful thread. Of course, official documentation is also a good place to start.
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