troubleshoot - $ django-admin.py: command not found - python

leavemealone:~ test$ django-admin.py startproject mysite
-bash: django-admin.py: command not found
leavemealone:~ test$ sudo ln -s /usr/local/lib/python2.6/dist-packages/django/bin/django-admin.py /usr/local/bin/django-admin.py
ln: /usr/local/bin/django-admin.py: File exists
leavemealone:~ test$ django-admin.py startproject.py
-bash: django-admin.py: command not found
leavemealone:~ test$
So, as a disclaimer, I might be an idiot.
I followed the install instructions on djangoproject.com, and followed their solution to the command not found error, yet it persists. I literally have no idea what to do at this point.

Solution in comments, also was here https://docs.djangoproject.com/en/1.6/faq/troubleshooting/. Take care the files exists, the $PATH includes the path to django-admin.py, and the symlinks are correctly asigned

If you don't want to set the $PATH, then you can just use one long command in Bash to start a new project. Like so:
python ~/path/to/django-admin.py startproject <project_name>
A common path might be:
python ~/documents/virtualenvs/pay/lib/python2.7/site-packages/django/bin/django-admin.py startproject store

Related

django-admin startproject doesn't do anything

I use command "django-admin startproject myapp" and it doesn't do anything
When I use this command again it says "CommandError: 'C:\Users\User\Desktop\djangotest2\myapp' already exists"
I tried disabling my antivirus and I tried using absolute path to django-admin.py and django-admin.exe file

django i18n: Make sure you have GNU gettext tools

