Modify Python Path for Python3 only - python

I am developing in both Python 3 and Python 2.6, and have both versions installed. With Python 3, however, the path to lots of the good modules (time, math, ...) is not part of my Python path. I can add the directory to the path, but it's tedious.
Is there a way to permanently modify the path for my Python 3 installation without affecting Python 2?

Create virtual environment: http://pypi.python.org/pypi/virtualenv/
Install packages you want in virtual environment.

If you can't import time, math modules that are in stdlib then your installation of Python 3 is broken.
When you run python setup.py install the files are installed in correct place for current python executable be it a system python or python from a virtualenv environment. The same goes for pip.
You don't need to modify any paths, just use an appropriate executables.

Related

Why is Python Installation in "Library" directory macOS?

I'm on a new Mac and installing Python. I believe I reinstalled my Python using Homebrew. The path, however, is unusual to me, and I'm wondering why it is located here:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
I'm used to Python being somewhere like this, instead (which is also is):
/Users/user.name/Library/Python/3.9/bin
What is different about the first installation? Are there any special considerations if my Python is located here?
/Library/Frameworks/Python.framework/Versions/3.x/bin/python3 is the Python distribution shipped with macOS.
It's usually a good practice to create your own Python environment for your projects instead of using the default Python installation (consider reading about pyenv).
If you want to use your Homebrew installation, run brew info python to find your Python installation path, then append the parent directory's path to your PATH variable by editing ~/.zshrc (assuming you're using zsh, the default shell).
$ brew info python
....
Python has been installed as
/opt/homebrew/bin/python3
...
# inside ~/.zshrc
...
export PATH="/opt/homebrew/bin:$PATH"
....

How do I reroute a project to another Python version in PyCharm? (Error: No Python at 'C:\Users\...\python.exe')

I wanted to replace Python 3.8 32-bit with the 64-bit version to install the face_recognition module, so I deleted the previous version and tried to re-route the project to the new Python version by going to File > Settings > Project Interpreter > Show all > Show Paths for Selected Interpreter, and adding all the Python files from the new folder and getting rid of the old ones.
However, it's still showing me this error when I try to install the module:
(Will2.0) C:\Users\solei\PycharmProjects\Will>pip install face_recognition
No Python at 'C:\Users\solei\AppData\Local\Programs\Python\Python38-32\python.exe'
I've also tried going to the Windows System Properties and changing everything that says "Python38-32" there, but it's still not working. It does work when I make a new environment, though, so at least I know that Python installed properly. It's just this one environment that is tripping me up (I'd prefer not to make a new project for this, btw. I've already installed a lot of modules in it.).
Your selected interpreter is not the system interpreter you've replaced with the 64-bit version, but your project's virtual environment interpreter. The virtual environment's files weren't changed in that process and need to be updated before you can use that environment again.
The system interpreter is your Python interpreter installed using the installation executable. In your case it is located in C:\Users\solei\AppData\Local\Programs\Python\Python38\. You can have multiple system interpreters installed, such as having Python 2.7, Python 3.7 and Python 3.8 side-by-side.
The virtual environment interpreter is a copy of another interpreter created using the venv package from the Python standard library. You can have many virtual environments interpreters in the system (one or more for every project, for example)
The base interpreter is the interpreter that was used as a template for the venv package. Every virtual environment interpreter has its base interpreter (usually a system interpreter) that it requires to run. Changing or upgrading the base interpreter requires updating the virtual environment.
If we take a quick look at the documentation, a virtual environment is described as
a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
That means you can setup an individual environment for every project, which will contain its own packages. The environment is a very efficient way of managing project packages, that's why PyCharm suggests a creation of such environment over the system interpreter by default. In short, it allows you to have two different versions of the same package used by two different projects, without the packages conflicting with each other.
This also explains why your virtual environment files weren't affected by your upgrade.
Now, I am unfortunately no Python expert. I had to spend some time examining how Python handles virtual environments on Windows and Ubuntu. It seems the environment always requires the base system interpreter present in the system. If you remove or change the location of the base interpreter, the environment will fail to function.
As I mentioned before editing this answer, you can in theory simply edit the pyenv.cfg file located in the root folder of the virtual environment. In practice, that will only work in simple cases and it is not the intended way of updating virtual environments.
You need to upgrade your virtual environment's files to work with your new system interpreter. That can mean the 64-bit version over the 32-bit version, or even a newer version of Python - such us upgrading from 3.7 to 3.8.
Close PyCharm
Check if the system interpreter you want to upgrade to is on the system Path
You can quickly check by running
python -c "import platform; print(platform.architecture())"
For you, the output should look like this
('64bit', 'WindowsPE')
If your output is different, you'll need to prefix the absolute path to the Python executable in step 4).
Navigate to the virtual environment's directory
The directory you're looking for contains the Include, Lib and Scripts directories and the pyenv.cfg file. From your screenshots, it seems this directory is your project's root directory, so in your case:
cd C:\Users\solei\PycharmProjects\Will2.0\
Upgrade the virtual environment
python -m venv --upgrade .
... or if Python is not on your path
C:\Users\solei\AppData\Local\Programs\Python\Python38\python.exe -m venv --upgrade .
The . in the commands refers to the current directory.
Open PyCharm and verify your environment is working correctly
... or simply try to run pip directly from the command line. Note you need to first activate the virtual environment by running the Scripts\activate.bat batch file.
If the above-mentioned method doesn't work, you might have to create a new virtual environment. You can create one easily without making a new PyCharm project. See this PyCharm documentation for reference. However, you'll still need to redownload all the required packages again.
For the simplicity, I recommend creating the new virtual environment in a .venv folder located in the project's root.
Disclaimer
I tested only the Python's behavior alone on a fresh Windows installation inside the Windows Sandbox. I was able to install the 32-bit Python, create a virtual environment, replace Python with the 64-bit version and upgrade the virtual environment to have it launch correctly again.

