0

This is my project structure

This is my project structure. I use virtualenv in my project but when I run it ,it has an ImportError.I use Mac. enter image description here enter image description here

But I can run it successfully use Pycharm

enter image description here

So how to run it successfully by Terminal.Because I want to run it in a Ubuntu server with cron


Thanks you for your answers.Here I show my solution.I modify my handler.py I think it may be related to The Module Search Path. So I add the project path to the PYTHONPATH.

import os

project_home = os.path.realpath(__file__)
project_home = os.path.split(project_home)[0]
import sys

sys.path.append(os.path.split(project_home)[0])
import shutil
from modules import db, json_parse, config_out
from init_log import init as initlog

initlog()

if __name__ == '__main__':
    try:
        columns = json_parse.json_parse()
        if not columns:
            sys.exit()
        is_table_has_exist = db.check_tables_exist(columns=columns)
        if is_table_has_exist:
            db.check_columns(columns=columns)
        is_ok, config_path = config_out.output(columns)
        if is_ok:
            file_name = os.path.split(config_path)[1]
            shutil.copy(config_path, os.path.join("/app/statics_log/config", file_name))
    except Exception, e:
        print e

And I run with crontab by this.

cd to/my/py_file/path && /project_path/.env/bin/python /path/to/py_file

example:

13 8 1 * * cd bulu-statics/create_config/ && /home/buka/bulu-statics/.env/bin/python /home/buka/bulu-statics/create_config/handler.py >> /app/statics_log/config/create_config.log
wyx
  • 3,334
  • 6
  • 24
  • 44

3 Answers3

0

PyCharm automatically adds project directories marked as containing sources to the PYTHONPATH environment variable, whihc is why it works from within pycharm. On the terminal use

PYTHONPATH=${PWD}/..:${PYTHONPATH} python handler.py
renefritze
  • 2,167
  • 14
  • 25
0

You can use explicit relative imports:

from .modules import db, json_parse, config_out

The proper way to do this is to turn your project into a proper Python package by adding a setup.py file and then installing it with pip install -e .

Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
0

probably because PyCharm added your project folder to the PythonPath, so you can run you app inside PyCharm.

However, when you try to run it from command line, python interpreter cannot find these libs in Python python, so what you need to do is to add your python virtualenv the python python.

there are different ways to adding python path, but I would suggest you to follow:

  1. prepare a setup.py you'll need to specify packages and install_requires.
  2. install your app locally in development mode via pip install -e /path/to/your-package -> it'll create a egg-link in your python virtualenv, you can run your app in your local terminal from now on;
  3. for packing and releasing, you may want to build an artifact by following https://docs.python.org/2.7/distutils/builtdist.html you may pip install or easy_install the artifact on your other machines. you also can release your package to PyPi if you want.
Community
  • 1
  • 1
Guoliang
  • 885
  • 2
  • 12
  • 20