I try django-admin.py makemessages -l zh_CN but has error :
CommandError: Can't find msguniq. Make sure you have GNU gettext tools 0.15 or newer installed.
after I use brew install gettext,it still get wrong.
Do I need to do something? here is my terminal screenshot
Please guide me thank you.
In Ubuntu:
$ sudo apt-get install gettext
For Mac users, after installing Homebrew and gettext as #Louis Barranqueiro says (steps 1 and 2):
Install Homebrew : /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install GNU gettext : brew install gettext
You shouldn't use brew link gettext --force in step 3, because it is risky (as Brew advises if you try). A better workaround is to set a new PATH variable for your virtual environment. So, in the postactivate file, which is located in the bin folder of your virtual environment folder, type:
export TEMP_PATH=$PATH
export PATH=$PATH:/usr/local/Cellar/gettext/0.19.7/bin
Note that you have to replace 0.19.7 by the version that is installed in your machine.
And in your predeactivate file, which is located in the same folder of postactivate file, type:
export PATH=$TEMP_PATH
unset TEMP_PATH
Now you can use the python manage.py makemessages -l <desired_language> without worries. :)
Cheers.
This procedure worked for me (OSX 10.11.2 - python v3.5 and Django 1.8)
It should work with your configuration.
Install gettext GNU tools with Homebrew using Terminal
Install Homebrew : /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install GNU gettext : brew install gettext
Create symlink : brew link gettext --force
This solution worked for me ( win. 7, 8 and 10 )
You need to download two folders:
gettext-runtime_0.18.1.1-2_win32
gettext-tools-dev_0.18.1.1-2_win32
You can find them here.
After you download them, unzip them and add the directory of the bin file of the both folders to the system variables PATH of your pc.
You will also need a file named libstdc++-6.dll download it from here and place it in your system directory. You will find adequate details on system directory here.
And that’s it. Hope it is useful for you.
Just below solution solved my problem. I am using Windows 10 64bit
1- Go to this link :
https://mlocati.github.io/articles/gettext-iconv-windows.html
2- Download 32 or 64 bit shared and static windows installation files
3-Install both of files
4-Restart your computer
Hi first of all make sure that your virtual environment is not in your root folder. I think it's better practice to keep your virtual environment outside of the root folder. Obviously make sure your environment is activated. Of course make sure you have gettext installed as well.
If your env folder is in your root folder
To test this just make sure you add {% load i18n %} in all your templates, choose a template and do something like this:
<h1>{% trans 'My Test to be translated' %}</h1>
Now run this command
django-admin makemessages -l 'zh_CN' -i your_venv
(Make sure you replace your_venv to the name of your virtual environment.
After you run the above command, you should get this in your terminal.
processing locale zh_CN
Now you should have a locale folder like this: locale/cn/LC_MESSAGES/django.po
Now you will need to compile the messages. Run this command
django-admin compilemessages
In your locale folder, now you should get you should see django.mo file as well, but you will notice the difference in django.po file. Just add your translation there, and you can test again by setting your en language to LANGUAGE_CODE = 'zh_CN' then just refresh and test the h1 string will be translated to Chinese.
In order for the above to work in your settings.py ensure you have this here, for now most important is the LOCALE_PATHS, but please check if this ('zh_CN', _('Chinese')), is correct
LANGUAGES = [
('zh_CN', _('Chinese')),
('en', _('English')),
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
In this reply, the most important part is to realize where your virtual environment is located. Reason why you get all these errors.
Please make sure you refer to this video here, it's a great tutorial.
https://www.youtube.com/watch?v=xI97sLMd1rM
#max-malysh's answer solved it for me —without touching system files.
Copy and run each of the following:
brew install gettext
GETTEXT_PATH="/usr/local/Cellar/gettext/0.19.8.1/bin"
FILE="venv/bin/activate"
echo "" >> $FILE
echo "export PATH=\$PATH:$GETTEXT_PATH" >> $FILE
source venv/bin/activate
GETTEXT_PATH="/usr/local/Cellar/gettext/0.19.8.1/bin" stores gettext_path in a shell variable —adapt the version number according to what brew install gettext
FILE="venv/bin/activate" stores the path to the venv shell script
echo "" >> $FILE adds an empty line at the end of the to make sure the next command is on its own line
echo "export PATH=\$PATH:$GETTEXT_PATH" >> $FILE adds a command to the venv shell script; this command adds the path to gettext binaries to the global $PATH variable, so that they are used before OS binaries.
source venv/bin/activate runs the venv shell script so that variables are properly set. You can run this command more than once.
If you're using Docker just simply run below command:
apt-get update
Then:
apt-get install gettext
If you use fish shell, another way around is to add this path to $fish_user_paths.This variable is prepended to $PATH, so you don't have to set it in all your projects.
You can do it with the following command line :
set -U fish_user_paths /usr/local/Cellar/gettext/0.19.8.1/bin $fish_user_paths
Remember to replace 0.19.8.1 with your gettext version.
This sets $fish_user_paths as a Universal Variable. Here's what help says about Universal Variables :
A universal variable is a variable whose value is shared across all
instances of fish, now and in the future – even after a reboot. You
can make a variable universal with set -U
So setting this variable in your shell once (no need to do it in a config file) will save it even after logging out or rebooting.
that works for windows users.
i am using django 2
access this https://mlocati.github.io/articles/gettext-iconv-windows.html
2 download the static version for your system
after downloaded execute the setup.
restart your pc and it will work.
That is all. THANKS.!!!!
the problem is hinted in the output from brew...
it has installed the GNU gettext but hasn't linked it into your bin directory because OSX already provides a different version of gettext...
so Django doesn't know to run the version you installed from brew.
apparently brew is too cautious here though and you should just link it https://stackoverflow.com/a/9787791/202168

DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both

I've been usually installed python packages through pip.
For Google App Engine, I need to install packages to another target directory.
I've tried:
pip install -I flask-restful --target ./lib
but it fails with:
must supply either home or prefix/exec-prefix -- not both
How can I get this to work?
Are you using OS X and Homebrew? The Homebrew python page https://github.com/Homebrew/brew/blob/master/docs/Homebrew-and-Python.md calls out a known issue with pip and a work around.
Worked for me.
You can make this "empty prefix" the default by adding a
~/.pydistutils.cfg file with the following contents:
[install]
prefix=
Edit: The Homebrew page was later changed to recommend passing --prefix on the command line, as discussed in the comments below. Here is the last version which contained that text. Unfortunately this only works for sdists, not wheels.
The issue was reported to pip, which later fixed it for --user. That's probably why the section has now been removed from the Homebrew page. However, the problem still occurs when using --target as in the question above.
I believe there is a simpler solution to this problem (Homebrew's Python on macOS) that won't break your normal pip operations.
All you have to do is to create a setup.cfg file at the root directory of your project, usually where your main __init__.py or executable py file is. So if the root folder of your project is: /path/to/my/project/, create a setup.cfg file in there and put the magic words inside:
[install]
prefix=
OK, now you sould be able to run pip's commands for that folder:
pip install package -t /path/to/my/project/
This command will run gracefully for that folder only. Just copy setup.cfg to whatever other projects you might have. No need to write a .pydistutils.cfg on your home directory.
After you are done installing the modules, you may remove setup.cfg.
On OSX(mac), assuming a project folder called /var/myproject
cd /var/myproject
Create a file called setup.cfg and add
[install]
prefix=
Run pip install <packagename> -t .
Another solution* for Homebrew users is simply to use a virtualenv.
Of course, that may remove the need for the target directory anyway - but even if it doesn't, I've found --target works by default (as in, without creating/modifying a config file) when in a virtual environment.
*I say solution; perhaps it's just another motivation to meticulously use venvs...
I hit errors with the other recommendations around --install-option="--prefix=lib". The only thing I found that worked is using PYTHONUSERBASE as described here.
export PYTHONUSERBASE=lib
pip install -I flask-restful --user
this is not exactly the same as --target, but it does the trick for me in any case.
As other mentioned, this is known bug with pip & python installed with homebrew.
If you create ~/.pydistutils.cfg file with "empty prefix" instruction it will fix this problem but it will break normal pip operations.
Until this bug is officially addressed, one of the options would be to create your own bash script that would handle this case:
#!/bin/bash
name=''
target=''
while getopts 'n:t:' flag; do
case "${flag}" in
n) name="${OPTARG}" ;;
t) target="${OPTARG}" ;;
esac
done
if [ -z "$target" ];
then
echo "Target parameter must be provided"
exit 1
fi
if [ -z "$name" ];
then
echo "Name parameter must be provided"
exit 1
fi
# current workaround for homebrew bug
file=$HOME'/.pydistutils.cfg'
touch $file
/bin/cat <<EOM >$file
[install]
prefix=
EOM
# end of current workaround for homebrew bug
pip install -I $name --target $target
# current workaround for homebrew bug
rm -rf $file
# end of current workaround for homebrew bug
This script wraps your command and:
accepts name and target parameters
checks if those parameters are empty
creates ~/.pydistutils.cfg file with "empty prefix" instruction in it
executes your pip command with provided parameters
removes ~/.pydistutils.cfg file
This script can be changed and adapted to address your needs but you get idea. And it allows you to run your command without braking pip. Hope it helps :)
If you're using virtualenv*, it might be a good idea to double check which pip you're using.
If you see something like /usr/local/bin/pip you've broken out of your environment. Reactivating your virtualenv will fix this:
VirtualEnv: $ source bin/activate
VirtualFish: $ vf activate [environ]
*: I use virtualfish, but I assume this tip is relevant to both.
I have a similar issue.
I use the --system flag to avoid the error as I decribe here on other thread where I explain the specific case of my situation.
I post this here expecting that can help anyone facing the same problem.

