Jython ImportError: No module named * (only on Windows Server 2012) - python

I have a CreateDomain.py script for weblogic. And now I want to import a java class in the python script. I exported the class com.swisslog.python.util.FileNameUtil to a jar and added it to the class path before calling the python script.
Here's the start of the script:
from wlstModule import *
from java.io import FileInputStream
from java.util import Properties
import jarray.array
import os
import shutil
import traceback
import sys
import getopt
from com.swisslog.python.util import FileNameUtil
I start the script from a bat file:
set CLASSPATH=%CLASSPATH%;wm6-python-util.jar
%MW_HOME%\wlserver\common\bin\wlst.cmd common\CreateDomain.py %1 %2 %3 %4
This works on Windows 7. But on Windows Server 2012 I get following error:
ImportError: no module named swisslog
Also using the full path doesn't work (jython ImportError: No module named)
Any help would be appreciated.

I found a stupid workaround. I added a main method which calls the real methods and prints the result with system.out.print. Then I can start the jar from the python script without an import:
result = os.popen(['java', '-jar', 'python-util.jar', 'arg').read()

Related

No module named Linq

I wrote a python script to use in C#, but when I run my project this error occurs: "No module named Linq". what should I do? I took the import part from another part of project from TFS and it works well there, so what is going on?
import System
import clr
import sys
clr.AddReference(''System.Core'')
from System import Array
from System import DateTime
from System.Linq import Enumerable
from System import Func
Check your single vs double quotes.
But try "ImportExtensions"
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

Python3 - ImportError: No module named

I have 2 folders:
my_python
code.py
MyCode
TestEntry.py
When I run the following commands:
cd /data/my_python
python3 code.py
The above works.
However, if I in my home folder and then run this:
python3 /data/my_python/code.py
I get the following error:
Traceback (most recent call last):
File "/data/my_python/code.py", line 4, in <module>
from TestEntry import TestEntry
ImportError: No module named 'TestEntry'
Here is the code:
import sys
import os
sys.path.append(os.path.abspath('../MyCode'))
from TestEntry import TestEntry
TestEntry().start(507,"My Param1","/param2",'.xyz',509)
Can you help me how to fix this?
You are adding a relative path to sys with your line sys.path.append(os.path.abspath('../MyCode')). Instead, you need to import relative to that file you are calling. Try this:
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from TestEntry import TestEntry
TestEntry().start(507, "My Param1", "/param2", '.xyz', 509)
That happens because, as #mkrieger1 mentioned, your sys.path gets messed up. I have a previous answer here which explains how to set it. By sys.path getting messed up, I mean that python will look in the dir that you are running from, not the dir that the script you are running is in. Here is the recommended method:
import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'MyCode')))
... (your code)
or
import sys, os
sys.path.append(os.path.abspath(os.path.join(__file__, '..', 'MyCode')))
... (your code)
This way python will look in the dir of the file you are running as well.

Import error even though file is clearly in path

I am running a script and I am getting the error:
Traceback (most recent call last):
File "common/tensorflow/run_tf_benchmark.py", line 30, in <module>
from common.base_benchmark_util import BaseBenchmarkUtil
ModuleNotFoundError: No module named 'common'
I am running on an ec2 instance with the following AMI: Deep Learning AMI (Ubuntu 16.04) Version 25.0 (ami-025c308193ac1a136) and I am also working in the tensorflow_p36 anaconda environment that comes from the AMI. My Ubuntu directory is structured as so:
home
--ubuntu
--benchmark_models
--benchmark
--common
--__init__.py
--base_benchmark_util.py
--tensorflow
--run_tf_benchmark.py
--__init__.py
Note, there are other files in the directory, but these are the relevant python files.
Currently, the imports at the top of run_tf_benchmark.py are
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append('/home/ubuntu/benchmark_models/benchmarks/common')
print(sys.path)
from argparse import ArgumentParser
from common.base_benchmark_util import BaseBenchmarkUtil
and the printed sys.path is
['/home/ubuntu/benchmark_models/benchmarks/common/tensorflow', '/home/ubuntu/src/cntk/bindings/python',
'/home/ubuntu/benchmark_models/models/image_recognition/tensorflow/mobilenet_v2', '/home/ubuntu/models',
'/home/ubuntu/models/research', '/home/ubuntu/models/research/slim',
'/home/ubuntu/anaconda3/lib/python36.zip', '/home/ubuntu/anaconda3/lib/python3.6',
'/home/ubuntu/anaconda3/lib/python3.6/lib-dynload', '/home/ubuntu/anaconda3/lib/python3.6/site-packages',
'/home/ubuntu/benchmark_models/benchmarks/common']
I can clearly see common in the python path, but the import error still happens. What am I doing wrong?
Just remove 'common' while importing because you are already in 'common' directory
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
sys.path.append('/home/ubuntu/benchmark_models/benchmarks/common')
print(sys.path)
from argparse import ArgumentParser
from base_benchmark_util import BaseBenchmarkUtil
Alright, I solved the issue. The issue was that I added /home/ubuntu/benchmark_models/benchmarks/common, not /home/ubuntu/benchmark_models/benchmarks/. By import benchmarks, it also allowed all of my other code to import common as well.

