Problems getting Django up and running on OSX - python

First, I'm following the tutorial on the Django Poll application on their site and have hit a few road blocks.
I've installed Python 2.7 and Django 1.3
Created a new project with
django-admin.py startproject myproject
I got mysql up and running and entered the credentials in settings.py
Then when I try to run
python manage.py syncdb
it puked ... http://pastie.org/2294709
Then when I installed Mysql-python
easy_install mysql-python
Success! ...
Searching for MySQL-python
Best match: MySQL-python 1.2.3
Adding MySQL-python 1.2.3 to easy-install.pth file
Using /Library/Python/2.6/site-packages
Processing dependencies for MySQL-python
Finished processing dependencies for MySQL-python
so I try again
python manage.py syncdb
Still pukes the same message as above
Am I missing Something?

jondavidjohn's comment is spot on: you will have no end of confusion with multiple versions of Python on the Mac. There are some things you can do to deal with this.
Stick with 2.6
Convert to Lion, which has 2.7
Add an alias to your shell environment to invoke version 2.7 with $ python. In my bash_profile, I have "alias python='/usr/local/bin/python'"
Be sure to set your PYTHONPATH variable to the site packages directory that corresponds to the version of Python that you're using, such as PYTHONPATH=/usr/local/Cellar/python/2.7.1/lib/python2.7/site-packages
Make sure you know which version of easy_install is operative; many module problems arise from assuming that easy_install will use the same site-packages version for everything. Not true always. Every different Python bin directory has its own version of easy_install (or pip, by preference) which will use the corresponding site packages directory.
Don't try to run Django under Python 3.x
Don't try to get rid of the OS version of Python; it will break your installation because software update sometimes relies on it.
Once you're past this hurdle, there are some other parts of getting the sample poll app in Django to run that I had problems with, so I put my experience at https://github.com/technocrat/Writing-your-first-Django-app

You need to install and use a version of easy_install for your Python 2.7. You are using the version for the system Python 2.6. Follow the instructions here to install Distribute using python2.7 to launch python. Then use easy_install-2.7 to install mysql-python.

Related

How to reinstall all user packages after updating Python version in Windows?

