1

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?

Viacheslav
  • 143
  • 4
  • 19

2 Answers2

1

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.

Viacheslav
  • 143
  • 4
  • 19
0

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.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • Thanks, it seems to me I tried a lot of suggestions from that post, the problem is that my server should be running in async mode as it actually never exists until I terminate it. So, using things like os.system or just import server_run and start main() will block me – Viacheslav Oct 04 '17 at 18:36
  • @Viacheslav If you read my last comment it's wrong. I'll look for an answer and I'll get back to you. – Xantium Oct 04 '17 at 19:10
  • thanks for your responses. Won't os.system() block me? I mean I start it in the start of my test and do some actions afterwards, but as I start server is the same process it just blocks it – Viacheslav Oct 04 '17 at 19:13
  • @Viacheslav The thread or subprocess module should 'unblock' it. This is very surprising if it does not. Maybe you could give me some code to work with it's hard to understand completely since do not know how it actually works? – Xantium Oct 04 '17 at 19:15
  • @Viacheslav Would you reload the page. I removed that comment because it was for windows. – Xantium Oct 04 '17 at 19:22
  • you are right, subprocess doesn't block it, but my server doesn't see imports any more – Viacheslav Oct 04 '17 at 19:22
  • @Viacheslav Hmm. I can only think of one thing now. That's create a third script which launches the server and the the second script using threading or subprocess. I'm sorry I can't think of anything else. I need more to work with but I'll keep thinking and if I get an answer you'll be the first to know. – Xantium Oct 04 '17 at 19:30