I have a Folder (python311) where I have all of the Python Components stored ("Lib", "Scripts", "python.exe" ...) which is on this Path: D:\python311.
Now I want to move this Folder (python311) into another Folder (Code) -> Path: D:\Code\python311.
Using VS Code it lets me choose the Interpreter which is fine, but when I want to intsll new modules with pip, it does not work. It tries to create an process between the Interpreter of the old Path (D:\python311\python.exe), which is no longer existent, and the new Path where pip is stored (D:\Code\python311\Scripts\pip.exe).
Solutions that I can think of would be for example reinstalling Python. I don't know if it can be solved through environment variables but it won't work because I store the Python Components on an external Drive.
You can solve it using environment variables even if it is on an external drive.
You will need to remove the old entry in the PATH variable and add the new entry (the new python path) or edit the old entry to include the new path.
Change path in environment variables -> Path
Furthermore, I have all my Pythons installed in same location:
PS C:\Python> ls
Directory: C:\Python
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 22.8.2022 11.23 p105_32
d---- 15.8.2022 17.12 p105_64
When I want to use different versions I create a new virtual environment Virtualenvwrapper.
mkvirtualenv envname -p C:/Python/p105_32
workon envname
Related
So I recenetly installed ROS on my system after which I cannot create a python virtual environment without the ROS dependencies. This is my pip3 freeze's output. I have truncated the ouput to fit StackOverflow's standards.
dynamic-reconfigure==1.7.1
gazebo-plugins==2.9.2
gazebo-ros==2.9.2
gencpp==0.6.5
geneus==3.0.0
genlisp==0.4.18
genmsg==0.5.16
gennodejs==2.0.2
genpy==0.6.15
image-geometry==1.15.0
interactive-markers==1.12.0
joint-state-publisher==1.15.0
joint-state-publisher-gui==1.15.0
laser-geometry==1.6.7
message-filters==1.15.11
python-qt-binding==0.4.4
qt-dotgraph==0.4.2
qt-gui==0.4.2
qt-gui-cpp==0.4.2
qt-gui-py-common==0.4.2
resource-retriever==1.12.6
rosbag==1.15.11
rosboost-cfg==1.15.8
rosclean==1.15.8
roscreate==1.15.8
rosgraph==1.15.11
roslaunch==1.15.11
roslib==1.15.8
roslint==0.12.0
roslz4==1.15.11
rosmake==1.15.8
rosmaster==1.15.11
rosmsg==1.15.11
rosnode==1.15.11
rosparam==1.15.11
rospy==1.15.11
rosservice==1.15.11
rostest==1.15.11
rostopic==1.15.11
rosunit==1.15.8
roswtf==1.15.11
rqt-action==0.4.9
rqt-bag==0.5.1
rqt-bag-plugins==0.5.1
rqt-console==0.4.11
You followed all of the ROS install instructions from the wiki. The last step is adding the command source /opt/ros/{distro}/setup.bash to your .bashrc file. This is the command that actually setups your PYTHONPATH and other needed variables.
If you don't want it to be automatically added you can simple remove that line from your bashrc. You'll just have to manually source the file from now on.
My Mac has contained multiple python versions so that I would like to fix one as my default python.
In the current, it has set the path of anaconda python. I have stopped to use the anaconda-python.
And I would like to change the python3 as default following with path:
/usr/local/bin/python3
How can I do this?
Hope:
python3 as default that path
jupyter notebook would follow that path
pip3 install package would follow that path
Update your environment variable PATH to include your required python path. Append the python path in the beginning of the environment variable PATH, so that first it'll look in that path.
You need to update that in .bash_profile file. For that go to the root path and look for the mentioned file. Run ls-a to check whether the file is present.
cd ~
ls -a
Open the file
vi .bash_profile
Add below commands in the file (press i to go into insert mode)
# Setting PATH for Python
export PATH={ur path}:$PATH
(Save the file by pressing :wq)
Check whether the update is made by running
echo $PATH
You can change the path to this by editing the .bashrc file
export PATH="/usr/local/bin/python3:${PATH}"
I'm using setuptools for python packaging where I define console script entry points the usual way in the setup.py file:
setup.py
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
setup(...
name='my_project',
entry_points={'console_scripts':['my_entry_name=my_package.scripts.my_python_script:main'
]},
...
)
After installing the package, I can call this entry point from a batch file like this:
my_CURRENT_batch_file.command
#!/bin/bash
cd "$(dirname "$0")" # set the working directory as the command file locations
~/anaconda3/envs/my_env_name/bin/entry_point_name <my script args>
While this works, the use of the virtual environment is causing me to include all the path info before the entry point call, which in my view really destroys the simplicity an entry point is supposed to provide to the consumer of a script. Is there a way to get setuptools to register the entry point system-wide so that I can call the entry point without the path like this?:
my_DESIRED_batch_file.command
#!/bin/bash
cd "$(dirname "$0")" # set the working directory as the command file locations
entry_point_name <my script args>
Without this complication introduced by the virtual environments, a console script entry point lets the script consumer use a script without having to know where the script is installed or even what language it's written in. I'd like to preserve this simplicity even when packaging in virtual environments.
What I've tried -
I located the actual entry point file in the virtual environment after installing the package:
/anaconda3/envs/my_env/bin/my_entry_name
and placed a copy of this file in the main bin path:
/anaconda3/bin/my_entry_name
and found that I can then call the entry point without the path, as desired, however this is a manual step I don't want to make script consumers to do. Is there a way to get setuptools to place the entry point file in the general bin path rather than the environment bin or some other automatic means to this end?
My setup
OS: macOS Catalina
Shell: bash (yes, I changed it back after Catalina update and suppressed the nagging 'zsh is now default' message)
IDE: PyCharm 19.1 Pro
Anaconda: 4.4.7 (note: was moved from root to User/my_user/ by Catalina update)
Python: 3.7
Virtual env type: conda
Looks like conda run could help.
I can't test it, but looks like it could allow to write a much simpler shell script. Something like that:
#!/usr/bin/env sh
conda run -n ENV my_entry_name "$#"
Seems to be an experimental feature though.
I have recently installed the "official" Python 3.5 on Mac Os X (El Capitan). During this installation, Python changes the global path to include /Library/Frameworks/Python.framework/Versions/3.5/bin (this can be seen using for instance /usr/libexec/path_helper).
However, I am unable to understand where this "set" is done (there is nothing nor in /etc/paths neither in /etc/paths.d). I would like to remove this entry from the global path (without removing the installation itself because it can be always useful).
I have got the same issue. After a SUPER LONG search I found this file: "~/.zprofile". Check within this file and you will find the following code:
# Setting PATH for Python 3.5
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
Just remove this code and save the file.
I also had a similar output when running:
echo $PATH
Standard out-put result:
/Library/Frameworks/Python.framework/Versions/3.9/bin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
The following sequence of steps solved the issue for me using zsh (macOS Big Sur Version 11.1)
cd /Users/JohnDoe
vim .zprofile
Remove the following PATH settings and save the file:
# Setting PATH for Python 3.8
#The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
# Setting PATH for Python 3.9
# The original version is saved in .zprofile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}"
export PATH
Refresh zsh profile from the users home directory:
source .zshrc
Check your work:
echo $PATH
Standard out-put result:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
To remove python 3.5 from your mac try below command
cd /Library/Frameworks/Python.framework/Versions
sudo rm -rf 3.5
What file do I edit, and how? I created a virtual environment.
The most elegant solution to this problem is here.
Original answer remains, but this is a messy solution:
If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's bin/activate file:
export PYTHONPATH="/the/path/you/want"
This way, the new PYTHONPATH will be set each time you use this virtualenv.
EDIT: (to answer #RamRachum's comment)
To have it restored to its original value on deactivate, you could add
export OLD_PYTHONPATH="$PYTHONPATH"
before the previously mentioned line, and add the following line to your bin/postdeactivate script.
export PYTHONPATH="$OLD_PYTHONPATH"
The comment by #s29 should be an answer:
One way to add a directory to the virtual environment is to install virtualenvwrapper (which is useful for many things) and then do
mkvirtualenv myenv
workon myenv
add2virtualenv . #for current directory
add2virtualenv ~/my/path
If you want to remove these path edit the file myenvhomedir/lib/python2.7/site-packages/_virtualenv_path_extensions.pth
Documentation on virtualenvwrapper can be found at http://virtualenvwrapper.readthedocs.org/en/latest/
Specific documentation on this feature can be found at
http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv
You can create a .pth file that contains the directory to search for, and place it in the {venv-root}/lib/{python-version}/site-packages directory. E.g.:
cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
echo /some/library/path > some-library.pth
The effect is the same as adding /some/library/path to sys.path, and remain local to the virtualenv setup.
Initialize your virtualenv
cd venv
source bin/activate
Just set or change your python path by entering command following:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
for checking python path enter in python:
python
\>\> import sys
\>\> sys.path
I modified my activate script to source the file .virtualenvrc, if it exists in the current directory, and to save/restore PYTHONPATH on activate/deactivate.
You can find the patched activate script here.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.
Then I added something like this to my .virtualenvrc:
export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}/some/library/path"
It's already answered here -> Is my virtual environment (python) causing my PYTHONPATH to break?
UNIX/LINUX
Add "export PYTHONPATH=/usr/local/lib/python2.0" this to ~/.bashrc file and source it by typing "source ~/.bashrc" OR ". ~/.bashrc".
WINDOWS XP
1) Go to the Control panel
2) Double click System
3) Go to the Advanced tab
4) Click on Environment Variables
In the System Variables window, check if you have a variable named PYTHONPATH. If you have one already, check that it points to the right directories. If you don't have one already, click the New button and create it.
PYTHON CODE
Alternatively, you can also do below your code:-
import sys
sys.path.append("/home/me/mypy")