I have a Windows 7 machine running Python 3.8.5 with a very large number of physics/electronics/data analysis/simulation packages. As it turned out, I must have - for some inexplicable reason - installed the 32-bit version of Python instead of the 64-bit one despite having a 64-bit system. And I didn't notice until very recently when I was trying to install some packages that require 64-bit Python. Hence I've now downloaded and installed the latest Python version that is supported by Windows 7, which seems to be 3.8.10.
Question: What is the easiest and also fail-safe way to reinstall all the user packages - that I currently have under 3.8.5 - to 3.8.10?
For some reason, I couldn't find any "canonical" solution for this online. As it seems, Python does not come with any built-in support for updating or system migration and I'm honestly wondering why...
Anyway, my first idea was to get a list of all user (= "local"?) packages currently installed under 3.8.5, but I don't know how. Reason: Doing help('modules') inside the interpreter will list all packages and I don't see a way to "selectively apply" pip to a specific Python version, e.g. something like python-3.8.5 -m pip list --local is not supported.
After getting a list of the user packages, I was thinking to pack it into a batch command pip install package_1 package_2 <...> package_N, thus reinstalling everything to Python 3.8.10. And afterwards uninstalling Python 3.8.5 and removing all environment variables from system PATH.
Is this the proper way to do this?
Anyway, my first idea was to get a list of all user (= "local"?) packages currently installed under 3.8.5, but I don't know how.
Create a list of installed packages with pip freeze > pkglist.txt or pip list --format=freeze. If you already have one, that's great.
Then uninstall 32-bit Python 3.8.5 and clean your path for all Python related variables. Now, install 64-bit Python 3.8.10.
After reinstalling, you can install back all the packages with pip install -r pkglist.txt and it will restore the exact versions of the packages.
If you insist on having both 32-bit and 64-bit versions installed and also have the Python Launcher installed, you could invoke 32 and 64 bit versions separately with py -3.8-64 -m pip and py -3.8-32 -m pip.
I don't see a way to "selectively apply" pip to a specific Python version.
This is possible with the Python Launcher on Windows. But only between major/minor versions and not the patch versions according to its help message.
I would also recommend creating a virtual environment this time before installing the packages and leaving the root environment alone. You can create one named venv with just python -m venv venv, activate it with ./venv/Scripts/activate and proceed with the installation of packages.
Nope, doesn't work. After installing the packages with the newer Python version in PATH, e.g. Jupyter won't start.
If the Jupyter error persists, you could try pinning packages to their most recent patch/minor versions to update them and yet not break your code.
As a last resort, you could try installing Python 3.10 alongside your current Python installation (without uninstall or editing the PATH) and then installing the absolute latest versions of the packages in a 3.10 virtual environment to see if it works for you. You would invoke the two versions with Py Launcher, e.g. py -3.10 and py -3.8.
If I understood correctly, you have multiple packages like NumPy, pandas etc. installed on your machine, and you want to reinstall them "automatically" on a fresh installation of python.
The method (I use) to perform such an operation is by creating a file named setup.py which includes a list of all the packages.
Bellow, I am attaching an example of such a file I use in one of my projects:
from setuptools import setup, find_packages
setup(
name='surface_quality_tools',
version='0.1',
install_requires=["matplotlib", "psutil", "numpy", "scipy", "pandas", "trimesh", "pyglet", "networkx", "protobuf",
"numpy-stl", "sklearn", "opencv-python", "seaborn", "scikit-image", "flask", "tqdm", "pytest"],
package_data={'': ['*.json']},
packages=find_packages(include=[])
)
to run the installation you should open a command prompt from inside the project directory and run:
pip install -e .
You can find a nice example in this blog page
One common way of handling packages in Python is via virtual environments. You can use Anaconda (conda), venv or any of several other solutions. For example, see this post:
https://towardsdatascience.com/virtual-environments-104c62d48c54#:~:text=A%20virtual%20environment%20is%20a,a%20system%2Dwide%20Python).
The way this works in by keeping the Python interpreter separate from the virtual environment that contains all the necessary packages.
Probably the main reason Python doesn't feature migration tools (at least as part of standard library) is because pip - the main package tool - doesn't handle conflict resolution all too well. When you update a version of Python it might so happen (especially with niche packages) that some of them won't work any more and pip often won't be able to solve the dependencies. This is why it's a good idea to keep a separate venv for different Python versions and different projects.
The other tool you could use for easy migration is Docker which is a semi-virtual machine working on top of your host OS and containing usually some linux distribution, Python along with the necessary packages necessary for running and development.
It takes a bit of time to set up a container image initially but afterwards setting everythin on a new machine or in the cloud becomes a breeze.
Listing currently installed packages is done via pip freeze command, the output of which you can then pipe into a file to keep a record of project requirements, for example pip freeze > requirements.txt.

After installation module django version switched to lowest version

I am working on project which use django as main project library. I faced with a need to install for this project new module django-inspect-model and django-inspect after playing with both i understood that these modules aren't appropriate solutions for me and i uninstall them by
pip uninstall django-inspect-model
I don't remember but when i was installing one of these modules i saw in console message about compatibility with my django version but i didnt pay attention for it. Because module was succesfully installed and after deinstallation both of them i tried to run
python manage.py shell
which didn't run and printed in console that some of my installed modules imporerly configured for example celery. After googling message about error i understand that this kind of errors rise due to incompatibility with django and i decided to check my django version by
python -c "import django;print(django.__version__)"
Which promted me that installed django version is 1.8.6 but i exactly knew that before above described actions my django's version was 2.2.0
After checking of my requirements.txt file there isn't a doubt that installed django version was 2.2.0. How it's possible that installed module can switch django on lowest version than it was? Is here anyone who faced with the same situation ? What if i only upgrade my project installed modules (django too) using the same requirements.txt? I am afraid that my actions can broke my whole project
While installing some packages, Django switches to lower versions due to compatibility issues. In case you want the previous version back, you need to upgrade your Django with the following command:
python -m pip install -U Django
or
pip install --upgrade django

