0

I have a python project and want to write a command line launcher. My project works fine when I run tests or launch from my IDE (pycharm). However when I try to launch via the command line I get a ModuleNotFoundError: No module named 'python' error.

Here is my directory structure:

.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── create_jenkins_jobs.sh
└── python
    ├── __init__.py
    ├── create_credentials.py
    ├── create_jenkins_jobs_cli.py
    ├── credential_builder.py
    ├── jenkins_multibranch_job_builder.py
    ├── templates
    │   ├── job.xml
    │   ├── multibranch_job_template.xml
    │   ├── ssh_cred_template.xml
    │   └── user_pw_cred_template.xml
    └── tests
        ├── __init__.py
        ├── integration
        │   ├── __init__.py
        │   ├── build_credential_test.py
        │   ├── build_job_test.py
        │   ├── conftest.py
        │   ├── test_id_rsa
        │   └── test_id_rsa.pub
        └── unit

The shell script create_jenkins_jobs.sh contains:

#!/bin/bash
python3 python/create_jenkins_jobs_cli.py "${@}"

the file create_jenkins_jobs_cli.py contains the following code:

from typing import Set
from shared.common import preflight_env_check

from python.jenkins_multibranch_job_builder import JenkinsMultibranchJobBuilder
def configure_jenkins_jobs(git_repos: Set[str]):
    jenkins_job_builder = JenkinsMultibranchJobBuilder(jenkins_server_url=JENKINS_BASE_SERVER_URL,
                                                       jenkins_folder=JENKINS_ROOT_JOB_FOLDER,
                                                       user=JENKINS_USER, token=JENKINS_TOKEN,
                                                       git_cred_id=GIT_SSH_CRED_ID,
                                                       git_repos=git_repos)
    jenkins_job_builder.configure_jobs()


configure_jenkins_jobs(GIT_REPOS)

When I launch create_jenkins_jobs.sh or create_jenkins_jobs_cli.py I always get

ModuleNotFoundError: No module named 'python'


$>./create_jenkins_jobs.sh
/Users/pswenson/dev/cc-cicd-automation/.venv/bin/python3: Error while finding module specification for 'python/create_jenkins_jobs_cli.py' (ModuleNotFoundError: No module named 'python/create_jenkins_jobs_cli')

I've tried all sorts of different techniques to get this to work with PYTHONPATH, working directories, etc...

It seems there is something basic about how python modules work that I don't understand.

1
  • 1
    It's the directory the main script lives in, not the current working directory, that is automatically added to the Python path. There is no package named python in the directory ./python.
    – chepner
    Commented Dec 10, 2020 at 21:05

1 Answer 1

1

Does changing the line:

from python.jenkins_multibranch_job_builder import JenkinsMultibranchJobBuilder

to

from jenkins_multibranch_job_builder import JenkinsMultibranchJobBuilder

Sort you?

1
  • I knew it was something dumb, thanks. turned out that pycharm was configured in such as way that ^^^ didn't work unless I referenced python as a module.... so when I did ^^^ pycharm broke but was easy to fix. Commented Dec 11, 2020 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.