this winter I've been working on a Flask Application following this tutorial.
Today, hence after 3 months, I decided to work again on it, but all my set up seems corrupted.
The application started simply activating the virtualenv and calling flask run.
As for today, the command returns:
No module named 'flask'
and so for pip, pip3 etc. even if all these modules are in venv/bin.
My only guess is that since then, I updated python systemwide to Python 3.8.3rc1, which somehow became the default python in the venv as well, even if I was working in python 3.7. If that is the case, I wouldn't know how to fix this problem.
Do you have any suggestion?
thanks
When you created your virtual environment (let's call it v), two things happened:
v/bin/python was a link to your system Python 3.7
v/lib/python3.7 was created for packages installed to the virtual environment.
When you updated, the v link remained the same, but now it points to Python 3.8, which will look for a library directory named python3.8. Hence, your old Flask installation is invisible to the new Python.
Ideally, you wouldn't use your system Python for anything; install your own Python (under /usr/local/ or something), so that instead of upgrading to a new version of PYthon, you can simply install a new version along side it. Then your virtual environment can continue using whatever version of Python it was created with.
That said, you should probably just recreate your virtual environment from scratch.
Try to update the Pip
->> pip install --upgrade pip
and then install the falsk again
->>pip install flask
and lemme know if it works
Related
I've been coding with R for quite a while but I want to start learning and using python more for its machine learning applications. However, I'm quite confused as to how to properly install packages and set up the whole working environment. Unlike R where I suppose most people just use RStudio and directly install packages with install.packages(), there seems to be a variety of ways this can be done in python, including pip install conda install and there is also the issue of doing it in the command prompt or one of the IDEs. I've downloaded python 3.8.5 and anaconda3 and some of my most burning questions right now are:
When to use which command for installing packages? (and also should I always do it in the command prompt aka cmd on windows instead of inside jupyter notebook)
How to navigate the cmd syntax/coding (for example the python documentation for installing packages has this piece of code: py -m pip install "SomeProject" but I am completely unfamiliar with this syntax and how to use it - so in the long run do I also have to learn what goes on in the command prompt or does most of the operations occur in the IDE and I mostly don't have to touch the cmd?)
How to set up a working directory of sorts (like setwd() in R) such that my .ipynb files can be saved to other directories or even better if I can just directly start my IDE from another file destination?
I've tried looking at some online resources but they mostly deal with coding basics and the python language instead of these technical aspects of the set up, so I would greatly appreciate some advice on how to navigate and set up the python working environment in general. Thanks a lot!
Python uses a different way of installing packages. Python has a thing named venv which stands for Virtual Environment. You install all of your packages in venv. Usually for each new project you make a new venv.
By using Anaconda on windows you install everything within the anaconda environment that you have specified.
python -m pip install "modulename" is a command that will install modulename to your default venv. You will be able to use this module when no other venv is specified. Here is the docs page. And here is a tutorial on how to use venv
By default python uses the same directory you have your code in. e.g. C:/Users/me/home/mypythonfile.py will run in C:/Users/me/home/ and will be able to access files in this directory. However you can use ../ to navigate directories or you can specify an absolute path to file you want to open e.g. with open("C:/system32/somesystemfile.sys") as file
Going over the technical differences of conda and pip:
So Conda is a packaging tool and installer that aims to do more than what pip does; handle library dependencies outside of the Python packages as well as the Python packages themselves. Both have many similar functionalities as well, you can install packages or create virtual environments with both.
It is generally advisable to generally have both conda and pip installed since there are some packages which might not be available with conda but with pip and vice versa.
The commands to install in both the ways is easy enough, but one thing to keep in mind is that
conda stores packages in the anaconda/pkgs directory
pip stores it in directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows
You can use both pip or conda inside the jupyter notebook, it will work just fine, but it may be possible that you get multiple versions of the same package.
Most of the times, you will use cmd only to install a module used in your code, or to create environments, py -m pip install "SomeProject" here basically means that the module "SomeProject" will be downloaded in base env.
You could think of conda as python with a variety of additional functionalities, such as certain pre-installed packages and tools, such as spyder and jupyter. Hence, you must be precise when you say:
I've downloaded python 3.8.5 and anaconda3
Does it mean you installed python in your computer and then also anaconda?
In general, or at least in my opinion, using anaconda has advantages for development, but typically you'll just use a simple python installation in production (if that applies to you).
Anaconda has it's own package registry/repository . When you call conda install <package>, it will search for the package there and install it if available. You would better search it first, for instance matplotlib.
pip is a package manager for the Python Package Index. pip also ships with anaconda. Hence, in an anaconda environment you may install packages from either sources (either using pip install or conda install). For instance, pandas from PyPI and pandas from conda. There is no guarantee that packages exist in both sources. You must either search it first or simply try it.
In your first steps, I would suggest you to stick to only one dev env (either simple python or anaconda, recommend the second). Because that simplifies the question: "which python and which pip is executed in the cmd line?". That said, those commands should work as expected in any terminal, it be a simple cmd or an embedded one like in PyCharm or VS Code.
You could inspect that by running (on windows and linux at least):
which python, which pip.
Honestly, this is a question/answer that falls outside the scope of SO and for more info you would better check official websites, such as for anaconda or search for python vs anaconda blogs.
After updating macOs to Monterey, every new modules I install with pip won’t be imported in my Python programs. My sis.path are good and all the packages I need are in the site-packages. Can somebody help me ?
You need to check your python environment.
try which pip3 or which python3 to check locate your active python directory.
You can have different python versions installed in macOS which often leads to this issue.
Or maybe you are using virtual environments which creates isolated site-packages. If this is intended, then you should run your script within this environment to use the packages that you've installed.
There are so many questions about this, and so many tries I've made and probably bungled...
But let me stick to the real problem: I have a fledgling Django app that I want to insulate from future changes to support software. I think putting in under a virtual environment is the answer. I'd like guidance, even just a pointer to the right Howto (one about migrating, not a fresh install).
My environment is Ubuntu 16.04.3 LTS, apache2, python3.5 and django 2.0. I'll be upgrading to the next LTS, which is why I want to insulate this app from changes.
Complicating matters is the fact that python2 and python 3 are both here, and pyhton2 is the default (what you get when you just call for "python". That makes things weird, for instance, because pip3 is uses the default python, so the output of 'pip3 freeze' is very different from what I get when I run it under python3, and I don't know the details of why.
What has failed in the past is my attempts to do it following guidance aimed at a freshly installed OS. What's more, probably because I did something wrong, pip3 lives in my $HOME/.local/bin/pip3. I don't know how to undo this.
If you have an existing environment that you wish to replicate inside of venv or on another machine inside of venv, then do this command inside your original environment:
pip freeze > requirements.txt
On the other machine or inside the virtual environment ( after having run path/to/venv/bin/activate ), do:
pip install -r path/to/requirements.txt
This should save and then restore your environment.
While working on a new python project and trying to learn my way through virtual environments, I've stumbled twice with the following problem:
I create my virtual environment called venv. Running pip freeze shows nothing.
I install my dependencies using pip install dependency. the venv library starts to populate, as confirmed by pip freeze.
After a couple of days, I go back to my project, and after activating the virtual environment via source venv/bin/activate, when running pip freeze I see the whole list of libraries installed in the system python distribution (I'm using Mac Os 10.9.5), instead of the small subset I wanted to keep inside my virtual environment.
I'm sure I must be doing something wrong in between, but I have no idea how could this happen. Any ideas?
Update:
after looking at this answer, I realized the that when running pip freeze, the pip command that's being invoked is the one at /usr/local/bin/pip instead of the one inside my virtual environment. So the virtual environment is fine, but I wonder what changes in the path might be causing this, and how to prevent them to happen again (my PYTHONPATH variable is not set).
I realized that my problem arose when moving my virtual environment folder around the system. The fix was to modify the activate and pip scripts located inside the venv/bin folder to point to the new venv location, as suggested by this answer. Now my pip freeze is showing the right files.
I got access to the server of my lab yesterday. I was trying to do some scientific computing on it with Numpy or so.
The problem is that the server has a SuSE linux(I never heard of such thing..) installed, and it's very difficult to install anything on it. So the administrator suggested that I should set my python environment locally since I did't have root access.
I have checked the default python interpreter on the server, it has python2.6 and python3.x. But I wish to use python2.7.x.
I followed some instructions to install python2.7 into /home/kevin/.local with something like this:
./configure prefix='/home/kevin/.local'
It works, but not easy, because I have to type
/home/kevin/.local/bin/python
everytime.
So, here is my question: what should I do to set the python2.7 I have installed locally as my default python evironment, so I can use
python test.py
to run my script. And when I use pip, it will install modules locally automatically.
ps: I have went through many of the threads available out there, but they only covers how to install modules locally. So I don't think they meet my purpose. And I also tried virtualenv. As I mentioned before, the default python version is 2.6 in usr/bin/python, so I can only get a python2.6 virtual environment locally, which is not what I want.
Since English is my second language, I'm not sure whether I have got everything clear. If not, please point out. It would be very kind of you to do so. And thanks for help!
If you already have python2.7 and virtualenv installed locally, you already have everything you need.
The piece of information you're missing is that you can specify which version of python you want to use when you create your virtualenv. You can use the -p option for that.
virtualenv -p /home/kevin/.local/bin/python my_virtual_env
Once you activate it, your python command will refer to the proper installation. Also, if you install via pip, your packages will be installed in /home/kevin/.local/
Virtualenv's documentation can help you better understand if you have more issues.