Trying to use python 2.7 when 3.5 is default

Currently I can use py2.7 by using a terminal alias. My problem is that when I try and run a script, it depends on a module requests. I have tried to pip install this and even download the requests2.7 folder and sudo python setup.py install. Even though I was aliased with python2.7, This resulted in it being downloaded to my python 3.5 site.
Installed /anaconda/lib/python3.5/site-packages/requests-2.7.0-py3.5.egg
Processing dependencies for requests==2.7.0
Finished processing dependencies for requests==2.7.0
How can I fix this? I need to run scripts as 2.7 but my default seems to be 3.5.

How to install pymssql to Python 3.4 rather than 2.7 on Ubuntu Linux?

I'm not overly familiar with Linux and am trying to run a Python script that is dependent upon Python 3.4 as well as pymssql. Both Python 2.7 and 3.4 are installed (usr/local/lib/[PYTHON_VERSION_HERE]). pymssql is also installed, except it's installed in the Python 2.7 directory, not the 3.4 directory. When I run my Python script (python3 myscript.py), I get the following error:
File "myscript.py", line 2, in
import pymssql
ImportError: No module named 'pymssql'
My belief is that I need to install pymssql to the Python 3.4 folder, but that's my uneducated opinion. So my question is this:
How can I get my script to run using Python 3.4 as well as use the pymssql package (sorry, probably wrong term there)?
I've tried many different approaches, broken my Ubuntu install (and subsequently reimaged), and at this point don't know what to do. I am a relative novice, so some of the replies I've seen on the web say to use ENV and separate the versions are really far beyond the scope of my understanding. If I have to go that route, then I will, but if there is another (i.e. easier) way to go here, I'd really appreciate it, as this was supposed to just be a tiny thing I need to take care of but it's tied up 12 hours of my life thus far! Thank you in advance.
It is better if when you run python3.4 you can have modules for that version.
Another way to get the desire modules running is install pip for python 3.4
sudo apt-get install python3-pip
Then install the module you want
python3.4 -m pip install pymssql
The easiest way is to use virtual environments instead of system paths or environment scripts. See official Python package installation guide.
All you need to do is to
# Create fresh Python environemnt
virtualenv -p python3.4 my-venv
# Activate it in current shell
source my-venv/bin/activate
# Install packages
pip install mysqlclent
Note that mysqlclient is Python 3.x compatible version.

Django is installing to the wrong python version

I had python 2.6 and I downloaded Django. I found that python 2.6 throws errors when trying to run django, so I downloaded python 2.7. Now, typing python in the terminal runs 2.7 but the django library isn't in the 2.7 folder. So I uninstalled django using:
sudo pip uninstall django
and that worked just fine. When i used the command:
sudo pip install django
it installed into the python 2.6 instead of python 2.7.
How can I install django into python 2.7 instead of python 2.6?
(I am running a MacBook Pro on 1.6, and I was told to not uninstall the base version of python because so many of the systems use 2.6)
You need to install pip for python2.7. If it's installed, you should be able to see it using which pip-2.7.
It's better not to touch system python. Use homebrew to install your own.
The problem is that you are running pip from your default Python installation (2.6), read this: How to run multiple python version on Windows, maybe answers give you how to solve in your OS X.
You can view the version of your default Python installation by executing python -V, there is a way to specify which version to use when you execute python, in Linux you can create an alias (alias python=python2.7) in your $HOME/.bash_profile, OS X must have something similar, then install pip using your preferred version.
BTW, It's recommended to use virtualenv

Categories