Cannot access imported functions in django shell by pipeline command

29 views
Skip to first unread message

Cheng-Hung Hsueh

unread,
Aug 25, 2017, 1:33:08 AM8/25/17
to Django users

It is a python script `tshi3.py` :


在此輸入代碼...
    import csv
   
def li2ho2():
       
print(csv)
    li2ho2
()

I copied this code and pasted it in `python manage.py shell`. It works.

But I ran `python manage.py shell < tshi3.py`. Got `NameError: name 'csv' is not defined.`

Why?


Enviroment:

在此輸入代碼...
    Linux Mint 18.1
   
Python 3.4.1 (default, Sep  3 2014, 08:45:22)
   
Django==1.11.4

Mike Morris

unread,
Aug 25, 2017, 7:58:00 PM8/25/17
to django...@googlegroups.com

Wild guess: Different default directory.

Try setting PYTHONPATH, or add "os.chdir('some/dir')" to top of script...

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7466c326-0f03-4299-9239-1126daf24aaf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Aug 25, 2017, 8:23:25 PM8/25/17
to django...@googlegroups.com


On Aug 24, 2017 10:33 PM, "Cheng-Hung Hsueh" <ihc...@gmail.com> wrote:

It is a python script `tshi3.py` :


在此輸入代碼...
    import csv
   
def li2ho2():
       
print(csv)
    li2ho2
()

I copied this code and pasted it in `python manage.py shell`. It works.

But I ran `python manage.py shell < tshi3.py`. Got `NameError: name 'csv' is not defined.`

Why?


I don't know if the 'manage.py shell' command supports what you are doing. I would instead write a management command to run arbitrary code from the command line.


-James
Reply all
Reply to author
Forward
0 new messages