Issues running python scripts in Command Prompt (Specifically with command line arguments)? - python

I am trying to run my python scripts in the command-prompt without calling python.exe first. I am specifically doing this in relation to running django-admin.py. I have C:\Python26 and C:\Python26\Scripts in my PATH. However, if I try running django-admin.py by doing:
django-admin.py startproject helloworld
I get the message: Type 'django-admin.py help' for usage.
Now, after some experimentation, I realized the problem is that the secondary arguments to these scripts are not being passed for some reason, since I tried it with a some other python scripts I have. I know I could avoid this problem by simply doing:
python C:\Python26\Scripts\django-admin.py startproject helloworld
But I know it should be possible to run the first command only and get it to work, because I had it working before. I've looked everywhere, and not many places have been helpful so any idea would be useful for me at this point.
Update: The .py file associations were set correctly, and the problem is still occuring.

Check assoc and ftype. If properly set, you can run a .py with arguments.
> assoc .py
.py=Python.File
> ftype Python.File
Python.File="C:\Python26\python.exe" "%1" %*
Depending on how your Python was installed, these may or may not be in place. You can set them with assoc and ftype.
> assoc .py=Python.File
> ftype Python.File="C:\Python26\python.exe" "%1" %*
Also, if .py is included in the PATHEXT environment variable, you can run .py files without the trailing .py.
> set PATHEXT=%PATHEXT%;.py
> django-admin startproject helloworld

I know this is an old thread, but I have searched for a few weeks for this very same problem and found nothing.
Today, however, I tried something new:
If you are using Windows 7, do not use Command Prompt for scripting purposes.
Instead, use the Windows PowerShell located at: All Programs -> Accessories -> Windows PowerShell -> Windows PowerShell. In there you can run the command django-admin.py startproject mysite if you added the correct paths to your environmental paths.
BTW, I'm now using Python 2.7 with Django 1.2.4 on Windows 7 Ultimate 32bit.

Related

Windows 10 won't launch my python script with the correct version of python despite correct file association and paths.

In Windows 10, I have both Python3.5 and Python2.7 installed, and need to keep both. My script is written for Python3.5. If I launch the script with "python" or "py" prefix, it launches without any problems. However, when I invoke it directly, it fails with an error similar to python version conflict:
File "C:\Users\user\AppData\Local\Programs\Python\Python35\lib\site.py", line 176
file=sys.stderr)
^
SyntaxError: invalid syntax
In my command prompt, I type
>python script.py
or
>py script.py
It works, but if I just run the script, either with ./script.py or just script.py (since the script is in my path) it fails with the conflict mentioned above.
My PATH, PYTHONHOME and PYTHONPATH all point to 3.5
>echo %PATH%
C:\Users\user\AppData\Local\Programs\Python\Python35;C:\Users\user\AppData\Local\Programs\Python\Python35\Scripts;
>echo %PYTHONPATH%
C:\Users\user\AppData\Local\Programs\Python\Python35\lib
>echo %PYTHONHOME%
C:\Users\user\AppData\Local\Programs\Python\Python35
My python file association is set to py launcher, which works correctly if I add py to the beginning of my path.
>assoc .py
.py=Python.File
>ftype Python.File
Python.File="C:\WINDOWS\py.exe" "%L" %*
My py and python both have correct versions for the ones called in my path:
>py --version
Python 3.5.3
>python --version
Python 3.5.3
which python points to the correct version too.
>which python
/cygdrive/c/Users/user/AppData/Local/Programs/Python/Python35/python
The only other thing I could think of was that cygwin or MSDOS shell have their own python interpreter they are using that interferes, but I can't find a version of python in either directory.
There must be some other file association overriding this one somewhere in Windows 10 that I don't know about. Unix shebang would solve this whole thing, but Windows is just annoying. Anyone have an clues?
SOLVED! assoc py was incorrect. In the regedit I found another key that had been added by an IDE, which added a command to edit with it's own python27 version. It somehow was taking precedence over the real python. After deleting this key, and resetting the Windows associate extension .py, it worked correctly.
Thanks to suggestion from #Jean-FrançoisFabre.
There is also one tweak to the issue. If you run Python scripts without the .py extension (using the PATHEXT environment variable) , you need to update the HKEY_CLASSES_ROOT\py_auto_file\shell\open\command registry ke
to "C:\PathToPython" "%1" %* The %* part might be missing, which ensures that arguments are correctly passed to Python.

os.system does not look the programs that are in my path