Importing test libraries failed. No module named 'a'

I have a folder /example which contains /Libs which further contains different folders /a, /b each containing python libraries. I am trying to run a robot framework code from /example.
The error it shows :
Importing test library 'a' failed: ImportError: No module named 'a'
File "/root/Libs/a/init.py", line 7, in
from a import a_classname
How can I solve this?
import os
import sys
filepath = "path/file/"
sys.path.append(os.path.abspath(filepath))
from a import a_classname

How to import a function from a module in the same folder? [duplicate]

This question already has answers here:
import function from a file in the same folder
(4 answers)
Closed last month.
I am trying to separate my script into several files with functions, so I moved some functions into separate files and want to import them into one main file. The structure is:
core/
main.py
posts_run.py
posts_run.py has two functions, get_all_posts and retrieve_posts, so I try import get_all_posts with:
from posts_run import get_all_posts
Python 3.5 gives the error:
ImportError: cannot import name 'get_all_posts'
Main.py contains following rows of code:
import vk
from configs import client_id, login, password
session = vk.AuthSession(scope='wall,friends,photos,status,groups,offline,messages', app_id=client_id, user_login=login,
user_password=password)
api = vk.API(session)
Then i need to import api to functions, so I have ability to get API calls to vk.
Full stack trace
Traceback (most recent call last):
File "E:/gited/vkscrap/core/main.py", line 26, in <module>
from posts_run import get_all_posts
File "E:\gited\vkscrap\core\posts_run.py", line 7, in <module>
from main import api, absolute_url, fullname
File "E:\gited\vkscrap\core\main.py", line 26, in <module>
from posts_run import get_all_posts
ImportError: cannot import name 'get_all_posts'
api - is a api = vk.API(session) in main.py.
absolute_url and fullname are also stored in main.py.
I am using PyCharm 2016.1 on Windows 7, Python 3.5 x64 in virtualenv.
How can I import this function?
You need to add __init__.py in your core folder. You getting this error because python does not recognise your folder as python package
After that do
from .posts_run import get_all_posts
# ^ here do relative import
# or
from core.posts_run import get_all_posts
# because your package named 'core' and importing looks in root folder
MyFile.py:
def myfunc():
return 12
start python interpreter:
>>> from MyFile import myFunc
>>> myFunc()
12
Alternatively:
>>> import MyFile
>>> MyFile.myFunc()
12
Does this not work on your machine?
Python doesn't find the module to import because it is executed from another directory.
Open a terminal and cd into the script's folder, then execute python from there.
Run this code in your script to print from where python is being executed from:
import os
print(os.getcwd())
EDIT:
This is a demonstration of what I mean
Put the code above in a test.py file located at C:\folder\test.py
open a terminal and type
python3 C:\folder\test.py
This will output the base directory of python executable
now type
cd C:\folder
python3 test.py
This will output C:\folder\. So if you have other modules in folder importing them should not be a problem
I usually write a bash/batch script to cd into the directory and start my programs. This allows to have zero-impact on host machines
A cheat solution can be found from this question (question is Why use sys.path.append(path) instead of sys.path.insert(1, path)? ). Essentially you do the following
import sys
sys.path.insert(1, directory_path_your_code_is_in)
import file_name_without_dot_py_at_end
This will get round that as you are running it in PyCharm 2016.1, it might be in a different current directory to what you are expecting...

Categories

Resources