How do I install a script to run anywhere from the command line?

If I have a basic Python script, with it's hashbang and what-not in place, so that from the terminal on Linux I can run
/path/to/file/MyScript [args]
without executing through the interpreter or any file extensions, and it will execute the program.
So would I install this script so that I can type simply
MyScript [args]
anywhere in the system and it will run? Can this be implemented for all users on the system, or must it be redone for each one? Do I simply place the script in a specific directory, or are other things necessary?
The best place to put things like this is /usr/local/bin.
This is the normal place to put custom installed binaries, and should be early in your PATH.
Simply copy the script there (probably using sudo), and it should work for any user.
Walkthrough of making a python script available anywhere:
Make a python script:
cd /home/el/bin
touch stuff.py
chmod +x stuff.py
Find out where your python is:
which python
/usr/bin/python
Put this code in there:
#!/usr/bin/python
print "hi"
Run in it the same directory:
python stuff.py
Go up a directory and it's not available:
cd ..
stuff.py
-bash: stuff.py: command not found
Not found! It's as we expect, add the file path of the python file to the $PATH
vi ~/.bashrc
Add the file:
export PATH=$PATH:/home/el/bin
Save it out, re apply the .bashrc, and retry
source ~/.bashrc
Try again:
cd /home/el
stuff.py
Prints:
hi
The trick is that the bash shell knows the language of the file via the shebang.
you can also use setuptools (https://pypi.org/project/setuptools/)
your script will be:
def hi():
print("hi")
(suppose the file name is hello.py)
also add __init__.py file next to your script (with nothing in it).
add setup.py script, with the content:
#!/usr/bin/env python3
import setuptools
install_requires = [
'WHATEVER PACKAGES YOU NEED GOES HERE'
]
setuptools.setup(
name="some_utils",
version="1.1",
packages=setuptools.find_packages(),
install_requires=install_requires,
entry_points={
'console_scripts': [
'cool_script = hello:hi',
],
},
include_package_data=True,
)
you can now run python setup.py develop in this folder
then from anywhere, run cool_script and your script will run.
Just create ~/bin and put export PATH=$PATH:$HOME/bin in your bashrc/profile. Don't mess with the system, it will bite you back, trust me.
Few more things (relevant to the question but not part of the answer):
The other way export PATH=$HOME/bin:$PATH is NOT safe, for bash will will look into your ~/bin folder for executables, and if their name matches with other executables in your original $PATH you will be surprised by unexpected/non working command execution.
Don't forget to chmod+x when you save your script in ~/bin.
Be aware of what you are putting in your ~/bin folder, if you are just testing something or working on unfinished script, its always better to use ./$SCRIPT_NAME from your CWD to execute the script than putting it under ~/bin.
The quick answer is to symlink your script to any directory included in your system $PATH.
The long answer is described below with a walk through example, (this is what I normally do):
a) Create the script e.g. $HOME/Desktop/myscript.py:
#!/usr/bin/python
print("Hello Pythonista!")
b) Change the permission of the script file to make it executable:
$ chmod +x myscript.py
c) Add a customized directory to the $PATH (see why in the notes below) to use it for the user's scripts:
$ export PATH="$PATH:$HOME/bin"
d) Create a symbolic link to the script as follows:
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
Notice that hello (can be anything) is the name of the command that you will use to invoke your script.
Note:
i) The reason to use $HOME/bin instead of the /usr/local/bin is to separate the local scripts from those of other users (if you wish to) and other installed stuff.
ii) To create a symlink you should use the complete correct path, i.e.
$HOME/bin GOOD ~/bin NO GOOD!
Here is a complete example:
$ pwd
~/Desktop
$ cat > myscript.py << EOF
> #!/usr/bin/python
> print("Hello Pythonista!")
> EOF
$ export PATH="$PATH:$HOME/bin"
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
$ chmod +x myscript.py
$ hello
Hello Pythonista!
Just create symbolic link to your script in /usr/local/bin/:
sudo ln -s /path/to/your/script.py /usr/local/bin/script
Putting the script somewhere in the PATH (like /usr/local/bin) is a good solution, but this forces all the users of your system to use/see your script.
Adding an alias in /etc/profile could be a way to do what you want allowing the users of your system to undo this using the unalias command. The line to be added would be:
alias MyScript=/path/to/file/MyScript
i find a simple alias in my ~/.bash_profile or ~/.zshrc is the easiest:
alias myscript="python path/to/my/script.py"
Type echo $PATH in a shell. Those are the directories searched when you type command, so put it in one of those.
Edit: Apparently don't use /usr/bin, use /usr/local/bin
Acording to FHS, the /usr/local/bin/ is the good place for custom scripts.
I prefer to make them 755 root:root, after copying them there.

Django-admin.py not working (-bash:django-admin.py: command not found)

I'm having trouble getting django-admin.py to work... it's in this first location:
/Users/mycomp/bin/ but I think I need it in another location for the terminal to recognize it, no?
Noob, Please help. Thanks!!
my-computer:~/Django-1.1.1 mycomp$ sudo ln -s /Users/mycomp/bin/django-admin.py /Users/mycomp/django-1.1.1/django-admin.py
Password:
ln: /Users/mycomp/django-1.1.1/django-admin.py: File exists
my-computer:~/Django-1.1.1 mycomp$ django-admin.py --version
-bash: django-admin.py: command not found
you need to export /Users/mycomp/bin to environment variable PATH
for a session
export PATH=/Users/mycomp/bin:$PATH
for permanent, whenever you use bash
echo "export PATH=/Users/mycomp/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
Note: And I think django automatically create executable django-admin file in the bin folder (notice there is no extensions .py) when you installed, So you should try django-admin only too.

Categories

Resources