I have a problem when calling programs inside a python script. The programs that are giving me problems are those that I have installed manually on my computer and then added them to path on .bashrc file. The programs that where installed using 'sudo apt-get install some_program' don't give me any problem
The programs where added to my .bashrc file as the following way:
#path to fastqc
export PATH=$PATH:/home/bioinfor3/bin/FastQC/
#path to fastx-toolkits
export PATH=$PATH:/home/bioinfor3/bin/fastx/
Inside my PyCharm, I am using the os module to call those programs the in the below manner:
os.system('fastqc seq.fastq')
And I get this error
sh: 1: fastqc: not found
I guess it has something to do with the sh path or something, but I am not able to make it work
EDIT:
If Pycharm is launched from the terminal, it inherits the bashrc file with my personal paths and it works
Presumably this is happening because you have modified your login environment to adjust your PATH, but this updated path isn't seen by the shell that's running PyCharm, or PyCharm appears to be nullifying it somehow.
You should first of all verify that
os.system('/home/bioinfor3/bin/FastQC/fastqc seq.fastq')
operates as you would expect (no reason why it shouldn't, but worth checking).
It seems from this answer that by default PyCharm doesn't use bash for its shell but tcsh. Therefore it isn't seeing the setting you are enforcing on bash.

Django-admin.py startproject is not being able to be created

whenever i give
django-admin.py startproject
i get a notepad of django-admin. So that means i type in my website inside that ?
i have executed the django-admin.py and django-admin.pyc in cmd also. Still it shows. What should I do ?
You have - for some reason - created a virtual environment inside the actual Python source directory on Windows.
There is nothing wrong with this, except you may face issues when you uninstall or upgrade Python.
To fix your immediate problem, you need to follow these steps:
Close all command prompt windows.
Open a new command prompt.
Type the following and hit ENTER which will activate your virtual environment:
C:\Python27\Scripts\pymote_ven\Scripts\activate.bat
Now, type the following to start your project:
python django-admin.py startproject nameofyourproject
You should also read the documentation because you have confused the directories that are involved.

django-admin.py is not working

Am new to django and i have been trying to set up a project with no success. When i type
django-admin.py startproject mysite i get this.
C:\Users\WASSWA SAM\Documents\django\djcode>django-admin.py startproject mysite
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=all output
etc....
I have added it to my path like so
C:\Python26\Lib\site-packages\django\bin
And when i import django from interactive mode it works perfectly. What could be the problem. I am using Windows 7 and django 1.3.1. I installed it using setup.py.
I had the same problem; I found a working solution at https://groups.google.com/forum/#!msg/django-users/b76qSG3mV6g/jP1o0ny3i2kJ:
python C:\Python27\Scripts\django-admin.py startproject mysite
This is an error with Python's installation on Windows. If you have the regkey entry
HKEY_CLASSES_ROOT\py_auto_file\shell\command\open\
change the value from
"C:\Python27\python.exe" "%1"
to
"C:\Python27\python.exe" "%1" %*
(Or your equivalent installation location)
This ensures that command-line arguments are passed onto python, rather than explicitly ignored. This is fixed in the latest install of Python 2.7. %* is argv[]. "%1" is the script's absolute path.
I had the same problem and could not solve it for hours. Create a new project from Pycharm or any other program you are using, install Django and then instead of running
django-admin.py startproject *project_name* .
run
django-admin startproject *project_name* .
Ommiting .py extension worked for me and everything is running as expected now.
I don't think you need to have django\bin in the path.
I think what you need to add to your PATH though is C:\Python26\scripts and then you should be able to run the startproject script.
In my case, I use eclipse and you can start an eclipse project from within the eclipse environment using PyDev.
Go up to File > New > Other > (within the PyDev Folder) > PyDev Django Project. In the wizard it will allow you to setup most of the commonly used features of Django.

Can't open file 'django-admin.py': [Errno 2] No such file or directory

I'm using Python 2.7, Django 1.2.5 and on Windows 7.
I am not sure what I've done. I used to be able to create Django projects like
python django-admin.py startproject test
Now however I get this error.
Can't open file 'django-admin.py':
[Errno 2] No such file or directory
I can type the following which works.
python C:\Python27\Scripts\django-admin.py startproject test
How can I have it the way it used to be? Not having the type the full path to the django-admin.py file.
Things I've already tried:
I uninstalled Python and manually removed the values from the PATH variable in Windows. Reinstalled Python. Deleted Django and reinstalled it too.
I've added C:\Python27\Scripts to my PATH and PYTHONPATH variable under Environmental Variables in Windows.
Any suggestions?
My PATH variable contains
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\ActiveState Komodo Edit 6\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\django-apps;F:\My_Projects;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Mercurial\bin;C:\Python27;C:\Python27\Scripts
PYTHONPATH has
C:\Python27;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\Python27\Scripts;C:\django-apps;f:\my_projects
I had this same problem with slightly newer versions of Python 2.7.x and Django - and it is not the PATH. This is all I had to do to fix it in Windows XP:
Find a .py file (any, even a blank).
Right click on it and choose: "Open with>" and then select "Choose program...".
This pops up a list of all programs - select python, and check the box "Always use the selected program to open this kind of file" and then click OK.
Checking this box resets file associations and fixes this problem for the command line.
The cause of the problem: Telling Windows to open up .py files in a text editor as default.
If C:\Python27\Scripts is in your Path, just type in:
django-admin.py startproject proj
There should be a file association with .py and try to execute. If you prefix with python the next command is a file path.
I've never been able to do python django-admin.py -- I get the same error you describe.
Make sure python is associated with .py. You can check via file properties (opens with...) or typing assoc .py in cmd.
Here is what I did to get this working:
Installed Python 2.7 to C:\Python27 using the install package for Windows at python.org
I chose to install the latest release version of Django (1.3) (not the development trunk)
Installed Django following the instructions here: http://docs.djangoproject.com/en/dev/topics/install/
I'm only doing local development, so I skipped install of Apache and mod_wsgi (as directed in Django instructions)
I don't need a database for my app, so I skipped "Get your database running" section in Django instructions; and I skipped "Remove any old versions of Django" (didn't have any installed)
I skipped down the instructions to "Installing an official release"
I installed bsdtar as directed in order to untar the release files on my windows machine - ran no problem.
Being on Windows, I started a cmd shell with admin privileges and ran the command "setup.py install" - ran no problem
I followed the instruction to verify Django install: running import django and django.print get_version() - returned 1.3, success
Then it came time to run django-admin.py startproject myproject - I received similar errors to the above, and when trying to run django-admin.py from within the python interpreter, I received syntax errors pointing at the arguments. Strange.
Found this thread, and ran this, which worked: c:\Python27\Scripts\django-admin.py startproject myproject and it worked.
Still curious, I wanted to see if I could make it work from cmd prompt, without the paths, since it seemed that should work - and it didn't.
This is what I did beyond the install instructions that made it work for me on Windows:
Verified the file associations, using info from this post - all associations good.
Used Start|Computer|System Properties|Advanced System Properties|Environment Variables dialog to set the environment variables as follows:
Set New System Variable: PYTHONPATH = c:\Python27\Lib;c:\Python27\Scripts
Edit existing User variable: PATH added: C:\Python27;C:\Python27\Scripts to the end.
The result:
.py files now execute from cmd command line (no need to run python interpreter first)
e.g. the command django-admin.py startproject mynewproject ran just fine.
Please post any questions in the comments, maybe I can help.
Is it possible you associated your .py files with another program (like a text editor)? I had this problem, too, after associating .py files with gedit.
I'd have the problem if I did this:
python django-admin.py startproject myproject
Once I reassociated .py files to python this problem went away.
Gosh! It drove me crazy! Just do the following!
python C:\python27\scripts\django-admin.py startproject mysite
The problem seems to be with the file association. After adding the PATH variables, remove all "py" associations instead of linking them to your Python executable:
Remove the file type ".py " from the registry or by using a small
free tool for Windows Vista/Windows 7 called 'Unassoc' (google for Windows 7 unassoc).
In my case simply unassociating the file type was not enough. I had to remove the file type entirely using the unassoc tool.
Now you should be good to go.
I've solved it! It's the command line to open a file .py.
It has to be like so:
"C:\Python27\python.exe" "%1" %*
mine was:
"C:\Python27\python.exe" "%1" without the final %*
I've used FileTypesMan to edit because Windows 7 can't edit this property.
In my case it was solved by adding the path to django-admin.py.
The instruction in windows with a python 7 and django 1.11 is:
python c:\Python27\Lib\site-packages\django\bin\django-admin.py startproject mysite
First, set the path in PowerShell (in your $profile) like this:
$env:PATH = "C:\Python27\;C:\Python27\Scripts;c:\python27\lib\site-packages\django\bin\;"
Then, to get .py files to open in PowerShell rather than cmd, add this line:
$env:PATHEXT += ";.py"
Finally, to be able to just type "django-admin" and have it work, add this line:
function django-admin {python (gcm django-admin.py | resolve-path) $args}
That should do the trick.
After I installed an IDE, I had a similar (if not the same) problem. Sure enough, the .py "Open With" setting had been changed, and changing it back to the Python Launcher for Windows did the trick.
On Windows, I used
django-admin startproject test
and it seemed to have worked.
Since the path is too long, I moved the folders to a shorter path inside C: /. This way I call it faster, there is a problem with the route. I use Windows 10 with Django 3.0.5, so it turns out like this: python C:\Python38\Scripts\django-admin.py startproject test
The problem for me was the installation of Visual Studio.
I tried to install django-admin, which is usely automatically set up:
pip3 install django-admin
and it gave to me :
error: Microsoft Visual C++ 14.0 or greater is required.
So, I install Microsoft Visual Studio, and then it works.
Make sure that you cd is where you saved your Notepad++ Python file. If you saved it under your \Python27\ directory, then while in the terminal or PowerShell window, type cd C:\Python27 and press enter.
This will open that directory so when you type python filename.py it will find it and run it. Just remember to save all .py files to the same directory, so you have no further problems.
No Windows, usei:
python -m django startproject mysite

Categories