Outils pour utilisateurs

Outils du site


issue148:python

Ceci est une ancienne révision du document !


Last month, I talked about Python 3.8.0b2 and I suggested that if you wanted to try it, you should consider a virtual environment. I then realized that I hadn't really discussed virtual environments before. So, I'm going to now. What is a Python Virtual Environment? Basically, it allows you to manage multiple versions of Python (and pip) easily, without messing up any of the packages or configurations that you already have. Why would you have multiple versions of Python? I know my environment is probably somewhat different from your needs, but I have multiple machines, and on each, my “main” version of Python is either 3.6 or 3.7 depending on what I'm doing. For example, I have one instance of Python 3.6 for machine learning programming, one Python 3.7 for “normal” work, one for testing of Python 3.8 beta, and I still have a version of Python 2.7 just-in-case. While it's easy to deal with a version of Python 2.x and a version of 3.x on a single machine, sometimes library versions will conflict, and that can cause programs to stop working.

What's the solution? I found a project called pyenv that pretty much does everything you need without much pain. You can find it at https://github.com/pyenv/pyenv. I found a great installation guide at https://realpython.com/intro-to-pyenv/. It is this guide that I used to install pyenv on my laptop, and I'll try to distill it down to get you up and running in the least amount of time.

Getting Started I'll limit my instructions to those for a PC using Ubuntu/Linux Mint/Debian or some other similar Linux distribution. If you are on a MAC, or use some other Linux distribution, there are many pieces of helpful information on the two above noted sites, and on others – with a simple web search. There are two ways to install pyenv. There is an easy way and a hard way. I'm going to discuss the easy way. The reason for this is that the easy way not only installs pyenv, but also installs some other pyenv tools that will be helpful – like pyenv-virtualenv, pyenv-update, and more. While you can manually get these installed, this way is so much easier.

The first thing you need to do is install the dependencies. You probably have most of them, but run the install command below just to be safe. Open a terminal and enter… sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \ libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \ xz-utils tk-dev libffi-dev liblzma-dev python-openssl git Now you need to make a decision on where you are going to install pyenv. I suggest you put it into the home directory. Change to whatever directory you choose, and enter: $ curl https://pyenv.run | bash

This runs the installer for pyenv. Close your terminal and reopen it. This loads any changes to the .bashrc file, or, alternatively, you can enter: $ source ~/.bashrc Next, we need to modify the .bashrc file. If you are using a different version, it might be .bash_profile. In your terminal, type: $ gedit ~/.bashrc and add the following lines (next page, top right) to the bottom of the file. Make sure that the first line defining the PYENV_ROOT points to the correct directory.

Save your .bashrc file and reload it. As before, this can be done by either closing and re-opening the terminal, or by simply typing: $ source ~/.bashrc This will cause the .bashrc to reload. Now, just to be complete, run an update on pyenv. $ pyenv update Now we have to install a version of Python. Right now, let's do the latest version of 3.7, which is 3.7.4. Again in your terminal window, type: $ pyenv install –list

