Run python script from another python script on linux - python

I'm using python 2.7 and ubuntu 16.04.
I have a simple REST server on python: file server_run.py in module1 which is importing some scripts from module2.
Now I'm writing an integration test, which is sending POST request to my server and verify that necessary action was taken by my server. Obviously, server should be up and running but I don't want to do it manually, I want to start my server (server_run.py which has also main method) from my test: server_run_test.py file in module3.
So, the task sounds very simple: I need to start one python script from another one, but I spent almost the whole day. I found a couple of solutions here, including:
script_path = "[PATH_TO_MODULE1]/server_run.py"
subprocess.Popen(['python', script_path], cwd=os.path.dirname(script_path))
But my server is not coming up, throwing the error:
Traceback (most recent call last):
File "[PATH_TO_MODULE1]/server_run.py", line 1, in <module>
from configuration.constants import *
File "[PATH_TO_MODULE2]/constants.py", line 1, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
So, it looks like when I'm trying to start my server in subprocess it doesn't see imports anymore.
Do you guys have any idea how can I fix it?

Eventually, the solution was found, 2 steps were taken:
1. In each module I had an empty __init__.py file, it was changed to:
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
__version__ = '${version}'
2. Instead of using the following syntax:
from configuration.constants import *
from configuration.config import *
config and constants were imported as:
from configuration import constants,config
and then we are using reference to them when need to get some constant.
Thanks everyone for looking into it.

Rather than running it using os module try using the import function. You will need to save in the same dictionary or a sub folder or the python installation but this seems to be the way to do it. Like this post suggests.

Related

How to use a Python function defined in a separate file in a Python chunk in Rmarkdown?

So, I have a file called 'function.py' which includes the simple function:
def square(x):
return x*x
I have a second bit of code like this:
from test import square
print(square(2))
If I store the second bit of code in a python file and run it in the terminal it works and gives the expected answer.
However, if I add a Python chunk in a Rmarkdown document like this:
```{python}
from test import square
print(square(2))
```
I get the error:
"Traceback (most recent call last):
File "/var/folders/g7/462tmml173nfzj0j8437t9_m0000gn/T/RtmptMA22N/chunk-code-48764cec023f.txt", line 1, in
from test import square
ImportError: cannot import name square"
The Rmarkdown file and the python file are in the same directory. Answers about the specific error message are about dependencies, but I don't see how that's relevant in my case?
I've searched the web and read documentation, but I think I am missing something important. Thank you for the help!
Edit:
Solved by specifically changing the path to the current working directory.
import sys, os
sys.path.append(os.getcwd())
import test
print(test.square(2))
Solved by specifically changing the path to the current working directory.
import sys, os
sys.path.append(os.getcwd())
import test
print(test.square(2))
Answer was based on Python: Best way to add to sys.path relative to the current running script.

Reload function only works once (Python)

I am working with some code in which I have to access functions that are stored in a directory other than the rest of python's modules (my script is in C:/path/M461/DataMapping, the module is in C:/path/M461/ModuleDir and is named functions.py - original, I know). My prof said that using importlib.reload was necessary to use the functions, but I'm having a technical error with reload. Here is my code:
parentDir = r'C:/path/M461/'
if parentDir not in set(sys.path):
sys.path.append(parentDir)
from ModuleDir import functions
dir(functions)
import importlib
importlib.reload(functions)
fieldDict = functions.fieldDictBuild()
When I run it, the very first time it works perfectly. Any subsequent attempts to run the file throw the error:
File "C:\Users\Kristen\Anaconda3\lib\importlib\__init__.py", line 147, in reload
raise ImportError(msg.format(name), name=name)
ImportError: module ModuleDir.functions not in sys.modules
The only workaround I have found is to completely restart the kernel every time I run the code, which is getting annoying. Is there a way to fix this permanently? Is it a problem with my code or with the reload module itself? And why is it necessary to reload functions at all?

No module named

