Whenever I try to expirement with Python on Windows, I always run into a wall with the import statements. Python simply can't find anything on Windows -- every import, even for something as core as timezone fails.
I know this has something to do with the PYTHONPATH environment variable. In my case, Python is installed to "C:\\Python27". My PYTHONPATH looks like this:
C:\Python27;C:\Python27\DLLs;C:\Python27\Lib
Still, nothing will import. I get errors like this:
File "D:\Code\Django\polls\models.py", line 3, in <module>
from django.utils import timezone
ImportError: cannot import name timezone
What's wrong with my situation?
Take a look at the official docs on using Python on Windows , in particular the section on finding modules .
You have to add the directory where you installed 3rd party modules to your PYTHONPATH
if you didn't install them to your Python27\\Libs\\site-packages
directory.
Another option is to get acquainted with pip and virtualenv . These tools make installing 3rd party modules a breeze. Although I don't know how well they are supported on Windows (I mainly do Python development on Linux).
PYTHONPATH
= If this variable exists in your environment, Python will add it to the normal search path for modules when you use any import statement; you normally do not modify this as well behaved Python scripts will install themselves in the site-packages
directory, and Python searches this by default.
PATH
= this is the global file system path. Your operating system will search the directories listed in this variable (from left to right), to find commands when you type something at a command prompt.
In order for Python to work correctly only Windows, the C:\\Python27
directory should be listed in PATH
. If you ran the installer as an Administrator, the installer will modify the global PATH
and add this for you. If you installed it as a normal user, you need to modify the PATH
manually.
To add this manually, right click on My Computer
and select Properties
. Click on Advanced
, then Environment Variables
. You'll see two boxes - User Variables
and System Variables
. You can only edit user variables - system variables need administrative access.
Simply add a new variable (or modify the existing PATH
) You should also add C:\\Python27\\Scripts
to your PATH
as most commands installed by Python scripts (like django-admin.py) are installed here. Directories are separated by ;
Once you have done this; python should work properly for you on Windows.
virtualenv is good option.
else simply add site-packages in path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.