This will list all of the available packages that you can install. It's a VERY long list, with the actual Python versions near the top. You'll also see versions of jython, ironpython, anaconda and anaconda3, activepython, and more. Right now, we can verify that the version text we need to use is “3.7.4”. (In a little bit, we'll also add the latest available version of 3.8.0). To install, type: $ pyenv install 3.7.4 This takes about five minutes on my old laptop, so go get a cup of coffee or tea and come back. Once the install is finished, type: $ pyenv versions You should see something like this… $ pyenv versions system * 3.7.4 (set by /home/greg/.python-version)

A couple of things here to note. First, you'll see * 3.7.4 which lets you know which version is set as the current default version of Python when using pyenv. Next, there is a “system” version, which is your regular version before we started this process. You can always use this without using pyenv. To prove this, in your terminal, type: $ python -V You should see whichever version you normally use. Now, you can always see what versions that pyenv has installed by doing a: $ ls ~/pyenv/versions/

This is where all of the pyenv Python versions are located. If you ever want to delete one, simply type: rm -rf ~/pyenv/versions/{version number} in a terminal window. For example, if I wanted to delete the 3.7.4 version, I'd do: $ rm -rf ~/pyenv/versions/3.7.4 Or, you can do it with a pyenv command: $ pyenv uninstall 3.7.4 But don't do that yet. Let's tell pyenv that we want to use the 3.7.4 version we just installed. $ pyenv local 3.7.4

Now ask python what version it is… $ python -V What you should see is something like this… Python 3.7.4 If not, try doing “exec $SHELL” and try again. Now just for sanity sake, go back to the system version. $ pyenv local system $ python -V

You should see your normal Python instance restored. Now, let's install the latest version of 3.8.0 in pyenv (which, at this writing, is 3.8.0b2). $ pyenv install 3.8-dev After about 5 minutes, everything will be installed. To verify, do the following: $ pyenv versions $ pyenv local 3.8-dev $ python -V Finally, let's make sure that we are using the correct pip – so we can install some libraries… $ pyenv which pip

You should see something like: /home/greg/pyenv/versions/3.8-dev/bin/pip and to further prove it to ourselves… $ pip –version pip 19.0.3 from /home/greg/pyenv/versions/3.8-dev/lib/python3.8/site-packages/pip (python 3.8) So now we know that our version of Python is 3.8.0b2, and the pip that we are using is also from python 3.8. As normal, let's do a pip list to see what library packages are installed. $ pip list and you should see… Package Version ———- ——- pip 19.0.3 setuptools 40.8.0

You might also get a notice to upgrade pip. Go ahead and do that if you want, then we'll set up a virtual environment. Notice that we are using “pip” and not “pip3”. It actually doesn't matter which you use when you are working with a pyenv install. They are all the same. (There is an issue that some are having where the wrong pip is being used when going by to the system version. As a safety check, when using system, do a “pip3 –version” after setting back to system version). Now, we'll deal with the virtual environment. As I said earlier, this is so we can have various special libraries installed without causing issues with our “normal” environment. Since we chose the easy install option, the virtualenv plugin is already installed. We'll create a virtual environment for our 3.8-dev install. The basic syntax is “pyenv virtualenv <python version> <environment name>”. So, for our example, we'll do… $ pyenv virtualenv 3.8-dev 38beta and the response should be something like: Looking in links: /tmp/tmpby1g9af4 Requirement already satisfied: setuptools in /home/greg/pyenv/versions/3.8-dev/envs/38beta/lib/python3.8/site-packages (40.8.0) Requirement already satisfied: pip in /home/greg/pyenv/versions/3.8-dev/envs/38beta/lib/python3.8/site-packages (19.0.3)

Now we want to activate our virtual environment $ pyenv activate 38beta You should see: pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior. (38beta) greg@greg-Latitude-E5500:~$ The first thing you should notice is the (38beta) that appears before your prompt. This lets you know that you are now in the virtual environment 38beta. Next, notice the warning notice above. This says that the prompt change will be going away in some future version. Many users are trying to get the maintainers of pyenv to change their minds, since this can tell you what, if any, virtual environment you are using just at a glance.

Now, verify you are using the correct python version… $ python -V Python 3.8.0b2+ And verify the pip version points to the correct python installation… (38beta) greg@greg-Latitude-E5500:~$ pip –version pip 19.1.1 from /home/greg/pyenv/versions/3.8-dev/envs/38beta/lib/python3.8/site-packages/pip (python 3.8) To get out of the virtual environment, simply use… $ pyenv deactivate This should reset the prompt back to its “normal” environment.

At this point, you should now be able to move around using different Python versions pretty easily, activate and deactivate your virtual environments, and install new versions of python. I should say that whatever you do, don't use python 3.8.0 for any serious production work until the full release comes out sometime in October or November. The actual release is currently scheduled for October 21, 2019. Here is the schedule as of now… 3.8.0 beta 3: Monday, 2019-07-29 3.8.0 beta 4: Monday, 2019-08-26 3.8.0 candidate 1: Monday, 2019-09-30 3.8.0 candidate 2: Monday, 2019-10-07 (if necessary) 3.8.0 final: Monday, 2019-10-21

If you want to keep up with the latest beta and release candidates within your pyenv world, when a new version is released, give it a day or two before you try to install the new one. Get a list from pip of the library packages you have installed in your beta setup (pip list > pippkgs38beta.txt), and use this raw file to create a requirements.txt file that you can use to automate the re-installation process, and finally remove the old beta or release candidate (rm -rf ~/pyenv/versions/3.8-dev), then reinstall the new version. Just for your information, Python 3.9 development is already underway. The first alpha release is scheduled for 2019-09-13, the first beta is scheduled for 2020-01-20 and final release is scheduled for 2020-06-08.

issue148/python.1567661766.txt.gz · Dernière modification : 2019/09/05 07:36 de d52fr