How exactly works Python venv folder? My project use the dependencies into this venv folder executing the project using a different Python version?

I am an absolute beginner with Python (I came from Java) and I have the following doubt about the venv folder in my project.
So basically I have this project structure:
As you can see it contains the venv folder. Reading on the Python official documentation:
https://docs.python.org/3/library/venv.html
I can read:
The venv module provides support for creating lightweight “virtual
environments” with their own site directories, optionally isolated
from system site directories. Each virtual environment has its own
Python binary (which matches the version of the binary that was used
to create this environment) and can have its own independent set of
installed Python packages in its site directories.
So from what I can understand it means that, using venv, every project have its own venv folder containing:
The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).
The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)
This because my PyCharm IDE is set to use Python 3.7
If this reasoning is correct (I am asbolutly not sure) it means that when I run something like:
python3 packet_sniffer.py
the packet_sniffer.py script (contained in the previous project) will be run using the Python 3 bynary and the dependencies that are into my venv folder. Is it correct?
If my reasoning is correct I have the following doubt:
What happen when I run it using Python 2 by
python packet_sniffer.py
I suppose that it is not using anymore the Python 3 version embedded into the venv folder of my project. And what dependencies of scapy am I using? where does it come from?
As you can see running with python instead python3 I am using Python 2 version:
root#kali:~# python --version
Python 2.7.17
root#kali:~# python3 --version
Python 3.7.5
If you create a virtual environment, and you then activate it, the python interpreter installed in this virtual environment will be run if you call any of these: python foo.py / python3 foo.py. There will be NO differences between calling python foo.py or python3 foo.py (if you installed python3 of course).
The lib folder will contain the associated dependencies that you install through pip in this virtual environment and when you use python interpreter through this virtual environment you will use the aforementioned dependencies
So from what I can understand it means that, using venv, every project have its own venv folder containing:
The bin folder: it contains the Python interpreter binary (in this specific case Python 3.7).
The lib folder: containing the dependencies that I have installed using pip3 (infact I installed scapy and scapy_http via pip3)
Using venv every project has its own virtual environment folder that you can name anything you want, containing yes the bin folder and the lib folder.
python virtual environments
The venv folder is a suggested standard for installing python virtual environments.
To use the virtual environment you must first activate it.
Note: Activate your python virtual environment prior to installing any packages required by your application.
from your command line execute:
source venv/bin/activate
python packet_sniffer.py
Note: You do not need to specify the specific version of python as it defaults to the version used to create the virtual environment.
PyCharm can be configured to utilize python virtual environments
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
If you have the virtual environment activated then everything you say about executing python3 is correct. However, all other commands are still available and will run from wherever they are located assuming they can be found by your shell. So your python command is probably running the system-level installed version of python.
If you want to know exactly where that python interpreter is running from:
python
>>> import sys
>>> print(sys.executable)
C:\Python27\python.exe

Python Modules Only Installing For Python 2

I'm fairly new to python and want to start doing some more advanced programming in python 3. I installed some modules using pip on the terminal (I'm using a mac) only to find out that the modules only installed for python 2. I think that it's because I only installed it to the python 2 path, which I think is because my system is running python 2 by default.
But I have no idea how to get around this. Any ideas?
You need to use pip3. OS X will default to Python 2 otherwise.
When creating your virtual environment (you are using a virtual environment, right?) use pyvenv <foo> instead of virtualenv <foo>, and that will create a Python 3 virtual environment, free of Python 2. Then you are free to use pip and it will install the modules into that venv.

change default pythonpath

I have two python installation. One is installed in default location and other is installed in my home directory. I am using home directory python installation. I am facing problem about pythonpath. When I run sys.path on 2nd python installation it gives me paths for libraries of default python installation
eg.
2nd python is installed in /home/python2.6.1
now when i use sys.path it gives me
/lib/python2.6/lib-old, /lib/python2.6/lib-dynload etc.
instead of
/home/python2.6.1/lib/python2.6/lib-old, /home/python2.6.1/lib/python2.6/lib-dynload
how can I change path for my python installation in home directory
This is because your libraries are installed in /lib or the interpreter has been configured to scan /lib for libraries. You need to recompile Python and give the configure script another prefix for the libraries.
But, this isn't what I would do. I'd install virtualenv and start managing my Python versions that way. This is what I do actually ;) Clean and simple!

Categories