I have a python project with this structure: (This is not a real project, only for testing)
ImportTest
ImportPersonsTest\
ImportPerson\
ImportPerson.py
RunImportPersonTest.py
RunImportTests.py
I want this tests to call each other. E.g :
RunImportTests.py calls a method in RunImportPersonTest.py, and RunImportPersonTest.py calls a method ImportPerson.py
RunImportPersonTest:
import os
import sys
sys.path.insert(0, os.getcwd() + "../../../")
from ImportPerson import ImportPerson
RunImportTests
import os
import sys
sys.path.insert(0, os.getcwd() + "../../")
from ImportPersonsTest import RunImportsPersonTest
I have success when I run ImportPerson.py and RunImportPersonTest.py, but when I try to run RunImportTests I get this error :
Traceback (most recent call last):
File "xxx\LiClipse Workspace\SystemTest\ImportTest\RunImportTests.py", line 4, in <module>
from ImportPersonsTest import RunImportsPersonTest
File "xxx\LiClipse Workspace\SystemTest\ImportTest\ImportPersonsTest\RunImportsPersonTest.py", line 4, in <module>
from ImportPerson import ImportPerson
ImportError: No module named 'ImportPerson'
Any suggestions?
Edit
New Structure
ImportTest
ImportPersonsTest\
ImportPerson\
ImportPerson.py
__init__.py
RunImportPersonTest.py
__init__.py
RunImportTests.py
__init__.py
Looks like you don't have any __init__.py files in your project. Python needs those files to be able to import modules from folders. The good news is, they are very easy to make: most of the time, they don't need anything in them, they just have to exist.
See: https://docs.python.org/2/tutorial/modules.html#packages
I think your use of os.getcwd() is flawed.
My guess is that you're running your program from the ImportTest directory and so your current working directory will already allow you to do the first import without any need to fix up your path. When you then try the second import, adding ".../ImportTest/../../.." or ".../ImportTest/../.." isn't helping Python find it.
To fix it either add the ImportPersonsTest directory to your path or use a suitably modified name in the import (ensuring you have your init files as already flagged) - e.g.
from ImportPersonsTest.ImportPerson import ImportPerson
There is two basic problem:
os.getcwd() as other os functions return path with no separator at the end. In fact you insert xxx\LiClipse Workspace\SystemTest\ImportTest../../../ which is not a valid path
As mention by #peter, using os.getcwd() is bad idea - it's depend on your location when you run the script. Use:
sys.path.append(os.path.dirname(__file__)) (insert recommend only at special cases)
But,
It seems that none of this caused your problem. It's only insert bad stuff to your sys.path. Your importing need to work good cause all the importing done from the self-module-dir, where python firstly search for the requested module.
I copy your package to mine machine - and both runs well!
I fix one spelling bug (RunImportsPersonTest -- RunImportPersonTest) - maybe there is other spelling problem

Python name 'os' is not defined even though it is explicitly imported

I have a module called imtools.py that contains the following function:
import os
def get_imlist(path):
return[os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
When I attempt to call the function get_imlist from the console using import imtools and imtools.get_imlist(path), I receive the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\...\PycharmProjects\first\imtools.py", line 5, in get_imlist
NameError: name 'os' is not defined
I'm new at Python and I must be missing something simple here, but cannot figure this out. If I define the function at the console it works fine. The specific history of this module script is as follows: initially it was written without the import os statement, then after seeing the error above the import os statement was added to the script and it was re-saved. The same console session was used to run the script before and after saving.
Based on small hints, I'm going to guess that your code didn't originally have the import os line in it but you corrected this in the source and re-imported the file.
The problem is that Python caches modules. If you import more than once, each time you get back the same module - it isn't re-read. The mistake you had when you did the first import will persist.
To re-import the imtools.py file after editing, you must use reload(imtools).
Same problem is with me I am also trying to follow the book of Programming Computer Vision with Python by Jan Erik Solem" [http://programmingcomputervision.com/]. I tried to explore on internet to see the problem but I did not find any valuable solution but I have solved this problem by my own effort.
First you just need to place the 'imtools.py' into the parent folder of where your Python is installed like C:\Python so place the file into that destination and type the following command:
from PIL import Image
from numpy import *
from imtools import *
Instead of typing the code with imtools.get_imlist() you just to remove the imtools from the code like:
get_imlist()
This may solve your problem as I had found my solution by the same technique I used.

Python - Modelica connection fails due to import error

I am trying to connect Modelica and Python using the Python27 block, provided by the Berkeley Simulations Lab:
http://simulationresearch.lbl.gov/modelica
I use this block to call a Python function:
def Test2(WriteValues):
''' Connection Test - Works if started from Dymola
'''
#Doing nothing and returning the input
ReturnList=WriteValues
return (ReturnList)
works perfectly.
Now I need to import some modules
#Importing Python modules works in general
import sys
import thread
import time
works aswell
Only now I want to import a module that is not part of Python but a site-package:
def Test1(WriteValues):
'''Connection Test - Doesnt work if started from Dymola
'''
#Importing some Bacpypes Module
#Path is by default C:\Python27\Lib\site-packages\BACpypes-0.7-py2.7.egg\bacpypes
#My os is win7
from bacpypes.core import run
#Doing nothing and returning the input
ReturnList=WriteValues
return (ReturnList)
This does not work. It does not matter if I import the BACpypes module inside a function or globally - the error is always
'module' object has no attribute 'argv'
Colleagues pointed me to the idea that it might be related to a multiple import problem. The function is being called by Modelica every 10 seconds (real-time-simualtion).
If I call the function Test1 outside of Modelica, there is no problem. It only fails using the Python27 block!
Does anyone have an idea about how to make the BACpypes import work?
UPDATE 2013-10-16:
I printed out the value of sys.argv for the script excecution in the Python directory and an excecution from Modelica.
sys.argv from Python directory:
['C:\\Python27\\Testcon.py']
sys.argv if function is called from inside Modelica:
['modpython']
Might this in any way be related to the error message I get?
The bug is caused because bacpypes uses sys.argv but the Python interpreter did not call PySys_SetArgv.
This will be fixed in the next version of the Modelica Buildings library, see https://github.com/lbl-srg/modelica-buildings/issues/191

Categories