You are not logged in.

#1 2015-12-25 12:37:09

morganjeff7272
Member
Registered: 2015-12-23
Posts: 6

[SOLVED]my python module imports but I can not use functions

From start to finish, Ill try to be as precise as possible.
Which python returns /usr/bin/python
Using version 3.5 I have the following directory structures
/lib/python3.5/site-packages 
I have a symbolic link in above directory to
/home/ep/python
which is where I am manipulating my own packages and modules
I have the blank file __init__.py in every folder below python and including python that way the interpreter will walk through the directories
ok so I have /home/ep/python/proj/reuseable/process_exists.py

import os
def process_exists(proc_name):
    ps = subprocess.Popen("ps ax -o pid= -o args= ", shell=True, stdout=subprocess.PIPE)
    ps_pid = ps.pid
    output = ps.stdout.read()
    ps.stdout.close()
    ps.wait()

    for line in output.split("\n"):
        res = re.findall("(\d+) (.*)", line)
        if res:
            pid = int(res[0][0])
            if proc_name in res[0][1] and pid != os.getpid() and pid != ps_pid:
                return True
    return False 

and I have /home/ep/python/proj/EPR/epr.py

import python.proj.reuseable as reuseable
if (not reuseable.process_exists('chromedriver')):
    #launch chromedriver as commandline script haven't figured this out yet exactly so 
    a=b
    # to make syntax work
else:
    #do nothing since it is running I will probably add logging functionality here
    b=a
    # to make syntax work

...

there is more in this file but it all works prior to adding the above.
My goal here is to launch the chromedriver if it is not running so I can use it as remoteDriver instead of multiple multiple instances of the dang thing running.
SO...... 2 questions.....

1. better way to test for process runnning than what I got from http://stackoverflow.com/questions/3805 … ll-running ?

2. I'm getting the Error
traceback...
File "epr.py", line 6, in <module>
if(not reuseable.process_exists('chromedriver')):
AttributeError: module 'python.proj.reuseable' has no attribute 'process_exists'

What have I missed/messed up?

Last edited by morganjeff7272 (2015-12-27 00:01:00)

Offline

#2 2015-12-25 18:47:50

morganjeff7272
Member
Registered: 2015-12-23
Posts: 6

Re: [SOLVED]my python module imports but I can not use functions

In my import statement I am one module too earliy to imply or call the function.
For reuseability and functionality once I figured that out I renamed the file process_exists.pr to process.py that way I can make more functions that have to do with process type functionality. and then fixed my import statement to
from python.proj.reuseable import process
that worked and now im working on the actual methos its broken but I found psutils still need my own method...
Ill post it when done.

Offline

#3 2015-12-25 19:16:27

morganjeff7272
Member
Registered: 2015-12-23
Posts: 6

Re: [SOLVED]my python module imports but I can not use functions

Just got real simple!

import psutil

def process_exists(proc_name):
    """Returns True/False if proc_name is found to be a running process"""
    for p in psutil.process_iter():
        if(p.name() == proc_name):
            return True
        else:
            return False

still need to launch the driver if not found but that should be straightforward.

Offline

#4 2015-12-25 19:16:58

morganjeff7272
Member
Registered: 2015-12-23
Posts: 6

Re: [SOLVED]my python module imports but I can not use functions

mark as solved.

Offline

#5 2015-12-26 06:10:21

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,791

Re: [SOLVED]my python module imports but I can not use functions

morganjeff7272 wrote:

mark as solved.

Hi,

We ask that you do that as only you know when it is really solved.  Edit your first post and change the topic to prepend [SOLVED].  You may need to trim the title a little to get it to all fit.

Thanks.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

Board footer

Powered by FluxBB